diff --git a/src/de/danoeh/antennapod/service/playback/PlaybackServiceMediaPlayer.java b/src/de/danoeh/antennapod/service/playback/PlaybackServiceMediaPlayer.java index 3b552c973..49f20012d 100644 --- a/src/de/danoeh/antennapod/service/playback/PlaybackServiceMediaPlayer.java +++ b/src/de/danoeh/antennapod/service/playback/PlaybackServiceMediaPlayer.java @@ -4,6 +4,8 @@ import android.content.ComponentName; import android.content.Context; import android.media.AudioManager; import android.media.RemoteControlClient; +import android.net.wifi.WifiManager; +import android.os.PowerManager; import android.telephony.TelephonyManager; import android.util.Log; import android.util.Pair; @@ -64,6 +66,11 @@ public class PlaybackServiceMediaPlayer { private final ThreadPoolExecutor executor; + /** + * A wifi-lock that is acquired if the media file is being streamed. + */ + private WifiManager.WifiLock wifiLock; + public PlaybackServiceMediaPlayer(Context context, PSMPCallback callback) { Validate.notNull(context); Validate.notNull(callback); @@ -228,7 +235,7 @@ public class PlaybackServiceMediaPlayer { audioFocusChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); if (focusGained == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) { - + acquireWifiLockIfNecessary(); setSpeed(Float.parseFloat(UserPreferences.getPlaybackSpeed())); mediaPlayer.start(); if (playerStatus == PlayerStatus.PREPARED && media.getPosition() > 0) { @@ -274,7 +281,7 @@ public class PlaybackServiceMediaPlayer { @Override public void run() { playerLock.lock(); - + releaseWifiLockIfNecessary(); if (playerStatus == PlayerStatus.PLAYING) { if (BuildConfig.DEBUG) Log.d(TAG, "Pausing playback."); @@ -375,7 +382,7 @@ public class PlaybackServiceMediaPlayer { @Override public void run() { playerLock.lock(); - + releaseWifiLockIfNecessary(); if (media != null) { playMediaObject(media, true, stream, startWhenPrepared.get(), false); } else if (mediaPlayer != null) { @@ -592,6 +599,7 @@ public class PlaybackServiceMediaPlayer { if (mediaPlayer != null) { mediaPlayer.release(); } + releaseWifiLockIfNecessary(); } public void setVideoSurface(final SurfaceHolder surface) { @@ -684,6 +692,7 @@ public class PlaybackServiceMediaPlayer { mediaPlayer = new AudioPlayer(context); } mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); + mediaPlayer.setWakeMode(context, PowerManager.PARTIAL_WAKE_LOCK); return setMediaPlayerListeners(mediaPlayer); } @@ -751,6 +760,7 @@ public class PlaybackServiceMediaPlayer { @Override public void run() { playerLock.lock(); + releaseWifiLockIfNecessary(); if (playerStatus != PlayerStatus.INDETERMINATE) { setPlayerStatus(PlayerStatus.INDETERMINATE, media); @@ -778,6 +788,7 @@ public class PlaybackServiceMediaPlayer { @Override public void run() { playerLock.lock(); + releaseWifiLockIfNecessary(); if (playerStatus == PlayerStatus.INDETERMINATE) { setPlayerStatus(PlayerStatus.STOPPED, null); @@ -791,6 +802,23 @@ public class PlaybackServiceMediaPlayer { }); } + private synchronized void acquireWifiLockIfNecessary() { + if (stream) { + if (wifiLock == null) { + wifiLock = ((WifiManager) context.getSystemService(Context.WIFI_SERVICE)) + .createWifiLock(WifiManager.WIFI_MODE_FULL, TAG); + wifiLock.setReferenceCounted(false); + } + wifiLock.acquire(); + } + } + + private synchronized void releaseWifiLockIfNecessary() { + if (wifiLock != null && wifiLock.isHeld()) { + wifiLock.release(); + } + } + /** * Holds information about a PSMP object. */ diff --git a/src/de/danoeh/antennapod/util/playback/IPlayer.java b/src/de/danoeh/antennapod/util/playback/IPlayer.java index 99f53fb52..2d4551b13 100644 --- a/src/de/danoeh/antennapod/util/playback/IPlayer.java +++ b/src/de/danoeh/antennapod/util/playback/IPlayer.java @@ -1,5 +1,6 @@ package de.danoeh.antennapod.util.playback; +import android.content.Context; import android.view.SurfaceHolder; import java.io.IOException; @@ -63,4 +64,6 @@ public interface IPlayer { void stop(); public void setVideoScalingMode(int mode); + + public void setWakeMode(Context context, int mode); }