Fixed bug in the playbackService related to audiofocus

This commit is contained in:
daniel oeh 2012-07-04 20:13:28 +02:00
parent 7751d7fcc2
commit 68e5d90b9c
4 changed files with 36 additions and 25 deletions

View File

@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<classpath> <classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="gen"/>
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/> <classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
<classpathentry kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/> <classpathentry kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="gen"/>
<classpathentry kind="output" path="bin/classes"/> <classpathentry kind="output" path="bin/classes"/>
</classpath> </classpath>

View File

@ -8,6 +8,6 @@
# project structure. # project structure.
# Project target. # Project target.
target=android-14 target=android-15
android.library.reference.1=../actionbarsherlock/library/ android.library.reference.1=../actionbarsherlock/library/
android.library.reference.2=../Android-ViewPagerIndicator/library android.library.reference.2=../Android-ViewPagerIndicator/library

View File

@ -413,7 +413,7 @@ public class MediaplayerActivity extends SherlockFragmentActivity implements
@Override @Override
public void onClick(View v) { public void onClick(View v) {
if (status == PlayerStatus.PLAYING) { if (status == PlayerStatus.PLAYING) {
playbackService.pause(); playbackService.pause(true);
} else if (status == PlayerStatus.PAUSED } else if (status == PlayerStatus.PAUSED
|| status == PlayerStatus.PREPARED) { || status == PlayerStatus.PREPARED) {
playbackService.play(); playbackService.play();

View File

@ -67,7 +67,6 @@ public class PlaybackService extends Service {
public static final int NOTIFICATION_TYPE_BUFFER_UPDATE = 2; public static final int NOTIFICATION_TYPE_BUFFER_UPDATE = 2;
public static final int NOTIFICATION_TYPE_RELOAD = 3; public static final int NOTIFICATION_TYPE_RELOAD = 3;
/** Is true if service is running. */ /** Is true if service is running. */
public static boolean isRunning = false; public static boolean isRunning = false;
@ -91,6 +90,9 @@ public class PlaybackService extends Service {
private PlayerStatus statusBeforeSeek; private PlayerStatus statusBeforeSeek;
/** True if mediaplayer was paused because it lost audio focus temporarily */
private boolean pausedBecauseOfTransientAudiofocusLoss;
private final IBinder mBinder = new LocalBinder(); private final IBinder mBinder = new LocalBinder();
public class LocalBinder extends Binder { public class LocalBinder extends Binder {
@ -103,6 +105,7 @@ public class PlaybackService extends Service {
public void onCreate() { public void onCreate() {
super.onCreate(); super.onCreate();
isRunning = true; isRunning = true;
pausedBecauseOfTransientAudiofocusLoss = false;
status = PlayerStatus.STOPPED; status = PlayerStatus.STOPPED;
Log.d(TAG, "Service created."); Log.d(TAG, "Service created.");
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
@ -141,21 +144,25 @@ public class PlaybackService extends Service {
switch (focusChange) { switch (focusChange) {
case AudioManager.AUDIOFOCUS_LOSS: case AudioManager.AUDIOFOCUS_LOSS:
Log.d(TAG, "Lost audio focus"); Log.d(TAG, "Lost audio focus");
pause(); pause(true);
stopSelf(); stopSelf();
break; break;
case AudioManager.AUDIOFOCUS_GAIN: case AudioManager.AUDIOFOCUS_GAIN:
Log.d(TAG, "Gained audio focus"); Log.d(TAG, "Gained audio focus");
play(); if (pausedBecauseOfTransientAudiofocusLoss) {
play();
}
break; break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK: case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
Log.d(TAG, "Lost audio focus temporarily. Ducking..."); Log.d(TAG, "Lost audio focus temporarily. Ducking...");
audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_LOWER, 0); AudioManager.ADJUST_LOWER, 0);
pausedBecauseOfTransientAudiofocusLoss = true;
break; break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT: case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
Log.d(TAG, "Lost audio focus temporarily. Pausing..."); Log.d(TAG, "Lost audio focus temporarily. Pausing...");
pause(); pause(false);
pausedBecauseOfTransientAudiofocusLoss = true;
} }
} }
}; };
@ -182,7 +189,7 @@ public class PlaybackService extends Service {
// check if already playing and playbackType is the same // check if already playing and playbackType is the same
} else if (media == null || mediaId != media.getId() } else if (media == null || mediaId != media.getId()
|| playbackType != shouldStream) { || playbackType != shouldStream) {
pause(); pause(true);
player.reset(); player.reset();
if (media == null || mediaId != media.getId()) { if (media == null || mediaId != media.getId()) {
feed = manager.getFeed(feedId); feed = manager.getFeed(feedId);
@ -218,7 +225,7 @@ public class PlaybackService extends Service {
switch (keycode) { switch (keycode) {
case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE: case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
if (status == PlayerStatus.PLAYING) { if (status == PlayerStatus.PLAYING) {
pause(); pause(true);
} else if (status == PlayerStatus.PAUSED) { } else if (status == PlayerStatus.PAUSED) {
play(); play();
} }
@ -230,7 +237,7 @@ public class PlaybackService extends Service {
break; break;
case KeyEvent.KEYCODE_MEDIA_PAUSE: case KeyEvent.KEYCODE_MEDIA_PAUSE:
if (status == PlayerStatus.PLAYING) { if (status == PlayerStatus.PLAYING) {
pause(); pause(true);
} }
break; break;
} }
@ -360,11 +367,12 @@ public class PlaybackService extends Service {
private MediaPlayer.OnErrorListener onErrorListener = new MediaPlayer.OnErrorListener() { private MediaPlayer.OnErrorListener onErrorListener = new MediaPlayer.OnErrorListener() {
private static final String TAG = "PlaybackService.onErrorListener"; private static final String TAG = "PlaybackService.onErrorListener";
@Override @Override
public boolean onError(MediaPlayer mp, int what, int extra) { public boolean onError(MediaPlayer mp, int what, int extra) {
Log.w(TAG, "An error has occured: " + what); Log.w(TAG, "An error has occured: " + what);
if (mp.isPlaying()) { if (mp.isPlaying()) {
pause(); pause(true);
} }
sendNotificationBroadcast(NOTIFICATION_TYPE_ERROR, what); sendNotificationBroadcast(NOTIFICATION_TYPE_ERROR, what);
stopSelf(); stopSelf();
@ -399,7 +407,6 @@ public class PlaybackService extends Service {
sendNotificationBroadcast(NOTIFICATION_TYPE_RELOAD, 0); sendNotificationBroadcast(NOTIFICATION_TYPE_RELOAD, 0);
} }
} }
}; };
@ -412,10 +419,13 @@ public class PlaybackService extends Service {
} }
}; };
public void pause() { public void pause(boolean abandonFocus) {
if (player.isPlaying()) { if (player.isPlaying()) {
Log.d(TAG, "Pausing playback."); Log.d(TAG, "Pausing playback.");
player.pause(); player.pause();
if (abandonFocus) {
audioManager.abandonAudioFocus(audioFocusChangeListener);
}
if (positionSaver != null) { if (positionSaver != null) {
positionSaver.cancel(true); positionSaver.cancel(true);
} }
@ -427,7 +437,7 @@ public class PlaybackService extends Service {
/** Pauses playback and destroys service. Recommended for video playback. */ /** Pauses playback and destroys service. Recommended for video playback. */
public void stop() { public void stop() {
pause(); pause(true);
stopSelf(); stopSelf();
} }
@ -453,6 +463,7 @@ public class PlaybackService extends Service {
setStatus(PlayerStatus.PLAYING); setStatus(PlayerStatus.PLAYING);
setupPositionSaver(); setupPositionSaver();
setupNotification(); setupNotification();
pausedBecauseOfTransientAudiofocusLoss = false;
} else { } else {
Log.d(TAG, "Failed to request Audiofocus"); Log.d(TAG, "Failed to request Audiofocus");
} }