Skip to content

Commit bbc8a68

Browse files
author
Michael Goffioul
committed
CB-13633: (android) Propagate buffering start/stop events on Android.
1 parent 74772c3 commit bbc8a68

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

src/android/AudioPlayer.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Licensed to the Apache Software Foundation (ASF) under one
2222
import android.media.MediaPlayer;
2323
import android.media.MediaPlayer.OnCompletionListener;
2424
import android.media.MediaPlayer.OnErrorListener;
25+
import android.media.MediaPlayer.OnInfoListener;
2526
import android.media.MediaPlayer.OnPreparedListener;
2627
import android.media.MediaRecorder;
2728
import android.os.Environment;
@@ -48,7 +49,7 @@ Licensed to the Apache Software Foundation (ASF) under one
4849
* android_asset: file name must start with /android_asset/sound.mp3
4950
* sdcard: file name is just sound.mp3
5051
*/
51-
public class AudioPlayer implements OnCompletionListener, OnPreparedListener, OnErrorListener {
52+
public class AudioPlayer implements OnCompletionListener, OnPreparedListener, OnErrorListener, OnInfoListener {
5253

5354
// AudioPlayer modes
5455
public enum MODE { NONE, PLAY, RECORD };
@@ -62,12 +63,18 @@ public enum STATE { MEDIA_NONE,
6263
MEDIA_LOADING
6364
};
6465

66+
// AudiorPlayer informations
67+
public enum INFORMATION { MEDIA_BUFFERING_START,
68+
MEDIA_BUFFERING_END
69+
};
70+
6571
private static final String LOG_TAG = "AudioPlayer";
6672

6773
// AudioPlayer message ids
6874
private static int MEDIA_STATE = 1;
6975
private static int MEDIA_DURATION = 2;
7076
private static int MEDIA_POSITION = 3;
77+
private static int MEDIA_INFO = 4;
7178
private static int MEDIA_ERROR = 9;
7279

7380
// Media error codes
@@ -482,6 +489,8 @@ public float getDuration(String file) {
482489
public void onPrepared(MediaPlayer player) {
483490
// Listen for playback completion
484491
this.player.setOnCompletionListener(this);
492+
// Listen for info aboutplayback
493+
this.player.setOnInfoListener(this);
485494
// seek to any location received while not prepared
486495
this.seekToPlaying(this.seekOnPrepared);
487496
// If start playing after prepared
@@ -531,6 +540,20 @@ public boolean onError(MediaPlayer player, int arg1, int arg2) {
531540
return false;
532541
}
533542

543+
@Override
544+
public boolean onInfo(MediaPlayer player, int what, int extra) {
545+
LOG.d(LOG_TAG, "AudioPlayer.onInfo(" + what + ", " + extra + ")");
546+
switch (what) {
547+
case MediaPlayer.MEDIA_INFO_BUFFERING_START:
548+
sendInfo(INFORMATION.MEDIA_BUFFERING_START);
549+
break;
550+
case MediaPlayer.MEDIA_INFO_BUFFERING_END:
551+
sendInfo(INFORMATION.MEDIA_BUFFERING_END);
552+
break;
553+
}
554+
return false;
555+
}
556+
534557
/**
535558
* Set the state and send it to JavaScript.
536559
*
@@ -543,6 +566,15 @@ private void setState(STATE state) {
543566
this.state = state;
544567
}
545568

569+
/**
570+
* Send information to Javascript.
571+
*
572+
* @param info
573+
*/
574+
private void sendInfo(INFORMATION info) {
575+
sendStatusChange(MEDIA_INFO, null, (float)info.ordinal());
576+
}
577+
546578
/**
547579
* Set the mode and send it to JavaScript.
548580
*

www/Media.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,18 @@ var mediaObjects = {};
3636
* errorCallback(int errorCode) - OPTIONAL
3737
* @param statusCallback The callback to be called when media status has changed.
3838
* statusCallback(int statusCode) - OPTIONAL
39+
* @param infoCallback The callback to be called when playback information is received.
40+
* statusCallback(int infoCode) - OPTIONAL
3941
*/
40-
var Media = function(src, successCallback, errorCallback, statusCallback) {
42+
var Media = function(src, successCallback, errorCallback, statusCallback, infoCallback) {
4143
argscheck.checkArgs('sFFF', 'Media', arguments);
4244
this.id = utils.createUUID();
4345
mediaObjects[this.id] = this;
4446
this.src = src;
4547
this.successCallback = successCallback;
4648
this.errorCallback = errorCallback;
4749
this.statusCallback = statusCallback;
50+
this.infoCallback = infoCallback;
4851
this._duration = -1;
4952
this._position = -1;
5053
exec(null, this.errorCallback, "Media", "create", [this.id, this.src]);
@@ -54,6 +57,7 @@ var Media = function(src, successCallback, errorCallback, statusCallback) {
5457
Media.MEDIA_STATE = 1;
5558
Media.MEDIA_DURATION = 2;
5659
Media.MEDIA_POSITION = 3;
60+
Media.MEDIA_INFO = 4;
5761
Media.MEDIA_ERROR = 9;
5862

5963
// Media states
@@ -64,6 +68,10 @@ Media.MEDIA_PAUSED = 3;
6468
Media.MEDIA_STOPPED = 4;
6569
Media.MEDIA_MSG = ["None", "Starting", "Running", "Paused", "Stopped"];
6670

71+
// Media informations
72+
Media.MEDIA_BUFFERING_START = 0;
73+
Media.MEDIA_BUFFERING_END = 1;
74+
6775
// "static" function to return existing objs.
6876
Media.get = function(id) {
6977
return mediaObjects[id];
@@ -221,6 +229,11 @@ Media.onStatus = function(id, msgType, value) {
221229
case Media.MEDIA_POSITION :
222230
media._position = Number(value);
223231
break;
232+
case Media.MEDIA_INFO:
233+
if (media.infoCallback) {
234+
media.infoCallback(value);
235+
}
236+
break;
224237
default :
225238
if (console.error) {
226239
console.error("Unhandled Media.onStatus :: " + msgType);

0 commit comments

Comments
 (0)