[Android] [PATCH 3/4] VideoPlayer: activate hdmi audio if available

Thomas Guillem thomas at gllm.fr
Tue Apr 7 11:40:35 CEST 2015


---
 .../vlc/gui/video/VideoPlayerActivity.java         | 28 ++++++++++++++++++++++
 .../src/org/videolan/vlc/util/VLCInstance.java     |  7 ++++++
 2 files changed, 35 insertions(+)

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 23faf4c..89f3f37 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
@@ -304,6 +304,8 @@ public class VideoPlayerActivity extends ActionBarActivity implements IVideoPlay
     private OnLayoutChangeListener mOnLayoutChangeListener;
     private AlertDialog mAlertDialog;
 
+    private boolean mHasHdmiAudio = false;
+
     @Override
     @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
     protected void onCreate(Bundle savedInstanceState) {
@@ -465,6 +467,8 @@ public class VideoPlayerActivity extends ActionBarActivity implements IVideoPlay
             filter.addAction(Intent.ACTION_BATTERY_CHANGED);
         filter.addAction(VLCApplication.SLEEP_INTENT);
         registerReceiver(mReceiver, filter);
+        if (mReceiverV21 != null)
+            registerV21();
 
         Log.d(TAG,
                 "Hardware acceleration mode: "
@@ -572,6 +576,8 @@ public class VideoPlayerActivity extends ActionBarActivity implements IVideoPlay
         super.onDestroy();
         if (mReceiver != null)
             unregisterReceiver(mReceiver);
+        if (mReceiverV21 != null)
+            unregisterReceiver(mReceiverV21);
 
         mAudioManager = null;
     }
@@ -829,6 +835,27 @@ public class VideoPlayerActivity extends ActionBarActivity implements IVideoPlay
         }
     };
 
+    @TargetApi(21)
+    private void registerV21() {
+        final IntentFilter intentFilter = new IntentFilter(AudioManager.ACTION_HDMI_AUDIO_PLUG);
+        registerReceiver(mReceiverV21, intentFilter);
+    }
+
+    private final BroadcastReceiver mReceiverV21 = LibVlcUtil.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) {
         showOverlay();
@@ -2623,6 +2650,7 @@ public class VideoPlayerActivity extends ActionBarActivity implements IVideoPlay
             mMediaListPlayer.getMediaList().add(new MediaWrapper(media));
             savedIndexPosition = mMediaListPlayer.getMediaList().size() - 1;
         }
+        VLCInstance.setAudioHdmiEnabled(this, mHasHdmiAudio);
         mMediaListPlayer.playIndex(savedIndexPosition, wasPaused);
 
         mCanSeek = false;
diff --git a/vlc-android/src/org/videolan/vlc/util/VLCInstance.java b/vlc-android/src/org/videolan/vlc/util/VLCInstance.java
index 547580b..cea57d9 100644
--- a/vlc-android/src/org/videolan/vlc/util/VLCInstance.java
+++ b/vlc-android/src/org/videolan/vlc/util/VLCInstance.java
@@ -144,4 +144,11 @@ public class VLCInstance {
         sLibVLC.setHardwareAcceleration(hardwareAcceleration);
         sLibVLC.setDevHardwareDecoder(devHardwareDecoder);
     }
+
+    public static synchronized void setAudioHdmiEnabled(Context context, boolean enabled) {
+        if (sLibVLC != null && sLibVLC.isHdmiAudioEnabled() != enabled) {
+            sLibVLC.setHdmiAudioEnabled(enabled);
+            restart(context);
+        }
+    }
 }
-- 
2.1.3



More information about the Android mailing list