diff --git a/android/src/main/java/com/johnsonsu/rnsoundplayer/RNSoundPlayerModule.java b/android/src/main/java/com/johnsonsu/rnsoundplayer/RNSoundPlayerModule.java index db287b8..920e66f 100644 --- a/android/src/main/java/com/johnsonsu/rnsoundplayer/RNSoundPlayerModule.java +++ b/android/src/main/java/com/johnsonsu/rnsoundplayer/RNSoundPlayerModule.java @@ -104,6 +104,10 @@ public void setVolume(float volume) throws IOException { @ReactMethod public void getInfo( Promise promise) { + if (this.mediaPlayer == null) { + promise.resolve(null); + return; + } WritableMap map = Arguments.createMap(); map.putDouble("currentTime", this.mediaPlayer.getCurrentPosition() / 1000.0); map.putDouble("duration", this.mediaPlayer.getDuration() / 1000.0); diff --git a/index.d.ts b/index.d.ts index 1828a47..c4ce0b9 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,11 +1,11 @@ -declare module 'react-native-sound-player' { - import { EmitterSubscription } from 'react-native'; +declare module "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; @@ -27,7 +27,7 @@ declare module 'react-native-sound-player' { /** 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, + callback: (data: SoundPlayerEventData) => void ) => EmitterSubscription; /** Play the loaded sound file. This function is the same as `resume`. */ play: () => void; @@ -54,4 +54,4 @@ declare module 'react-native-sound-player' { const SoundPlayer: SoundPlayerType; export default SoundPlayer; -} \ No newline at end of file +} diff --git a/index.js b/index.js index 76bfbeb..8e68314 100644 --- a/index.js +++ b/index.js @@ -1,27 +1,26 @@ /** * @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) + RNSoundPlayer.playSoundFile(name, type); }, - + playSoundFileWithDelay: (name: string, type: string, delay: number) => { - RNSoundPlayer.playSoundFileWithDelay(name, type, delay) + RNSoundPlayer.playSoundFileWithDelay(name, type, delay); }, loadSoundFile: (name: string, type: string) => { - RNSoundPlayer.loadSoundFile(name, type) + RNSoundPlayer.loadSoundFile(name, type); }, setNumberOfLoops: (loops: number) => { @@ -29,66 +28,73 @@ export default { }, playUrl: (url: string) => { - RNSoundPlayer.playUrl(url) + RNSoundPlayer.playUrl(url); }, loadUrl: (url: string) => { - RNSoundPlayer.loadUrl(url) + RNSoundPlayer.loadUrl(url); }, onFinishedPlaying: (callback: (success: boolean) => any) => { if (_finishedPlayingListener) { - _finishedPlayingListener.remove() - _finishedPlayingListener = undefined + _finishedPlayingListener.remove(); + _finishedPlayingListener = undefined; } _finishedPlayingListener = _soundPlayerEmitter.addListener( - 'FinishedPlaying', + "FinishedPlaying", callback - ) + ); }, onFinishedLoading: (callback: (success: boolean) => any) => { if (_finishedLoadingListener) { - _finishedLoadingListener.remove() - _finishedLoadingListener = undefined + _finishedLoadingListener.remove(); + _finishedLoadingListener = undefined; } _finishedLoadingListener = _soundPlayerEmitter.addListener( - 'FinishedLoading', + "FinishedLoading", callback - ) + ); }, - addEventListener: (eventName: 'FinishedLoading' | 'FinishedPlaying' | 'FinishedLoadingURL' | 'FinishedLoadingFile', callback: Function) => _soundPlayerEmitter.addListener(eventName, callback), + addEventListener: ( + eventName: + | "FinishedLoading" + | "FinishedPlaying" + | "FinishedLoadingURL" + | "FinishedLoadingFile", + callback: Function + ) => _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) + RNSoundPlayer.seek(seconds); }, setVolume: (volume: number) => { - RNSoundPlayer.setVolume(volume) + RNSoundPlayer.setVolume(volume); }, setSpeaker: (on: boolean) => { - if(Platform.OS === "android"){ + if (Platform.OS === "android") { console.log("setSpeaker is not implement on Android"); } else { RNSoundPlayer.setSpeaker(on); @@ -99,13 +105,13 @@ export default { unmount: () => { if (_finishedPlayingListener) { - _finishedPlayingListener.remove() - _finishedPlayingListener = undefined + _finishedPlayingListener.remove(); + _finishedPlayingListener = undefined; } if (_finishedLoadingListener) { - _finishedLoadingListener.remove() - _finishedLoadingListener = undefined + _finishedLoadingListener.remove(); + _finishedLoadingListener = undefined; } - } -} + }, +}; diff --git a/ios/RNSoundPlayer.m b/ios/RNSoundPlayer.m index 1b81011..dda42e6 100644 --- a/ios/RNSoundPlayer.m +++ b/ios/RNSoundPlayer.m @@ -117,6 +117,7 @@ @implementation RNSoundPlayer }; resolve(data); } + resolve(nil); } - (void) audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag { diff --git a/package.json b/package.json index aa06a23..fb909bf 100644 --- a/package.json +++ b/package.json @@ -1,24 +1,30 @@ { - "name": "react-native-sound-player", - "version": "0.10.8", - "description": "Play or stream audio files in ReactNative on iOS/Android", - "main": "index.js", - "types": "index.d.ts", - "keywords": [ - "reactnative", - "react-native", - "sound", - "player", - "audio", - "streaming" - ], - "repository": { - "type": "git", - "url": "https://github.com/johnsonsu/react-native-sound-player" - }, - "author": { - "name": "Johnson Su", - "email": "johnsonsu@johnsonsu.com" - }, - "license": "MIT" + "name": "react-native-sound-player", + "version": "0.10.9", + "description": "Play or stream audio files in ReactNative on iOS/Android", + "main": "index.js", + "types": "index.d.ts", + "keywords": [ + "reactnative", + "react-native", + "sound", + "player", + "audio", + "streaming" + ], + "repository": { + "type": "git", + "url": "https://github.com/johnsonsu/react-native-sound-player" + }, + "author": { + "name": "Johnson Su", + "email": "johnsonsu@johnsonsu.com" + }, + "prettier": { + "trailingComma": "es5", + "tabWidth": 2, + "semi": true, + "singleQuote": false + }, + "license": "MIT" }