playing nicely automatically!!!

This commit is contained in:
Arjun Patel
2022-01-16 13:40:45 -08:00
parent 8b8cdbef92
commit ff454d10f0
2 changed files with 54 additions and 23 deletions
+47 -19
View File
@@ -11,6 +11,7 @@ import PowerPlayer from "../components/PowerPlayer";
import { Message } from "../models/message"; import { Message } from "../models/message";
import { useAuth } from "./authContext"; import { useAuth } from "./authContext";
import { SendService } from "../services/sendService"; import { SendService } from "../services/sendService";
import { useTeamDashboardContext } from "./teamDashboardContext";
interface AudioContextInterface { interface AudioContextInterface {
selectedTeammate: string; // can only have one selected selectedTeammate: string; // can only have one selected
@@ -79,6 +80,31 @@ export default function AudioContextProvider({ children }) {
new MicRecorder({ bitRate: 128 }) new MicRecorder({ bitRate: 128 })
); );
// playing incoming messages
const { allMessages, messagesByTeamMate } = useTeamDashboardContext();
console.log(allMessages);
console.log(messagesByTeamMate);
useEffect(() => {
if (!allMessages.length) {
toast("no message to play");
return;
}
// only play if it's incoming, as I listened to my message before sending it
if (allMessages[0].receiverUserId == currUser.uid) {
console.log("playing message");
console.log(allMessages[0]);
// play message
setPlayerSrc(allMessages[0].audioDataUrl);
// select the user
// start player on the bottom
}
}, [allMessages]);
const value: AudioContextInterface = { const value: AudioContextInterface = {
selectedTeammate, selectedTeammate,
selectTeamMember, selectTeamMember,
@@ -184,6 +210,11 @@ export default function AudioContextProvider({ children }) {
); );
}, [audioInputDeviceId]); // change it everytime we change the input device }, [audioInputDeviceId]); // change it everytime we change the input device
// auto play
useEffect(() => {
// todo play audio to selected output device
}, []);
// SECTION: recording // SECTION: recording
async function startRecording() { async function startRecording() {
recorder recorder
@@ -224,7 +255,11 @@ export default function AudioContextProvider({ children }) {
function onEndedPlaying(e) { function onEndedPlaying(e) {
toast.success("finished playing"); toast.success("finished playing");
setStartPlaying(false); setPlayerSrc("");
// if there are still items in the player queue, then change the src and play the subsequent messages
// if no more messages, deselect user and hide the player
} }
// todo usecallback hook // todo usecallback hook
@@ -273,9 +308,6 @@ export default function AudioContextProvider({ children }) {
[isRecording, selectedTeammate] [isRecording, selectedTeammate]
); );
const testAudioClip =
"https://firebasestorage.googleapis.com/v0/b/nirvana-ccf04.appspot.com/o/messages%2F0E248EA9-AABC-4921-A06B-F1330DFBEE4A.m4a?alt=media&token=9cc81944-96a8-449f-a08f-50925d776565";
const handleKeyboardShortcut = useCallback( const handleKeyboardShortcut = useCallback(
(event) => { (event) => {
if (event.repeat) { if (event.repeat) {
@@ -311,8 +343,8 @@ export default function AudioContextProvider({ children }) {
if (isSilenceMode) { if (isSilenceMode) {
toast.error("you are in silence mode, please disable it first"); toast.error("you are in silence mode, please disable it first");
} else { } else {
// alright now you are good to play messages // alright now you are good to play last message chunk in conversation with selected user
setStartPlaying(true); // todo create last chunk, and create a playlist
} }
} else { } else {
toast("Invalid keyboard shortcut."); toast("Invalid keyboard shortcut.");
@@ -351,7 +383,7 @@ export default function AudioContextProvider({ children }) {
setSelectedTeamMember(userId); setSelectedTeamMember(userId);
} }
const [startPlaying, setStartPlaying] = useState<Boolean>(false); const [playerSrc, setPlayerSrc] = useState<string>("");
return ( return (
<AudioContext.Provider value={value}> <AudioContext.Provider value={value}>
@@ -361,18 +393,14 @@ export default function AudioContextProvider({ children }) {
{/* <PowerPlayer /> */} {/* <PowerPlayer /> */}
{/* player for audio messages */} {/* player for audio messages */}
{startPlaying ? ( <AudioPlayer
<AudioPlayer autoPlay
autoPlay src={playerSrc}
src={testAudioClip} onPlay={(e) => console.log("onPlay")}
onPlay={(e) => console.log("onPlay")} showSkipControls={true}
showSkipControls={true} onEnded={onEndedPlaying}
onEnded={onEndedPlaying} className="w-screen flex flex-row"
className="w-screen flex flex-row" />
/>
) : (
<></>
)}
</AudioContext.Provider> </AudioContext.Provider>
); );
} }
+7 -4
View File
@@ -176,8 +176,6 @@ export function TeamDashboardContextProvider({ children }) {
// listener for all incoming messages // listener for all incoming messages
useEffect(() => { useEffect(() => {
console.log("first only");
// todo for new messages, change document.title // todo for new messages, change document.title
/** /**
@@ -207,7 +205,6 @@ export function TeamDashboardContextProvider({ children }) {
// update map of teammate to relevant messages // update map of teammate to relevant messages
setMessagesByTeamMate((prevMap) => { setMessagesByTeamMate((prevMap) => {
console.log("settting state");
// if the map contains the teammate userid already, then cool, just unshift to that array // if the map contains the teammate userid already, then cool, just unshift to that array
const newMap = { ...prevMap }; const newMap = { ...prevMap };
if (newMessage.receiverUserId in prevMap) { if (newMessage.receiverUserId in prevMap) {
@@ -238,8 +235,14 @@ export function TeamDashboardContextProvider({ children }) {
return <Loading />; return <Loading />;
} }
const transformedValue = {
...value,
allMessages,
messagesByTeamMate,
};
return ( return (
<TeamDashboardContext.Provider value={value}> <TeamDashboardContext.Provider value={transformedValue}>
{children} {children}
</TeamDashboardContext.Provider> </TeamDashboardContext.Provider>
); );