Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
react-native-stream-player
Advanced tools
Play audio stream and audio files in ReactNative on iOS/Android
Play audio files, stream audio, icecast and showcast streams from URL, using ReactNative.
yarn
or npm
// yarn
yarn add react-native-stream-player
// or npm
npm install --save react-native-stream-player
For RN >= 0.60 you can skip this step.
react-native link react-native-stream-player
{project_root}/android/app/src/main/res/raw/
. Just create the folder if it doesn't exist.playSoundFile(fileName, fileType)
function:import SoundPlayer from 'react-native-stream-player'
try {
// play the file tone.mp3
SoundPlayer.playSoundFile('tone', 'mp3')
// or play from url
SoundPlayer.playUrl('https://example.com/music.mp3')
} catch (e) {
console.log(`cannot play the sound file`, e)
}
Please note that the device can still go to sleep (screen goes off) while audio is playing. When this happens, the audio will stop playing. To prevent this, you can use something like react-native-keep-awake. Or alternatively, for iOS, you can add a Background Mode of
Audio, AirPlay, and Picture in Picture
in XCode. To do this, select your application from Targets, then click onSigning & Capabilities
and addBackground Modes
. once the options for it appear on yourSigning & Capabilities
page select the checkbox withAudio, AirPlay, and Picture in Picture
. This will allow the application to continue playing audio when the app is in the background and even when the device is locked.
playSoundFile(fileName: string, fileType: string)
Play the sound file named fileName
with file type fileType
.
playSoundFileWithDelay(fileName: string, fileType: string, delay: number)
- iOS OnlyPlay the sound file named fileName
with file type fileType
after a a delay of delay
in seconds from the current device time.
loadSoundFile(fileName: string, fileType: string)
Load the sound file named fileName
with file type fileType
, without playing it.
This is useful when you want to play a large file, which can be slow to mount,
and have precise control on when the sound is played. This can also be used in
combination with getInfo()
to get audio file duration
without playing it.
You should subscribe to the onFinishedLoading
event to get notified when the
file is loaded.
playUrl(url: string)
Play the audio from url. Supported formats are:
loadUrl(url: string)
Load the audio from the given url
without playing it. You can then play the audio
by calling play()
. This might be useful when you find the delay between calling
playUrl()
and the sound actually starts playing is too much.
addEventListener(callback: (object: ResultObject) => SubscriptionObject)
Subscribe to any event. Returns a subscription object. Subscriptions created by this function cannot be removed by calling unmount()
. You NEED to call yourSubscriptionObject.remove()
when you no longer need this event listener or whenever your component unmounts.
Supported events are:
FinishedLoading
FinishedPlaying
FinishedLoadingURL
FinishedLoadingFile
// Example
...
// Create instance variable(s) to store your subscriptions in your class
_onFinishedPlayingSubscription = null
_onFinishedLoadingSubscription = null
_onFinishedLoadingFileSubscription = null
_onFinishedLoadingURLSubscription = null
// Subscribe to event(s) you want when component mounted
componentDidMount() {
_onFinishedPlayingSubscription = SoundPlayer.addEventListener('FinishedPlaying', ({ success }) => {
console.log('finished playing', success)
})
_onFinishedLoadingSubscription = SoundPlayer.addEventListener('FinishedLoading', ({ success }) => {
console.log('finished loading', success)
})
_onFinishedLoadingFileSubscription = SoundPlayer.addEventListener('FinishedLoadingFile', ({ success, name, type }) => {
console.log('finished loading file', success, name, type)
})
_onFinishedLoadingURLSubscription = SoundPlayer.addEventListener('FinishedLoadingURL', ({ success, url }) => {
console.log('finished loading url', success, url)
if(!success){
//url not loaded or stream is not available.
}
})
}
// Remove all the subscriptions when component will unmount
componentWillUnmount() {
_onFinishedPlayingSubscription.remove()
_onFinishedLoadingSubscription.remove()
_onFinishedLoadingURLSubscription.remove()
_onFinishedLoadingFileSubscription.remove()
}
...
onFinishedPlaying(callback: (success: boolean) => any)
Subscribe to the "finished playing" event. The callback
function is called whenever a file is finished playing. This function will be deprecated soon, please use addEventListener
above.
onFinishedLoading(callback: (success: boolean) => any)
Subscribe to the "finished loading" event. The callback
function is called whenever a file is finished loading, i.e. the file is ready to be play()
, resume()
, getInfo()
, etc. This function will be deprecated soon, please use addEventListener
above.
unmount()
Unsubscribe the "finished playing" and "finished loading" event. This function will be deprecated soon, please use addEventListener
and remove your own listener by calling yourSubscriptionObject.remove()
.
play()
Play the loaded sound file. This function is the same as resume()
.
pause()
Pause the currently playing file.
resume()
Resume from pause and continue playing the same file. This function is the same as play()
.
stop()
Stop playing, call playSound(fileName: string, fileType: string)
to start playing again.
seek(seconds: number)
Seek to seconds
of the currently playing file.
setSpeaker(on: boolean)
Overwrite default audio output to speaker, which forces playUrl()
function to play from speaker.
setMixAudio(on: boolean)
Only available on iOS. If you set this option, your audio will be mixed with audio playing in background apps, such as the Music app.
setVolume(volume: number)
Set the volume of the current player. This does not change the volume of the device.
setNumberOfLoops(volume: number)
- iOS OnlySet the number of loops. A negative value will loop indefinitely until the stop()
command is called.
getInfo() => Promise<{currentTime: number, duration: number}>
Get the currentTime
and duration
of the currently mounted audio media. This function returns a promise which resolves to an Object containing currentTime
and duration
properties.
// Example
...
playSong() {
try {
SoundPlayer.playSoundFile('engagementParty', 'm4a')
} catch (e) {
alert('Cannot play the file')
console.log('cannot play the song file', e)
}
}
async getInfo() { // You need the keyword `async`
try {
const info = await SoundPlayer.getInfo() // Also, you need to await this because it is async
console.log('getInfo', info) // {duration: 12.416, currentTime: 7.691}
} catch (e) {
console.log('There is no song playing', e)
}
}
onPressPlayButton() {
this.playSong()
this.getInfo()
}
...
FAQs
Play audio stream and audio files in ReactNative on iOS/Android
We found that react-native-stream-player demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.