diff options
author | daniel oeh <daniel.oeh@gmail.com> | 2014-08-05 17:12:31 +0200 |
---|---|---|
committer | daniel oeh <daniel.oeh@gmail.com> | 2014-08-05 17:12:31 +0200 |
commit | 640cdad1feefc6186854233cfd1effee778233cb (patch) | |
tree | 5127eaada4d89a6148806de88e590633faf0492d | |
parent | df92bd98abe6d8a472a934a2cb9aa0ca2ede0784 (diff) | |
download | AntennaPod-640cdad1feefc6186854233cfd1effee778233cb.zip |
Acquire wifi lock if media file is being streamed
-rw-r--r-- | src/de/danoeh/antennapod/service/playback/PlaybackServiceMediaPlayer.java | 32 |
1 files changed, 29 insertions, 3 deletions
diff --git a/src/de/danoeh/antennapod/service/playback/PlaybackServiceMediaPlayer.java b/src/de/danoeh/antennapod/service/playback/PlaybackServiceMediaPlayer.java index dd7aaee24..49f20012d 100644 --- a/src/de/danoeh/antennapod/service/playback/PlaybackServiceMediaPlayer.java +++ b/src/de/danoeh/antennapod/service/playback/PlaybackServiceMediaPlayer.java @@ -4,6 +4,7 @@ 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; @@ -65,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); @@ -229,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) { @@ -275,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."); @@ -376,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) { @@ -593,6 +599,7 @@ public class PlaybackServiceMediaPlayer { if (mediaPlayer != null) { mediaPlayer.release(); } + releaseWifiLockIfNecessary(); } public void setVideoSurface(final SurfaceHolder surface) { @@ -753,6 +760,7 @@ public class PlaybackServiceMediaPlayer { @Override public void run() { playerLock.lock(); + releaseWifiLockIfNecessary(); if (playerStatus != PlayerStatus.INDETERMINATE) { setPlayerStatus(PlayerStatus.INDETERMINATE, media); @@ -780,6 +788,7 @@ public class PlaybackServiceMediaPlayer { @Override public void run() { playerLock.lock(); + releaseWifiLockIfNecessary(); if (playerStatus == PlayerStatus.INDETERMINATE) { setPlayerStatus(PlayerStatus.STOPPED, null); @@ -793,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. */ |