[Android] Prevent the CPU to sleep while playing audio
Ludovic Fauvet
git at videolan.org
Mon Nov 5 14:17:51 CET 2012
vlc-ports/android | branch: master | Ludovic Fauvet <etix at videolan.org> | Mon Nov 5 14:12:33 2012 +0100| [6a85c2a2ec0817045b6aef8cabeef01ed63986ba] | committer: Ludovic Fauvet
Prevent the CPU to sleep while playing audio
Acquiring a wake-lock is necessary when the audio is playing while the
screen is turned off to avoid the CPU to enter in deep sleep and thus
would cause the playback to stop.
This commit adds a new permission: WAKE_LOCK
> http://git.videolan.org/gitweb.cgi/vlc-ports/android.git/?a=commit;h=6a85c2a2ec0817045b6aef8cabeef01ed63986ba
---
vlc-android/AndroidManifest.xml | 1 +
vlc-android/src/org/videolan/vlc/AudioService.java | 12 ++++++++++++
2 files changed, 13 insertions(+)
diff --git a/vlc-android/AndroidManifest.xml b/vlc-android/AndroidManifest.xml
index e1501f9..9c62c0f 100644
--- a/vlc-android/AndroidManifest.xml
+++ b/vlc-android/AndroidManifest.xml
@@ -15,6 +15,7 @@
<uses-permission android:name="android.permission.READ_LOGS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
+ <uses-permission android:name="android.permission.WAKE_LOCK"/>
<application
android:name="org.videolan.vlc.VLCApplication"
diff --git a/vlc-android/src/org/videolan/vlc/AudioService.java b/vlc-android/src/org/videolan/vlc/AudioService.java
index 1805c6f..7a5609f 100644
--- a/vlc-android/src/org/videolan/vlc/AudioService.java
+++ b/vlc-android/src/org/videolan/vlc/AudioService.java
@@ -49,6 +49,7 @@ import android.media.RemoteControlClient.MetadataEditor;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
+import android.os.PowerManager;
import android.os.RemoteException;
import android.support.v4.app.NotificationCompat;
import android.telephony.TelephonyManager;
@@ -83,6 +84,7 @@ public class AudioService extends Service {
private boolean mDetectHeadset = true;
private OnAudioFocusChangeListener audioFocusListener;
private ComponentName mRemoteControlClientReceiverComponent;
+ private PowerManager.WakeLock mWakeLock;
/**
* RemoteControlClient is for lock screen playback control.
@@ -118,6 +120,11 @@ public class AudioService extends Service {
mRemoteControlClientReceiverComponent = new ComponentName(getPackageName(),
RemoteControlClientReceiver.class.getName());
+ // Make sure the audio player will acquire a wake-lock while playing. If we don't do
+ // that, the CPU might go to sleep while the song is playing, causing playback to stop.
+ PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
+ mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
+
IntentFilter filter = new IntentFilter();
filter.setPriority(Integer.MAX_VALUE);
filter.addAction(ACTION_REMOTE_BACKWARD);
@@ -198,6 +205,7 @@ public class AudioService extends Service {
@Override
public void onDestroy() {
super.onDestroy();
+ mWakeLock.release();
unregisterReceiver(serviceReceiver);
}
@@ -327,6 +335,7 @@ public class AudioService extends Service {
Log.i(TAG, "MediaPlayerPlaying");
service.changeAudioFocus(true);
service.setRemoteControlClientPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING);
+ service.mWakeLock.acquire();
break;
case EventManager.MediaPlayerPaused:
Log.i(TAG, "MediaPlayerPaused");
@@ -334,16 +343,19 @@ public class AudioService extends Service {
// also hide notification if phone ringing
service.hideNotification();
service.setRemoteControlClientPlaybackState(RemoteControlClient.PLAYSTATE_PAUSED);
+ service.mWakeLock.release();
break;
case EventManager.MediaPlayerStopped:
Log.i(TAG, "MediaPlayerStopped");
service.executeUpdate();
service.setRemoteControlClientPlaybackState(RemoteControlClient.PLAYSTATE_STOPPED);
+ service.mWakeLock.release();
break;
case EventManager.MediaPlayerEndReached:
Log.i(TAG, "MediaPlayerEndReached");
service.executeUpdate();
service.next();
+ service.mWakeLock.release();
break;
case EventManager.MediaPlayerVout:
if(msg.getData().getInt("data") > 0) {
More information about the Android
mailing list