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

TIMOB-13150-Added support for async operation #4593

Merged
merged 1 commit into from
Aug 22, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
125 changes: 96 additions & 29 deletions android/modules/media/src/java/ti/modules/titanium/media/TiSound.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

public class TiSound
implements MediaPlayer.OnCompletionListener, MediaPlayer.OnErrorListener, KrollProxyListener,
MediaPlayer.OnBufferingUpdateListener, MediaPlayer.OnInfoListener
MediaPlayer.OnBufferingUpdateListener, MediaPlayer.OnInfoListener, MediaPlayer.OnPreparedListener
{
private static final String TAG = "TiSound";

Expand Down Expand Up @@ -71,6 +71,11 @@ public class TiSound
protected boolean playOnResume;
protected boolean remote;
protected Timer progressTimer;

private boolean pausePending = false;
private boolean stopPending = false;
private boolean playPending = false;
private boolean prepareRequired = false;

public TiSound(KrollProxy proxy)
{
Expand All @@ -79,7 +84,7 @@ public TiSound(KrollProxy proxy)
this.remote = false;
}

protected void initialize()
protected void initializeAndPlay()
throws IOException
{
try {
Expand Down Expand Up @@ -140,13 +145,20 @@ protected void initialize()
mp.setOnInfoListener(this);
mp.setOnBufferingUpdateListener(this);

mp.prepare(); // Probably need to allow for Async
setState(STATE_INITIALIZED);

setVolume(volume);
if (proxy.hasProperty(TiC.PROPERTY_TIME)) {
setTime(TiConvert.toInt(proxy.getProperty(TiC.PROPERTY_TIME)));
if (remote) { // try async
mp.setOnPreparedListener(this);
mp.prepareAsync();
playPending = true;
} else {
mp.prepare();
setState(STATE_INITIALIZED);
setVolume(volume);
if (proxy.hasProperty(TiC.PROPERTY_TIME)) {
setTime(TiConvert.toInt(proxy.getProperty(TiC.PROPERTY_TIME)));
}
startPlaying();
}

} catch (Throwable t) {
Log.w(TAG, "Issue while initializing : " , t);
release();
Expand Down Expand Up @@ -183,6 +195,8 @@ public void pause()
mp.pause();
paused = true;
setState(STATE_PAUSED);
} else if (playPending) {
pausePending = true;
}
}
} catch (Throwable t) {
Expand All @@ -195,26 +209,48 @@ public void play()
try {
if (mp == null) {
setState(STATE_STARTING);
initialize();
}

if (mp != null) {
if (!isPlaying()) {
Log.d(TAG, "audio is not playing, starting.", Log.DEBUG_MODE);
Log.d(TAG, "Play: Volume set to " + volume, Log.DEBUG_MODE);
mp.start();
setState(STATE_PLAYING);
paused = false;
startProgressTimer();
initializeAndPlay();
} else {
if (prepareRequired) {
prepareAndPlay();
} else {
startPlaying();
}
setState(STATE_PLAYING);
}
} catch (Throwable t) {
Log.w(TAG, "Issue while playing : " , t);
reset();
}
}

private void prepareAndPlay() throws IllegalStateException, IOException
{
prepareRequired = false;
if (remote) {
playPending = true;
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener()
{
@Override
public void onPrepared(MediaPlayer mp)
{
mp.setOnPreparedListener(null);
mp.seekTo(0);
if (!stopPending && !pausePending) {
startPlaying();
}
playPending = false;
pausePending = false;
stopPending = false;
}
});
mp.prepareAsync();
} else {
mp.prepare();
mp.seekTo(0);
startPlaying();
}
}

public void reset()
{
try {
Expand Down Expand Up @@ -388,14 +424,9 @@ public void stop()
mp.stop();
setState(STATE_STOPPED);
stopProgressTimer();
try {
mp.prepare();
mp.seekTo(0);
} catch (IOException e) {
Log.e(TAG, "Error while preparing audio after stop(). Ignoring.", Log.DEBUG_MODE);
} catch (IllegalStateException e) {
Log.w(TAG, "Error while preparing audio after stop(). Ignoring.", Log.DEBUG_MODE);
}
prepareRequired = true;
} else if (playPending) {
stopPending = true;
}

if (isPaused()) {
Expand Down Expand Up @@ -559,5 +590,41 @@ public void propertiesChanged(List<KrollPropertyChange> changes, KrollProxy prox
for (KrollPropertyChange change : changes) {
propertyChanged(change.getName(), change.getOldValue(), change.getNewValue(), proxy);
}
}
}

private void startPlaying()
{
if (mp != null) {
if (!isPlaying()) {
Log.d(TAG, "audio is not playing, starting.", Log.DEBUG_MODE);
Log.d(TAG, "Play: Volume set to " + volume, Log.DEBUG_MODE);
mp.start();
paused = false;
startProgressTimer();
}
setState(STATE_PLAYING);
}
}

@Override
public void onPrepared(MediaPlayer mp)
{
mp.setOnPreparedListener(null);
setState(STATE_INITIALIZED);
setVolume(volume);
if (proxy.hasProperty(TiC.PROPERTY_TIME)) {
setTime(TiConvert.toInt(proxy.getProperty(TiC.PROPERTY_TIME)));
}
if (!pausePending && !stopPending) {
try {
startPlaying();
} catch (Throwable t) {
Log.w(TAG, "Issue while playing : ", t);
reset();
}
}
playPending = false;
pausePending = false;
stopPending = false;
}
}