Skip to content

Commit

Permalink
Merge pull request #135 from sgilroy/android-remote-complete
Browse files Browse the repository at this point in the history
Fix "complete" event not dispatched on Android
  • Loading branch information
riderx authored Oct 29, 2024
2 parents f4a74b6 + 2363425 commit cd3afdf
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ Load an audio file
### isPreloaded(...)

```typescript
isPreloaded(options: PreloadOptions) => Promise<boolean>
isPreloaded(options: PreloadOptions) => Promise<{ found: boolean; }>
```

Check if an audio file is preloaded
Expand All @@ -261,7 +261,7 @@ Check if an audio file is preloaded
| ------------- | --------------------------------------------------------- |
| **`options`** | <code><a href="#preloadoptions">PreloadOptions</a></code> |

**Returns:** <code>Promise&lt;boolean&gt;</code>
**Returns:** <code>Promise&lt;{ found: boolean; }&gt;</code>

**Since:** 6.1.0

Expand Down
11 changes: 11 additions & 0 deletions android/src/main/java/ee/forgr/audio/AudioAsset.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class AudioAsset {
private int playIndex = 0;
private final String assetId;
protected final NativeAudio owner;
protected AudioCompletionListener completionListener;

AudioAsset(
NativeAudio owner,
Expand Down Expand Up @@ -167,4 +168,14 @@ public boolean isPlaying() throws Exception {

return audioList.get(playIndex).isPlaying();
}

public void setCompletionListener(AudioCompletionListener listener) {
this.completionListener = listener;
}

protected void notifyCompletion() {
if (completionListener != null) {
completionListener.onCompletion(this.assetId);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package ee.forgr.audio;

public interface AudioCompletionListener {
void onCompletion(String assetId);
}
2 changes: 1 addition & 1 deletion android/src/main/java/ee/forgr/audio/AudioDispatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public void onCompletion(MediaPlayer mp) {
this.stop();

if (this.owner != null) {
this.owner.dispatchComplete();
this.owner.notifyCompletion();
}
}
} catch (Exception ex) {
Expand Down
2 changes: 2 additions & 0 deletions android/src/main/java/ee/forgr/audio/NativeAudio.java
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,7 @@ private void preloadAsset(PluginCall call) {
audioChannelNum,
volume
);
remoteAudioAsset.setCompletionListener(this::dispatchComplete);
audioAssetList.put(audioId, remoteAudioAsset);
} else if (
uri.getScheme() != null && uri.getScheme().equals("file")
Expand Down Expand Up @@ -535,6 +536,7 @@ private void preloadAsset(PluginCall call) {
audioChannelNum,
volume
);
asset.setCompletionListener(this::dispatchComplete);
audioAssetList.put(audioId, asset);
} else {
throw new IllegalArgumentException(
Expand Down
3 changes: 3 additions & 0 deletions android/src/main/java/ee/forgr/audio/RemoteAudioAsset.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ private void initializeMediaPlayer(MediaPlayer mediaPlayer) {
isPrepared = true;
Log.d(TAG, "MediaPlayer prepared for " + uri.toString());
});
mediaPlayer.setOnCompletionListener(mp -> {
notifyCompletion();
});
mediaPlayer.setOnErrorListener((mp, what, extra) -> {
Log.e(TAG, "MediaPlayer error: " + what + ", " + extra);
return false;
Expand Down

0 comments on commit cd3afdf

Please sign in to comment.