@mythologi/react-native-sound-player
Advanced tools
Comparing version 0.11.3 to 0.11.4
@@ -1,59 +0,62 @@ | ||
declare module "react-native-sound-player" { | ||
import { EmitterSubscription } from "react-native"; | ||
declare module '@mythologi/react-native-sound-player' { | ||
import { EmitterSubscription } from 'react-native' | ||
export type SoundPlayerEvent = | ||
| "FinishedLoading" | ||
| "FinishedPlaying" | ||
| "FinishedLoadingURL" | ||
| "FinishedLoadingFile"; | ||
| 'FinishedLoading' | ||
| 'FinishedPlaying' | ||
| 'FinishedLoadingURL' | ||
| 'FinishedLoadingFile' | ||
export type SoundPlayerEventData = { | ||
success?: boolean; | ||
url?: string; | ||
name?: string; | ||
type?: string; | ||
}; | ||
success?: boolean | ||
url?: string | ||
name?: string | ||
type?: string | ||
} | ||
interface SoundPlayerType { | ||
playSoundFile: (name: string, type: string) => void; | ||
playSoundFileWithDelay: (name: string, type: string, delay: number) => void; | ||
loadSoundFile: (name: string, type: string) => void; | ||
playUrl: (url: string) => void; | ||
loadUrl: (url: string) => void; | ||
playSoundFile: (name: string, type: string) => void | ||
playSoundFileWithDelay: (name: string, type: string, delay: number) => void | ||
loadSoundFile: (name: string, type: string) => void | ||
playUrl: (url: string) => void | ||
loadUrl: (url: string) => void | ||
/** @deprecated please use addEventListener*/ | ||
onFinishedPlaying: (callback: (success: boolean) => unknown) => void; | ||
onFinishedPlaying: (callback: (success: boolean) => unknown) => void | ||
/** @deprecated please use addEventListener*/ | ||
onFinishedLoading: (callback: (success: boolean) => unknown) => void; | ||
/** 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. */ | ||
onFinishedLoading: (callback: (success: boolean) => unknown) => void | ||
/** | ||
* 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. | ||
*/ | ||
addEventListener: ( | ||
eventName: SoundPlayerEvent, | ||
callback: (data: SoundPlayerEventData) => void | ||
) => EmitterSubscription; | ||
callback: (data: SoundPlayerEventData) => void, | ||
) => EmitterSubscription | ||
/** Play the loaded sound file. This function is the same as `resume`. */ | ||
play: () => void; | ||
play: () => void | ||
/** Pause the currently playing file. */ | ||
pause: () => void; | ||
pause: () => void | ||
/** Resume from pause and continue playing the same file. This function is the same as `play`. */ | ||
resume: () => void; | ||
resume: () => void | ||
/** Stop playing, call `playSound` to start playing again. */ | ||
stop: () => void; | ||
stop: () => void | ||
/** Seek to seconds of the currently playing file. */ | ||
seek: (seconds: number) => void; | ||
seek: (seconds: number) => void | ||
/** Set the volume of the current player. This does not change the volume of the device. */ | ||
setVolume: (volume: number) => void; | ||
setVolume: (volume: number) => void | ||
/** Only available on iOS. Overwrite default audio output to speaker, which forces playUrl() function to play from speaker. */ | ||
setSpeaker: (on: boolean) => void; | ||
setSpeaker: (on: boolean) => void | ||
/** 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. */ | ||
setMixAudio: (on: boolean) => void; | ||
setMixAudio: (on: boolean) => void | ||
/** IOS only. Set the number of loops. A negative value will loop indefinitely until the stop() command is called. */ | ||
setNumberOfLoops: (loops: number) => void; | ||
setNumberOfLoops: (loops: number) => void | ||
/** 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. */ | ||
getInfo: () => Promise<{ currentTime: number; duration: number }>; | ||
getInfo: () => Promise<{ currentTime: number; duration: number }> | ||
/** @deprecated Please use addEventListener and remove your own listener by calling yourSubscriptionObject.remove(). */ | ||
unmount: () => void; | ||
unmount: () => void | ||
} | ||
const SoundPlayer: SoundPlayerType; | ||
const SoundPlayer: SoundPlayerType | ||
export default SoundPlayer; | ||
export default SoundPlayer | ||
} |
112
index.js
@@ -1,109 +0,93 @@ | ||
/** | ||
* @flow | ||
*/ | ||
"use strict"; | ||
'use strict' | ||
import { NativeModules, NativeEventEmitter, Platform } from "react-native"; | ||
const { RNSoundPlayer } = NativeModules; | ||
import { NativeModules, NativeEventEmitter, Platform } from 'react-native' | ||
const { RNSoundPlayer } = NativeModules | ||
const _soundPlayerEmitter = new NativeEventEmitter(RNSoundPlayer); | ||
let _finishedPlayingListener = null; | ||
let _finishedLoadingListener = null; | ||
const _soundPlayerEmitter = new NativeEventEmitter(RNSoundPlayer) | ||
let _finishedPlayingListener = null | ||
let _finishedLoadingListener = null | ||
export default { | ||
playSoundFile: (name: string, type: string) => { | ||
RNSoundPlayer.playSoundFile(name, type); | ||
playSoundFile: (name, type) => { | ||
RNSoundPlayer.playSoundFile(name, type) | ||
}, | ||
playSoundFileWithDelay: (name: string, type: string, delay: number) => { | ||
RNSoundPlayer.playSoundFileWithDelay(name, type, delay); | ||
playSoundFileWithDelay: (name, type, delay) => { | ||
RNSoundPlayer.playSoundFileWithDelay(name, type, delay) | ||
}, | ||
loadSoundFile: (name: string, type: string) => { | ||
RNSoundPlayer.loadSoundFile(name, type); | ||
loadSoundFile: (name, type) => { | ||
RNSoundPlayer.loadSoundFile(name, type) | ||
}, | ||
setNumberOfLoops: (loops: number) => { | ||
RNSoundPlayer.setNumberOfLoops(loops); | ||
setNumberOfLoops: loops => { | ||
RNSoundPlayer.setNumberOfLoops(loops) | ||
}, | ||
playUrl: (url: string) => { | ||
RNSoundPlayer.playUrl(url); | ||
playUrl: url => { | ||
RNSoundPlayer.playUrl(url) | ||
}, | ||
loadUrl: (url: string) => { | ||
RNSoundPlayer.loadUrl(url); | ||
loadUrl: url => { | ||
RNSoundPlayer.loadUrl(url) | ||
}, | ||
onFinishedPlaying: (callback: (success: boolean) => any) => { | ||
onFinishedPlaying: callback => { | ||
if (_finishedPlayingListener) { | ||
_finishedPlayingListener.remove(); | ||
_finishedPlayingListener = undefined; | ||
_finishedPlayingListener.remove() | ||
_finishedPlayingListener = undefined | ||
} | ||
_finishedPlayingListener = _soundPlayerEmitter.addListener( | ||
"FinishedPlaying", | ||
callback | ||
); | ||
_finishedPlayingListener = _soundPlayerEmitter.addListener('FinishedPlaying', callback) | ||
}, | ||
onFinishedLoading: (callback: (success: boolean) => any) => { | ||
onFinishedLoading: callback => { | ||
if (_finishedLoadingListener) { | ||
_finishedLoadingListener.remove(); | ||
_finishedLoadingListener = undefined; | ||
_finishedLoadingListener.remove() | ||
_finishedLoadingListener = undefined | ||
} | ||
_finishedLoadingListener = _soundPlayerEmitter.addListener( | ||
"FinishedLoading", | ||
callback | ||
); | ||
_finishedLoadingListener = _soundPlayerEmitter.addListener('FinishedLoading', callback) | ||
}, | ||
addEventListener: ( | ||
eventName: | ||
| "FinishedLoading" | ||
| "FinishedPlaying" | ||
| "FinishedLoadingURL" | ||
| "FinishedLoadingFile", | ||
callback: Function | ||
) => _soundPlayerEmitter.addListener(eventName, callback), | ||
addEventListener: (eventName, callback) => _soundPlayerEmitter.addListener(eventName, callback), | ||
play: () => { | ||
// play and resume has the exact same implementation natively | ||
RNSoundPlayer.resume(); | ||
RNSoundPlayer.resume() | ||
}, | ||
pause: () => { | ||
RNSoundPlayer.pause(); | ||
RNSoundPlayer.pause() | ||
}, | ||
resume: () => { | ||
RNSoundPlayer.resume(); | ||
RNSoundPlayer.resume() | ||
}, | ||
stop: () => { | ||
RNSoundPlayer.stop(); | ||
RNSoundPlayer.stop() | ||
}, | ||
seek: (seconds: number) => { | ||
RNSoundPlayer.seek(seconds); | ||
seek: seconds => { | ||
RNSoundPlayer.seek(seconds) | ||
}, | ||
setVolume: (volume: number) => { | ||
RNSoundPlayer.setVolume(volume); | ||
setVolume: volume => { | ||
RNSoundPlayer.setVolume(volume) | ||
}, | ||
setSpeaker: (on: boolean) => { | ||
if (Platform.OS === "android" || Platform.isTVOS) { | ||
console.log("setSpeaker is not implemented on Android or tvOS"); | ||
setSpeaker: on => { | ||
if (Platform.OS === 'android' || Platform.isTVOS) { | ||
console.log('setSpeaker is not implemented on Android or tvOS') | ||
} else { | ||
RNSoundPlayer.setSpeaker(on); | ||
RNSoundPlayer.setSpeaker(on) | ||
} | ||
}, | ||
setMixAudio: (on: boolean) => { | ||
if (Platform.OS === "android") { | ||
console.log("setMixAudio is not implemented on Android"); | ||
setMixAudio: on => { | ||
if (Platform.OS === 'android') { | ||
console.log('setMixAudio is not implemented on Android') | ||
} else { | ||
RNSoundPlayer.setMixAudio(on); | ||
RNSoundPlayer.setMixAudio(on) | ||
} | ||
@@ -116,11 +100,11 @@ }, | ||
if (_finishedPlayingListener) { | ||
_finishedPlayingListener.remove(); | ||
_finishedPlayingListener = undefined; | ||
_finishedPlayingListener.remove() | ||
_finishedPlayingListener = undefined | ||
} | ||
if (_finishedLoadingListener) { | ||
_finishedLoadingListener.remove(); | ||
_finishedLoadingListener = undefined; | ||
_finishedLoadingListener.remove() | ||
_finishedLoadingListener = undefined | ||
} | ||
}, | ||
}; | ||
} |
{ | ||
"name": "@mythologi/react-native-sound-player", | ||
"version": "0.11.3", | ||
"version": "0.11.4", | ||
"description": "Play or stream audio files in ReactNative on iOS/Android", | ||
@@ -19,2 +19,5 @@ "main": "index.js", | ||
}, | ||
"scripts": { | ||
"prettier": "prettier --write ." | ||
}, | ||
"author": { | ||
@@ -24,9 +27,6 @@ "name": "Johnson Su", | ||
}, | ||
"prettier": { | ||
"trailingComma": "es5", | ||
"tabWidth": 2, | ||
"semi": true, | ||
"singleQuote": false | ||
}, | ||
"license": "MIT" | ||
"license": "MIT", | ||
"devDependencies": { | ||
"prettier": "^2.6.0" | ||
} | ||
} |
@@ -30,4 +30,4 @@ # react-native-sound-player | ||
- On iOS, drag and drop sound file into project in Xcode. Remember to check **"Copy items if needed"** option and **"Add to targets"**. | ||
- On Android, put sound files in `{project_root}/android/app/src/main/res/raw/`. Just create the folder if it doesn't exist. | ||
- On iOS, drag and drop sound file into project in Xcode. Remember to check **"Copy items if needed"** option and **"Add to targets"**. | ||
- On Android, put sound files in `{project_root}/android/app/src/main/res/raw/`. Just create the folder if it doesn't exist. | ||
@@ -40,8 +40,8 @@ 2. Import the library and call the `playSoundFile(fileName, fileType)` function: | ||
try { | ||
// play the file tone.mp3 | ||
SoundPlayer.playSoundFile('tone', 'mp3') | ||
// or play from url | ||
SoundPlayer.playUrl('https://example.com/music.mp3') | ||
// 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) | ||
console.log(`cannot play the sound file`, e) | ||
} | ||
@@ -53,3 +53,3 @@ ``` | ||
> To prevent this, you can use something like [react-native-keep-awake](https://github.com/corbt/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 on `Signing & Capabilities` and add `Background Modes`. once the options for it appear on your `Signing & Capabilities` page select the checkbox with `Audio, 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. | ||
> 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 on `Signing & Capabilities` and add `Background Modes`. once the options for it appear on your `Signing & Capabilities` page select the checkbox with `Audio, 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. | ||
@@ -62,6 +62,5 @@ ## Functions | ||
### `playSoundFileWithDelay(fileName: string, fileType: string, delay: number)` - iOS Only | ||
Play the sound file named `fileName` with file type `fileType` after a a delay of `delay` in *seconds* from the current device time. | ||
Play the sound file named `fileName` with file type `fileType` after a a delay of `delay` in _seconds_ from the current device time. | ||
@@ -81,4 +80,4 @@ ### `loadSoundFile(fileName: string, fileType: string)` | ||
- [AVPlayer (iOS)](https://stackoverflow.com/questions/21879981/avfoundation-avplayer-supported-formats-no-vob-or-mpg-containers) | ||
- [MediaPlayer (Android)](https://developer.android.com/guide/topics/media/media-formats) | ||
- [AVPlayer (iOS)](https://stackoverflow.com/questions/21879981/avfoundation-avplayer-supported-formats-no-vob-or-mpg-containers) | ||
- [MediaPlayer (Android)](https://developer.android.com/guide/topics/media/media-formats) | ||
@@ -181,3 +180,2 @@ ### `loadUrl(url: string)` | ||
### `setNumberOfLoops(volume: number)` - iOS Only | ||
@@ -184,0 +182,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
20
54572
1
142
211