Get live audio stream data for React Native (works for iOS and Android)
MIT License
80
stars
33
forks
source link
Live Audio Streaming not working in socket using when i receiving the audio file facing the error like Failed to load sound {"extra": -2147483648, "what": 1} #29
`import { Alert, Button, PermissionsAndroid, Platform, StyleSheet, Text, View } from 'react-native'; import React, { useEffect, useState } from 'react'; import io from 'socket.io-client'; import { useNavigation } from '@react-navigation/native'; import Sound from 'react-native-sound'; import LiveAudioStream from 'react-native-live-audio-stream'; import { Buffer } from 'buffer'; import RNFS from 'react-native-fs';
Sound.setCategory('Playback');
const LiveStreaming = () => { const navigation = useNavigation(); const [socket, setSocket] = useState(null); const [isStreaming, setIsStreaming] = useState(false); const [initial, setInitial] = useState(false);
useEffect(() => { const socketInstance = io('https://devtamilcalendar.shrewdbs.com', { path: '/audio-meeting' });
}, []);
const ListenerJoin = () => { if (socket) { const data = { username: "Raone", isSpeaker: false, }; socket.emit('join', data); } };
const SpeakerJoin = () => { if (socket) { const data = { username: "Ravana", isSpeaker: true, }; socket.emit('join', data); } };
const requestAudioPermission = async () => { if (Platform.OS === 'android') { try { const granted = await PermissionsAndroid.request( PermissionsAndroid.PERMISSIONS.RECORD_AUDIO, { title: 'Audio Permission', message: 'App needs access to your microphone to record audio.', buttonNeutral: 'Ask Me Later', buttonNegative: 'Cancel', buttonPositive: 'OK', }, ); if (granted !== PermissionsAndroid.RESULTS.GRANTED) { console.log('Audio permission denied'); } } catch (err) { console.warn(err); } } };
useEffect(() => {
console.log("isStreaming",isStreaming)
}, [ ]);
const startStreaming = () => { if (socket && !isStreaming) { LiveAudioStream.start(); LiveAudioStream.on('data', data => { var chunk = Buffer.from(data, 'base64'); socket.emit('audio_emit', data); // console.log('Emitted audio chunk:',data); });
};
const receiveAudio = async (chunk) => { console.log("Received audio chunk:", chunk); await playSound(chunk); };
const stopStreaming = () => { if (isStreaming) { LiveAudioStream.stop(); setIsStreaming(false); } };
const AdminClear = () => { if (socket) { socket.emit('adminCommand', "clearAll"); } };
const playSound = async (chunk) => { try {
} catch (err) { console.log('Failed to play sound', err); } };
return (
); };
export default LiveStreaming;
const styles = StyleSheet.create({
}); `