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

feat(android): Progress report for Media plugin (CB-7175) #22

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
27 changes: 26 additions & 1 deletion src/android/AudioPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Licensed to the Apache Software Foundation (ASF) under one
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaRecorder;
import android.os.Environment;
import android.util.Log;
Expand All @@ -40,7 +41,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, OnBufferingUpdateListener {

// AudioPlayer modes
public enum MODE { NONE, PLAY, RECORD };
Expand Down Expand Up @@ -84,6 +85,8 @@ public enum STATE { MEDIA_NONE,
private boolean prepareOnly = true; // playback after file prepare flag
private int seekOnPrepared = 0; // seek to this location once media is prepared

private int buffered = 0; // Buffered percentage

/**
* Constructor.
*
Expand Down Expand Up @@ -276,6 +279,16 @@ public void onCompletion(MediaPlayer player) {
this.setState(STATE.MEDIA_STOPPED);
}

/**
* Callback to be invoked when progress has been made on buffering media source
*
* @param player The MediaPlayer that reached the end of the file
Copy link
Contributor

Choose a reason for hiding this comment

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

that reached the end of file sounds wrong... based on the callback name.

* @param percent Buffered percentage
*/
public void onBufferingUpdate(MediaPlayer player, int percent) {
this.setBuffered(percent);
}

/**
* Get current position of playback.
*
Expand Down Expand Up @@ -347,6 +360,8 @@ public float getDuration(String file) {
public void onPrepared(MediaPlayer player) {
// Listen for playback completion
this.player.setOnCompletionListener(this);
// Listen for download progress
this.player.setOnBufferingUpdateListener(this);
// seek to any location received while not prepared
this.seekToPlaying(this.seekOnPrepared);
// If start playing after prepared
Expand Down Expand Up @@ -407,6 +422,16 @@ private void setState(STATE state) {
this.state = state;
}

/**
* Set the buffered percentage
*
* @param buffered
*/
private void setBuffered(int buffered) {
this.handler.webView.sendJavascript("cordova.require('org.apache.cordova.media.Media').onBuffered('" + this.id + "', " + buffered + ");");
this.buffered = buffered;
}

/**
* Set the mode and send it to JavaScript.
*
Expand Down
21 changes: 18 additions & 3 deletions 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 bufferedCallback The callback to be called when media has progressed buffering.
* bufferedCallback(int bufferedPercentage) - OPTIONAL
*/
var Media = function(src, successCallback, errorCallback, statusCallback) {
argscheck.checkArgs('SFFF', 'Media', arguments);
var Media = function(src, successCallback, errorCallback, statusCallback, bufferedCallback) {
argscheck.checkArgs('SFFFF', 'Media', arguments);
this.id = utils.createUUID();
mediaObjects[this.id] = this;
this.src = src;
this.successCallback = successCallback;
this.errorCallback = errorCallback;
this.statusCallback = statusCallback;
this.bufferedCallback = bufferedCallback;
this._duration = -1;
this._position = -1;
exec(null, this.errorCallback, "Media", "create", [this.id, this.src]);
Expand Down Expand Up @@ -192,4 +195,16 @@ Media.onStatus = function(id, msgType, value) {

};

module.exports = Media;
Media.onBuffered = function(id, buffered) {
var media = mediaObjects[id];

if(media) {
media.bufferedCallback && media.bufferedCallback(buffered);
Copy link
Contributor

Choose a reason for hiding this comment

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

the indentation here doesn't match the indentation of the else block below.

}
else {
console.error && console.error("Received Media.onBuffered callback for unknown media :: " + id);
}

};

module.exports = Media;
Copy link
Contributor

Choose a reason for hiding this comment

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

the red null w/ red linefeed means you don't have a newline at the end of your file. please add one.

Also, please file a bug in issues.apache.org/jira and reference it in the commit message (see the contribution guide...)