Skip to content

Commit

Permalink
Fix null pointer error on Android and version bump
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsonsu committed Jun 1, 2021
1 parent ddaf4c3 commit 4fd0430
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
16 changes: 8 additions & 8 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -54,4 +54,4 @@ declare module 'react-native-sound-player' {
const SoundPlayer: SoundPlayerType;

export default SoundPlayer;
}
}
76 changes: 41 additions & 35 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,94 +1,100 @@
/**
* @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) => {
RNSoundPlayer.setNumberOfLoops(loops);
},

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);
Expand All @@ -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;
}
}
}
},
};
1 change: 1 addition & 0 deletions ios/RNSoundPlayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ @implementation RNSoundPlayer
};
resolve(data);
}
resolve(nil);
}

- (void) audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
Expand Down
50 changes: 28 additions & 22 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": "[email protected]"
},
"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": "[email protected]"
},
"prettier": {
"trailingComma": "es5",
"tabWidth": 2,
"semi": true,
"singleQuote": false
},
"license": "MIT"
}

0 comments on commit 4fd0430

Please sign in to comment.