Skip to content
Open
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
34 changes: 33 additions & 1 deletion src/android/AudioPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Licensed to the Apache Software Foundation (ASF) under one
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnInfoListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.media.MediaRecorder;
import android.os.Environment;
Expand All @@ -48,7 +49,7 @@ Licensed to the Apache Software Foundation (ASF) under one
* android_asset: file name must start with /android_asset/sound.mp3
* sdcard: file name is just sound.mp3
*/
public class AudioPlayer implements OnCompletionListener, OnPreparedListener, OnErrorListener {
public class AudioPlayer implements OnCompletionListener, OnPreparedListener, OnErrorListener, OnInfoListener {

// AudioPlayer modes
public enum MODE { NONE, PLAY, RECORD };
Expand All @@ -62,12 +63,18 @@ public enum STATE { MEDIA_NONE,
MEDIA_LOADING
};

// AudiorPlayer informations
public enum INFORMATION { MEDIA_BUFFERING_START,
MEDIA_BUFFERING_END
};

private static final String LOG_TAG = "AudioPlayer";

// AudioPlayer message ids
private static int MEDIA_STATE = 1;
private static int MEDIA_DURATION = 2;
private static int MEDIA_POSITION = 3;
private static int MEDIA_INFO = 4;
private static int MEDIA_ERROR = 9;

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

@Override
public boolean onInfo(MediaPlayer player, int what, int extra) {
LOG.d(LOG_TAG, "AudioPlayer.onInfo(" + what + ", " + extra + ")");
switch (what) {
case MediaPlayer.MEDIA_INFO_BUFFERING_START:
sendInfo(INFORMATION.MEDIA_BUFFERING_START);
break;
case MediaPlayer.MEDIA_INFO_BUFFERING_END:
sendInfo(INFORMATION.MEDIA_BUFFERING_END);
break;
}
return false;
}

/**
* Set the state and send it to JavaScript.
*
Expand All @@ -543,6 +566,15 @@ private void setState(STATE state) {
this.state = state;
}

/**
* Send information to Javascript.
*
* @param info
*/
private void sendInfo(INFORMATION info) {
sendStatusChange(MEDIA_INFO, null, (float)info.ordinal());
}

/**
* Set the mode and send it to JavaScript.
*
Expand Down
15 changes: 14 additions & 1 deletion www/Media.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,18 @@ var mediaObjects = {};
* errorCallback(int errorCode) - OPTIONAL
* @param statusCallback The callback to be called when media status has changed.
* statusCallback(int statusCode) - OPTIONAL
* @param infoCallback The callback to be called when playback information is received.
* statusCallback(int infoCode) - OPTIONAL
*/
var Media = function(src, successCallback, errorCallback, statusCallback) {
var Media = function(src, successCallback, errorCallback, statusCallback, infoCallback) {
argscheck.checkArgs('sFFF', 'Media', arguments);
this.id = utils.createUUID();
mediaObjects[this.id] = this;
this.src = src;
this.successCallback = successCallback;
this.errorCallback = errorCallback;
this.statusCallback = statusCallback;
this.infoCallback = infoCallback;
this._duration = -1;
this._position = -1;
exec(null, this.errorCallback, "Media", "create", [this.id, this.src]);
Expand All @@ -54,6 +57,7 @@ var Media = function(src, successCallback, errorCallback, statusCallback) {
Media.MEDIA_STATE = 1;
Media.MEDIA_DURATION = 2;
Media.MEDIA_POSITION = 3;
Media.MEDIA_INFO = 4;
Media.MEDIA_ERROR = 9;

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

// Media informations
Media.MEDIA_BUFFERING_START = 0;
Media.MEDIA_BUFFERING_END = 1;

// "static" function to return existing objs.
Media.get = function(id) {
return mediaObjects[id];
Expand Down Expand Up @@ -221,6 +229,11 @@ Media.onStatus = function(id, msgType, value) {
case Media.MEDIA_POSITION :
media._position = Number(value);
break;
case Media.MEDIA_INFO:
if (media.infoCallback) {
media.infoCallback(value);
}
break;
default :
if (console.error) {
console.error("Unhandled Media.onStatus :: " + msgType);
Expand Down