Acquire wifi lock if media file is being streamed

This commit is contained in:
daniel oeh 2014-08-05 17:12:31 +02:00
parent df92bd98ab
commit 640cdad1fe
1 changed files with 29 additions and 3 deletions

View File

@ -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.
*/