Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle looping for Android #213

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 54 additions & 21 deletions android/src/main/java/com/zmxv/RNSound/RNSoundModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ public class RNSoundModule extends ReactContextBaseJavaModule {
Map<Integer, MediaPlayer> playerPool = new HashMap<>();
ReactApplicationContext context;
final static Object NULL = null;
/**
* Number of loops for the sound.
* -1 - play inifity
* 0 - play once
* 1 - play twice etc.
*/
Integer loops = 0;

public RNSoundModule(ReactApplicationContext context) {
super(context);
Expand Down Expand Up @@ -134,22 +141,7 @@ public void play(final Integer key, final Callback callback) {
if (player.isPlaying()) {
return;
}
player.setOnCompletionListener(new OnCompletionListener() {
boolean callbackWasCalled = false;

@Override
public synchronized void onCompletion(MediaPlayer mp) {
if (!mp.isLooping()) {
if (callbackWasCalled) return;
callbackWasCalled = true;
try {
callback.invoke(true);
} catch (Exception e) {
//Catches the exception: java.lang.RuntimeException·Illegal callback invocation from native module
}
}
}
});
player.setOnCompletionListener(new OnCompletionListenerLooped(callback, this.loops));
player.setOnErrorListener(new OnErrorListener() {
boolean callbackWasCalled = false;

Expand Down Expand Up @@ -201,11 +193,16 @@ public void setVolume(final Integer key, final Float left, final Float right) {
}

@ReactMethod
public void setLooping(final Integer key, final Boolean looping) {
MediaPlayer player = this.playerPool.get(key);
if (player != null) {
player.setLooping(looping);
}
public void setNumberOfLoops(final Integer key, final Integer loops) {
if (loops == -1) {
MediaPlayer player = this.playerPool.get(key);
if (player != null) {
player.setLooping(true);
}
}
else {
this.loops = loops;
}
}

@ReactMethod
Expand Down Expand Up @@ -258,3 +255,39 @@ public Map<String, Object> getConstants() {
return constants;
}
}

class OnCompletionListenerLooped implements OnCompletionListener {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably best to be static class OnCompletionListenerLooped

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why static? Nobody gonna use it outside the RNSoundModule and static is little heavier than private. Isn't it? I'm not java master.

/**
* Number of loops. 0 - play once, 1 - play twice etc.
* Media player have set looping to true if -1 is given from react component.
*/
Integer loops = 0;
boolean callbackWasCalled = false;
Callback callback;

public OnCompletionListenerLooped(Callback callback, Integer loops) {
super();
this.callback = callback;
this.loops = loops;
}

@Override
public synchronized void onCompletion(MediaPlayer mp) {
if (!mp.isLooping()) {
if (this.loops > 0) {
mp.start();
this.loops--;
return;
}
if (callbackWasCalled)
return;
callbackWasCalled = true;
try {
callback.invoke(true);
}
catch (Exception e) {
//Catches the exception: java.lang.RuntimeException·Illegal callback invocation from native module
}
}
}
}
2 changes: 1 addition & 1 deletion sound.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ Sound.prototype.getNumberOfLoops = function() {
Sound.prototype.setNumberOfLoops = function(value) {
this._numberOfLoops = value;
if (this._loaded) {
if (IsAndroid || IsWindows) {
if (IsWindows) {
RNSound.setLooping(this._key, !!value);
} else {
RNSound.setNumberOfLoops(this._key, value);
Expand Down