[Android] PlaybackService: enable/disable hdmi from service

Thomas Guillem git at videolan.org
Wed Jul 8 11:08:31 CEST 2015


vlc-ports/android | branch: master | Thomas Guillem <thomas at gllm.fr> | Wed Jul  8 10:40:22 2015 +0200| [39306b8808053e609c148999a053ff4d90a494eb] | committer: Thomas Guillem

PlaybackService: enable/disable hdmi from service

> http://git.videolan.org/gitweb.cgi/vlc-ports/android.git/?a=commit;h=39306b8808053e609c148999a053ff4d90a494eb
---

 .../src/org/videolan/vlc/PlaybackService.java      |   38 ++++++++++++++++++--
 .../vlc/gui/video/VideoPlayerActivity.java         |   32 -----------------
 .../src/org/videolan/vlc/util/VLCOptions.java      |   15 --------
 3 files changed, 36 insertions(+), 49 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/PlaybackService.java b/vlc-android/src/org/videolan/vlc/PlaybackService.java
index 5fb71a0..4a6184f 100644
--- a/vlc-android/src/org/videolan/vlc/PlaybackService.java
+++ b/vlc-android/src/org/videolan/vlc/PlaybackService.java
@@ -126,6 +126,8 @@ public class PlaybackService extends Service implements IVLCVout.Callback {
     private final IBinder mBinder = new LocalBinder();
     private MediaWrapperList mMediaList = new MediaWrapperList();
     private MediaPlayer mMediaPlayer;
+    private boolean mIsAudioTrack = false;
+    private boolean mHasHdmiAudio = false;
 
     final private ArrayList<Callback> mCallbacks = new ArrayList<Callback>();
     private boolean mDetectHeadset = true;
@@ -169,7 +171,13 @@ public class PlaybackService extends Service implements IVLCVout.Callback {
     private MediaPlayer newMediaPlayer() {
         final SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
         final MediaPlayer mp = new MediaPlayer(LibVLC());
-        mp.setAudioOutput(VLCOptions.getAout(pref));
+        final String aout = VLCOptions.getAout(pref);
+        if (mp.setAudioOutput(aout) && aout.equals("android_audiotrack")) {
+            mIsAudioTrack = true;
+            if (mHasHdmiAudio)
+                mp.setAudioOutputDevice("hdmi");
+        } else
+            mIsAudioTrack = false;
         return mp;
     }
 
@@ -229,6 +237,7 @@ public class PlaybackService extends Service implements IVLCVout.Callback {
             filter.addAction(VLCApplication.CALL_ENDED_INTENT);
         }
         registerReceiver(mReceiver, filter);
+        registerV21();
 
         final SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
         boolean stealRemoteControl = pref.getBoolean("enable_steal_remote_control", false);
@@ -300,6 +309,8 @@ public class PlaybackService extends Service implements IVLCVout.Callback {
         if (mWakeLock.isHeld())
             mWakeLock.release();
         unregisterReceiver(mReceiver);
+        if (mReceiverV21 != null)
+            unregisterReceiver(mReceiverV21);
         if (mRemoteControlClientReceiver != null) {
             unregisterReceiver(mRemoteControlClientReceiver);
             mRemoteControlClientReceiver = null;
@@ -432,6 +443,29 @@ public class PlaybackService extends Service implements IVLCVout.Callback {
             changeAudioFocusFroyoOrLater(acquire);
     }
 
+
+    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
+    private void registerV21() {
+        final IntentFilter intentFilter = new IntentFilter(AudioManager.ACTION_HDMI_AUDIO_PLUG);
+        registerReceiver(mReceiverV21, intentFilter);
+    }
+
+    private final BroadcastReceiver mReceiverV21 = AndroidUtil.isLolliPopOrLater() ? new BroadcastReceiver()
+    {
+        @TargetApi(Build.VERSION_CODES.LOLLIPOP)
+        @Override
+        public void onReceive(Context context, Intent intent) {
+            final String action = intent.getAction();
+            if (action == null)
+                return;
+            if (action.equalsIgnoreCase(AudioManager.ACTION_HDMI_AUDIO_PLUG)) {
+                mHasHdmiAudio = intent.getIntExtra(AudioManager.EXTRA_AUDIO_PLUG_STATE, 0) == 1;
+                if (mMediaPlayer != null && mIsAudioTrack)
+                    mMediaPlayer.setAudioOutputDevice(mHasHdmiAudio ? "hdmi" : "stereo");
+            }
+        }
+    } : null;
+
     private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
         @Override
         public void onReceive(Context context, Intent intent) {
@@ -507,7 +541,7 @@ public class PlaybackService extends Service implements IVLCVout.Callback {
             /*
              * headset plug events
              */
-            if (mDetectHeadset) {
+            if (mDetectHeadset && !mHasHdmiAudio) {
                 if (action.equalsIgnoreCase(AudioManager.ACTION_AUDIO_BECOMING_NOISY)) {
                     Log.i(TAG, "Headset Removed.");
                     if (mMediaPlayer.isPlaying() && hasCurrentMedia())
diff --git a/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java b/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
index 6951822..5530609 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
@@ -305,8 +305,6 @@ public class VideoPlayerActivity extends AppCompatActivity implements IVLCVout.C
     private OnLayoutChangeListener mOnLayoutChangeListener;
     private AlertDialog mAlertDialog;
 
-    private boolean mHasHdmiAudio = false;
-
     private static LibVLC LibVLC() {
         return VLCInstance.get();
     }
@@ -461,8 +459,6 @@ public class VideoPlayerActivity extends AppCompatActivity implements IVLCVout.C
             filter.addAction(Intent.ACTION_BATTERY_CHANGED);
         filter.addAction(VLCApplication.SLEEP_INTENT);
         registerReceiver(mReceiver, filter);
-        if (mReceiverV21 != null)
-            registerV21();
 
         this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
 
@@ -619,8 +615,6 @@ public class VideoPlayerActivity extends AppCompatActivity implements IVLCVout.C
         super.onDestroy();
         if (mReceiver != null)
             unregisterReceiver(mReceiver);
-        if (mReceiverV21 != null)
-            unregisterReceiver(mReceiverV21);
 
         mAudioManager = null;
     }
@@ -861,27 +855,6 @@ public class VideoPlayerActivity extends AppCompatActivity implements IVLCVout.C
         exit(RESULT_OK);
     }
 
-    @TargetApi(21)
-    private void registerV21() {
-        final IntentFilter intentFilter = new IntentFilter(AudioManager.ACTION_HDMI_AUDIO_PLUG);
-        registerReceiver(mReceiverV21, intentFilter);
-    }
-
-    private final BroadcastReceiver mReceiverV21 = AndroidUtil.isLolliPopOrLater() ? new BroadcastReceiver()
-    {
-        @TargetApi(21)
-        @Override
-        public void onReceive(Context context, Intent intent) {
-            final String action = intent.getAction();
-            if (action == null)
-                return;
-            if (action.equalsIgnoreCase(AudioManager.ACTION_HDMI_AUDIO_PLUG)) {
-                mHasHdmiAudio = true;
-                Log.d(TAG, "has hdmi audio");
-            }
-        }
-    } : null;
-
     @Override
     public boolean onTrackballEvent(MotionEvent event) {
         if (mIsLoading)
@@ -2613,11 +2586,6 @@ public class VideoPlayerActivity extends AppCompatActivity implements IVLCVout.C
             if (openedPosition == -1) {
                 /* prepare playback */
                 mService.stop();
-                if (VLCOptions.isAudioHdmiEnabled() != mHasHdmiAudio) {
-                    VLCOptions.setAudioHdmiEnabled(mHasHdmiAudio);
-                    VLCInstance.restart(this);
-                    mService.restartMediaPlayer();
-                }
                 final MediaWrapper mw = new MediaWrapper(mUri);
                 if (wasPaused)
                     mw.addFlags(VLCOptions.MEDIA_PAUSED);
diff --git a/vlc-android/src/org/videolan/vlc/util/VLCOptions.java b/vlc-android/src/org/videolan/vlc/util/VLCOptions.java
index 6fb227e..9b8d308 100644
--- a/vlc-android/src/org/videolan/vlc/util/VLCOptions.java
+++ b/vlc-android/src/org/videolan/vlc/util/VLCOptions.java
@@ -53,8 +53,6 @@ public class VLCOptions {
     public final static int MEDIA_NO_HWACCEL = 0x02;
     public final static int MEDIA_PAUSED = 0x4;
 
-    private static boolean sHdmiAudioEnabled = false;
-
     public static ArrayList<String> getLibOptions(Context context) {
         final SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
 
@@ -97,11 +95,6 @@ public class VLCOptions {
         options.add("--androidwindow-chroma");
         options.add(chroma != null ? chroma : "RV32");
 
-        if (sHdmiAudioEnabled) {
-            options.add("--spdif");
-            options.add("--audiotrack-audio-channels");
-            options.add("8"); // 7.1 maximum
-        }
         options.add(verboseMode ? "-vvv" : "-vv");
         return options;
     }
@@ -215,12 +208,4 @@ public class VLCOptions {
         }
         Util.commitPreferences(editor);
     }
-
-    public static synchronized void setAudioHdmiEnabled(boolean enabled) {
-        sHdmiAudioEnabled = enabled;
-    }
-
-    public static synchronized boolean isAudioHdmiEnabled() {
-        return sHdmiAudioEnabled;
-    }
 }



More information about the Android mailing list