mirror of
https://github.com/AntennaPod/AntennaPod.git
synced 2025-12-01 12:31:45 +00:00
Fixed bug in the playbackService related to audiofocus
This commit is contained in:
@ -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>
|
||||||
|
|||||||
@ -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
|
||||||
|
|||||||
@ -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();
|
||||||
|
|||||||
@ -57,17 +57,16 @@ public class PlaybackService extends Service {
|
|||||||
public static final String EXTRA_START_WHEN_PREPARED = "extra.de.podfetcher.service.startWhenPrepared";
|
public static final String EXTRA_START_WHEN_PREPARED = "extra.de.podfetcher.service.startWhenPrepared";
|
||||||
|
|
||||||
public static final String ACTION_PLAYER_STATUS_CHANGED = "action.de.podfetcher.service.playerStatusChanged";
|
public static final String ACTION_PLAYER_STATUS_CHANGED = "action.de.podfetcher.service.playerStatusChanged";
|
||||||
|
|
||||||
public static final String ACTION_PLAYER_NOTIFICATION = "action.de.podfetcher.service.playerNotification";
|
public static final String ACTION_PLAYER_NOTIFICATION = "action.de.podfetcher.service.playerNotification";
|
||||||
public static final String EXTRA_NOTIFICATION_CODE = "extra.de.podfetcher.service.notificationCode";
|
public static final String EXTRA_NOTIFICATION_CODE = "extra.de.podfetcher.service.notificationCode";
|
||||||
public static final String EXTRA_NOTIFICATION_TYPE = "extra.de.podfetcher.service.notificationType";
|
public static final String EXTRA_NOTIFICATION_TYPE = "extra.de.podfetcher.service.notificationType";
|
||||||
|
|
||||||
public static final int NOTIFICATION_TYPE_ERROR = 0;
|
public static final int NOTIFICATION_TYPE_ERROR = 0;
|
||||||
public static final int NOTIFICATION_TYPE_INFO = 1;
|
public static final int NOTIFICATION_TYPE_INFO = 1;
|
||||||
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;
|
||||||
}
|
}
|
||||||
@ -357,14 +364,15 @@ 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();
|
||||||
@ -384,7 +392,7 @@ public class PlaybackService extends Service {
|
|||||||
manager.removeQueueItem(PlaybackService.this, media.getItem());
|
manager.removeQueueItem(PlaybackService.this, media.getItem());
|
||||||
}
|
}
|
||||||
manager.setFeedMedia(PlaybackService.this, media);
|
manager.setFeedMedia(PlaybackService.this, media);
|
||||||
|
|
||||||
FeedItem nextItem = manager.getFirstQueueItem();
|
FeedItem nextItem = manager.getFirstQueueItem();
|
||||||
if (nextItem == null) {
|
if (nextItem == null) {
|
||||||
Log.d(TAG, "No more items in queue");
|
Log.d(TAG, "No more items in queue");
|
||||||
@ -398,24 +406,26 @@ public class PlaybackService extends Service {
|
|||||||
resetVideoSurface();
|
resetVideoSurface();
|
||||||
sendNotificationBroadcast(NOTIFICATION_TYPE_RELOAD, 0);
|
sendNotificationBroadcast(NOTIFICATION_TYPE_RELOAD, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
private MediaPlayer.OnBufferingUpdateListener onBufferingUpdateListener = new MediaPlayer.OnBufferingUpdateListener() {
|
private MediaPlayer.OnBufferingUpdateListener onBufferingUpdateListener = new MediaPlayer.OnBufferingUpdateListener() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onBufferingUpdate(MediaPlayer mp, int percent) {
|
public void onBufferingUpdate(MediaPlayer mp, int percent) {
|
||||||
sendNotificationBroadcast(NOTIFICATION_TYPE_BUFFER_UPDATE, percent);
|
sendNotificationBroadcast(NOTIFICATION_TYPE_BUFFER_UPDATE, percent);
|
||||||
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
@ -424,10 +434,10 @@ public class PlaybackService extends Service {
|
|||||||
stopForeground(true);
|
stopForeground(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 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");
|
||||||
}
|
}
|
||||||
@ -464,7 +475,7 @@ public class PlaybackService extends Service {
|
|||||||
status = newStatus;
|
status = newStatus;
|
||||||
sendBroadcast(new Intent(ACTION_PLAYER_STATUS_CHANGED));
|
sendBroadcast(new Intent(ACTION_PLAYER_STATUS_CHANGED));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void sendNotificationBroadcast(int type, int code) {
|
private void sendNotificationBroadcast(int type, int code) {
|
||||||
Intent intent = new Intent(ACTION_PLAYER_NOTIFICATION);
|
Intent intent = new Intent(ACTION_PLAYER_NOTIFICATION);
|
||||||
intent.putExtra(EXTRA_NOTIFICATION_TYPE, type);
|
intent.putExtra(EXTRA_NOTIFICATION_TYPE, type);
|
||||||
@ -547,7 +558,7 @@ public class PlaybackService extends Service {
|
|||||||
Log.d(TAG, "Player is in illegal state. Finishing now");
|
Log.d(TAG, "Player is in illegal state. Finishing now");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user