[Android] Audio track selection for multi-track audio files

Sébastien Toque git at videolan.org
Fri Mar 2 23:32:46 CET 2012


android | branch: master | Sébastien Toque <xilasz at gmail.com> | Fri Mar  2 23:14:16 2012 +0100| [abfa12b09d5b89186ed09763002c46bc8381a835] | committer: Sébastien Toque

Audio track selection for multi-track audio files

> http://git.videolan.org/gitweb.cgi/android.git/?a=commit;h=abfa12b09d5b89186ed09763002c46bc8381a835
---

 vlc-android/jni/libvlcjni.c                        |   42 ++++++++++++++++++++
 vlc-android/res/layout/player.xml                  |   34 +++++++++++++---
 vlc-android/src/org/videolan/vlc/LibVLC.java       |   12 ++++++
 .../vlc/gui/video/VideoPlayerActivity.java         |   39 ++++++++++++++++++
 4 files changed, 121 insertions(+), 6 deletions(-)

diff --git a/vlc-android/jni/libvlcjni.c b/vlc-android/jni/libvlcjni.c
index afd410a..1c99033 100644
--- a/vlc-android/jni/libvlcjni.c
+++ b/vlc-android/jni/libvlcjni.c
@@ -719,6 +719,48 @@ jint Java_org_videolan_vlc_LibVLC_getAudioTracksCount(JNIEnv *env, jobject thiz)
     return -1;
 }
 
+jobjectArray Java_org_videolan_vlc_LibVLC_getAudioTrackDescription(JNIEnv *env, jobject thiz)
+{
+    libvlc_media_player_t *mp = getMediaPlayer(env, thiz);
+    if (!mp)
+        return NULL;
+
+    int i_nbTracks = libvlc_audio_get_track_count(mp) - 1;
+    if (i_nbTracks < 0)
+        i_nbTracks = 0;
+    jobjectArray array = (*env)->NewObjectArray(env, i_nbTracks,
+            (*env)->FindClass(env, "java/lang/String"),
+            NULL);
+
+    libvlc_track_description_t *first = libvlc_audio_get_track_description(mp);
+    libvlc_track_description_t *desc = first != NULL ? first->p_next : NULL;
+    unsigned i;
+    for (i = 0; i < i_nbTracks; ++i)
+    {
+        jstring name = (*env)->NewStringUTF(env, desc->psz_name);
+        (*env)->SetObjectArrayElement(env, array, i, name);
+        desc = desc->p_next;
+    }
+    libvlc_track_description_list_release(first);
+    return array;
+}
+
+jint Java_org_videolan_vlc_LibVLC_getAudioTrack(JNIEnv *env, jobject thiz)
+{
+    libvlc_media_player_t *mp = getMediaPlayer(env, thiz);
+    if (mp)
+        return libvlc_audio_get_track(mp);
+    return -1;
+}
+
+jint Java_org_videolan_vlc_LibVLC_setAudioTrack(JNIEnv *env, jobject thiz, jint index)
+{
+    libvlc_media_player_t *mp = getMediaPlayer(env, thiz);
+    if (mp)
+        return libvlc_audio_set_track(mp, index);
+    return -1;
+}
+
 jint Java_org_videolan_vlc_LibVLC_getVideoTracksCount(JNIEnv *env, jobject thiz)
 {
     libvlc_media_player_t *mp = getMediaPlayer(env, thiz);
diff --git a/vlc-android/res/layout/player.xml b/vlc-android/res/layout/player.xml
index ed35c74..89e8ee3 100644
--- a/vlc-android/res/layout/player.xml
+++ b/vlc-android/res/layout/player.xml
@@ -31,20 +31,42 @@
         <TextView
             android:id="@+id/player_overlay_title"
             android:layout_width="0dp"
-            android:layout_height="wrap_content"
+            android:layout_height="fill_parent"
             android:layout_weight="1"
             android:gravity="left|center_vertical"
             android:text="@string/title"
             android:textColor="#ffffff"
             android:textSize="15dip" />
 
-        <TextView
-            android:id="@+id/player_overlay_battery"
+        <ImageButton
+            android:id="@+id/player_overlay_audio"
+            android:layout_width="40dp"
+            android:layout_height="40dp"
+            android:layout_marginLeft="10dp"
+            android:layout_marginRight="10dp"
+            android:background="@drawable/header_icon_audio" />
+
+        <LinearLayout
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
-            android:gravity="right|center_vertical"
-            android:textColor="#ffffff"
-            android:textSize="15dip" />
+            android:orientation="vertical" >
+
+            <TextView
+                android:id="@+id/player_overlay_systime"
+                android:layout_width="wrap_content"
+                android:layout_height="wrap_content"
+                android:layout_gravity="right|center_vertical"
+                android:textColor="#ffffff"
+                android:textSize="15dip" />
+
+            <TextView
+                android:id="@+id/player_overlay_battery"
+                android:layout_width="wrap_content"
+                android:layout_height="wrap_content"
+                android:layout_gravity="right|center_vertical"
+                android:textColor="#ffffff"
+                android:textSize="15dip" />
+        </LinearLayout>
     </LinearLayout>
 
     <RelativeLayout
diff --git a/vlc-android/src/org/videolan/vlc/LibVLC.java b/vlc-android/src/org/videolan/vlc/LibVLC.java
index 67bfcd0..e3f7682 100644
--- a/vlc-android/src/org/videolan/vlc/LibVLC.java
+++ b/vlc-android/src/org/videolan/vlc/LibVLC.java
@@ -346,6 +346,18 @@ public class LibVLC {
 
     private native TrackInfo[] readTracksInfo(int instance, String mrl);
 
+    public native int getAudioTracksCount();
+
+    public native String[] getAudioTrackDescription();
+
+    public native int getAudioTrack();
+
+    public native int setAudioTrack(int index);
+
+    public native int getVideoTracksCount();
+
+    public native int getSpuTracksCount();
+
     /**
      * Return true if there is a video track in the file
      */
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 bce717e..ba38e04 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
@@ -30,8 +30,11 @@ import org.videolan.vlc.R;
 import org.videolan.vlc.Util;
 
 import android.app.Activity;
+import android.app.AlertDialog;
+import android.app.AlertDialog.Builder;
 import android.content.BroadcastReceiver;
 import android.content.Context;
+import android.content.DialogInterface;
 import android.content.Intent;
 import android.content.IntentFilter;
 import android.content.pm.ActivityInfo;
@@ -44,6 +47,7 @@ import android.os.Handler;
 import android.os.Message;
 import android.os.PowerManager;
 import android.os.PowerManager.WakeLock;
+import android.text.format.DateFormat;
 import android.util.Log;
 import android.view.Display;
 import android.view.MotionEvent;
@@ -89,11 +93,13 @@ public class VideoPlayerActivity extends Activity {
     private boolean mShowing;
     private SeekBar mSeekbar;
     private TextView mTitle;
+    private TextView mSysTime;
     private TextView mBattery;
     private TextView mTime;
     private TextView mLength;
     private TextView mInfo;
     private SeekBar mWheel;
+    private ImageButton mAudio;
     private ImageButton mLock;
     private ImageButton mSize;
 
@@ -110,6 +116,7 @@ public class VideoPlayerActivity extends Activity {
     private int mAudioDisplayRange;
     private float mTouchY, mVol;
     private boolean mIsAudioChanged;
+    private String[] mAudioTracks;
 
     //Wheel
     private static final int WHEEL_DEAD_ZONE = 7;
@@ -133,6 +140,7 @@ public class VideoPlayerActivity extends Activity {
 
         /* header */
         mTitle = (TextView) findViewById(R.id.player_overlay_title);
+        mSysTime = (TextView) findViewById(R.id.player_overlay_systime);
         mBattery = (TextView) findViewById(R.id.player_overlay_battery);
 
         mTime = (TextView) findViewById(R.id.player_overlay_time);
@@ -146,6 +154,9 @@ public class VideoPlayerActivity extends Activity {
         mWheel.setProgress(mMiddle);
         mWheel.setOnSeekBarChangeListener(mWheelListener);
 
+        mAudio = (ImageButton) findViewById(R.id.player_overlay_audio);
+        mAudio.setOnClickListener(mAudioListener);
+
         mLock = (ImageButton) findViewById(R.id.player_overlay_lock);
         mLock.setOnClickListener(mLockListener);
 
@@ -545,6 +556,26 @@ public class VideoPlayerActivity extends Activity {
 
     };
 
+    private OnClickListener mAudioListener = new OnClickListener() {
+        public void onClick(View v) {
+            if (mAudioTracks == null || mAudioTracks.length <= 1)
+                return;
+
+            int current = mLibVLC.getAudioTrack() - 1;
+
+            Builder builder = new AlertDialog.Builder(VideoPlayerActivity.this);
+            builder.setSingleChoiceItems(mAudioTracks, current, new DialogInterface.OnClickListener() {
+                @Override
+                public void onClick(DialogInterface dialog, int which) {
+                    dialog.dismiss();
+                    mLibVLC.setAudioTrack(which + 1);
+                }
+            });
+
+            builder.show();
+        }
+    };
+
     /**
      *
      */
@@ -638,6 +669,13 @@ public class VideoPlayerActivity extends Activity {
             mHandler.removeMessages(FADE_OUT);
             mHandler.sendMessageDelayed(msg, timeout);
         }
+        if (mAudioTracks == null) {
+            mAudioTracks = mLibVLC.getAudioTrackDescription();
+            if (mAudioTracks != null && mAudioTracks.length > 1)
+                mAudio.setVisibility(View.VISIBLE);
+            else
+                mAudio.setVisibility(View.GONE);
+        }
         updateOverlayPausePlay();
     }
 
@@ -699,6 +737,7 @@ public class VideoPlayerActivity extends Activity {
 
         mSeekbar.setMax(length);
         mSeekbar.setProgress(time);
+        mSysTime.setText(DateFormat.format("kk:mm", System.currentTimeMillis()));
         mTime.setText(Util.millisToString(time));
         mLength.setText(Util.millisToString(length));
         return time;



More information about the Android mailing list