[Android] spu: use TrackID instead of index

Sébastien Toque git at videolan.org
Thu Feb 28 20:31:58 CET 2013


vlc-ports/android | branch: master | Sébastien Toque <xilasz at gmail.com> | Thu Feb 28 20:22:01 2013 +0100| [cfc88aa13f81949bcd7f6f4ad296578577dc2f6e] | committer: Sébastien Toque

spu: use TrackID instead of index

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

 vlc-android/jni/libvlcjni.c                        |   29 +++++++++---
 vlc-android/src/org/videolan/vlc/LibVLC.java       |    2 +-
 .../vlc/gui/video/VideoPlayerActivity.java         |   50 +++++++++++++-------
 3 files changed, 56 insertions(+), 25 deletions(-)

diff --git a/vlc-android/jni/libvlcjni.c b/vlc-android/jni/libvlcjni.c
index 0403401..1464b4f 100644
--- a/vlc-android/jni/libvlcjni.c
+++ b/vlc-android/jni/libvlcjni.c
@@ -1106,28 +1106,45 @@ jint Java_org_videolan_vlc_LibVLC_getVideoTracksCount(JNIEnv *env, jobject thiz)
     return -1;
 }
 
-jobjectArray Java_org_videolan_vlc_LibVLC_getSpuTrackDescription(JNIEnv *env, jobject thiz)
+jobject Java_org_videolan_vlc_LibVLC_getSpuTrackDescription(JNIEnv *env, jobject thiz)
 {
     libvlc_media_player_t *mp = getMediaPlayer(env, thiz);
     if (!mp)
         return NULL;
 
     int i_nbTracks = libvlc_video_get_spu_count(mp);
-    jobjectArray array = (*env)->NewObjectArray(env, i_nbTracks,
-            (*env)->FindClass(env, "java/lang/String"),
-            NULL);
+    jclass mapClass = (*env)->FindClass(env, "java/util/Map");
+    jclass hashMapClass = (*env)->FindClass(env, "java/util/HashMap");
+    jmethodID mapPut = (*env)->GetMethodID(env, mapClass, "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
+    /*
+     * "What are you building? Lay your hand on it. Where is it?"
+     * We need a concrete map to start
+     */
+    jmethodID mapInit = (*env)->GetMethodID(env, hashMapClass, "<init>", "()V");
+    jclass integerCls = (*env)->FindClass(env, "java/lang/Integer");
+    jmethodID integerConstructor = (*env)->GetMethodID(env, integerCls, "<init>", "(I)V");
+
+    jobject spuTrackMap = (*env)->NewObject(env, hashMapClass, mapInit);
 
     libvlc_track_description_t *first = libvlc_video_get_spu_description(mp);
     libvlc_track_description_t *desc = first;
     unsigned i;
     for (i = 0; i < i_nbTracks; ++i)
     {
+        // store audio track ID and name in a map as <ID, Track Name>
+        jobject track_id = (*env)->NewObject(env, integerCls, integerConstructor, desc->i_id);
         jstring name = (*env)->NewStringUTF(env, desc->psz_name);
-        (*env)->SetObjectArrayElement(env, array, i, name);
+        (*env)->CallObjectMethod(env, spuTrackMap, mapPut, track_id, name);
         desc = desc->p_next;
     }
     libvlc_track_description_list_release(first);
-    return array;
+
+    // Clean up local references
+    (*env)->DeleteLocalRef(env, mapClass);
+    (*env)->DeleteLocalRef(env, hashMapClass);
+    (*env)->DeleteLocalRef(env, integerCls);
+
+    return spuTrackMap;
 }
 
 jint Java_org_videolan_vlc_LibVLC_getSpuTracksCount(JNIEnv *env, jobject thiz)
diff --git a/vlc-android/src/org/videolan/vlc/LibVLC.java b/vlc-android/src/org/videolan/vlc/LibVLC.java
index df4ba14..9a7f048 100644
--- a/vlc-android/src/org/videolan/vlc/LibVLC.java
+++ b/vlc-android/src/org/videolan/vlc/LibVLC.java
@@ -471,7 +471,7 @@ public class LibVLC {
 
     public native int getVideoTracksCount();
 
-    public native String[] getSpuTrackDescription();
+    public native Map<Integer,String> getSpuTrackDescription();
 
     public native int getSpuTrack();
 
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 ce74df8..1ea60cd 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
@@ -24,8 +24,6 @@ import java.io.File;
 import java.io.UnsupportedEncodingException;
 import java.lang.reflect.Method;
 import java.net.URLDecoder;
-import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.Map;
 
 import org.videolan.vlc.AudioServiceController;
@@ -143,7 +141,7 @@ public class VideoPlayerActivity extends Activity {
     private ImageButton mSize;
     private boolean mIsLocked = false;
     private int mLastAudioTrack = -1;
-    private int mLastSpuTrack = -1;
+    private int mLastSpuTrack = -2;
 
     /**
      * For uninterrupted switching between audio and video mode
@@ -174,8 +172,7 @@ public class VideoPlayerActivity extends Activity {
 
     // Tracks & Subtitles
     private Map<Integer,String> mAudioTracksList;
-    private String[] mSubtitleTracksLibVLC;
-    private ArrayList<String> mSubtitleTracksList;
+    private Map<Integer,String> mSubtitleTracksList;
 
     // Advance Function
     private AdvFuncDialog mAdvFuncDialog;
@@ -995,18 +992,37 @@ public class VideoPlayerActivity extends Activity {
     private final OnClickListener mSubtitlesListener = new OnClickListener() {
         @Override
         public void onClick(View v) {
-            String[] arrList = new String[mSubtitleTracksList.size()];
-            arrList = mSubtitleTracksList.toArray(arrList);
+            final String[] arrList = new String[mSubtitleTracksList.size()];
+            int i = 0;
+            int listPosition = 0;
+            for(Map.Entry<Integer,String> entry : mSubtitleTracksList.entrySet()) {
+                arrList[i] = entry.getValue();
+                // map the track position to the list position
+                if(entry.getKey() == mLibVLC.getSpuTrack())
+                    listPosition = i;
+                i++;
+            }
+
             AlertDialog dialog = new AlertDialog.Builder(VideoPlayerActivity.this)
             .setTitle(R.string.track_text)
-            .setSingleChoiceItems(arrList, mLibVLC.getSpuTrack(), new DialogInterface.OnClickListener() {
+            .setSingleChoiceItems(arrList, listPosition, new DialogInterface.OnClickListener() {
                 @Override
-                public void onClick(DialogInterface dialog, int position) {
+                public void onClick(DialogInterface dialog, int listPosition) {
+                    int trackID = -2;
+                    // Reverse map search...
+                    for(Map.Entry<Integer, String> entry : mSubtitleTracksList.entrySet()) {
+                        if(arrList[listPosition].equals(entry.getValue())) {
+                            trackID = entry.getKey();
+                            break;
+                        }
+                    }
+                    if(trackID < -1) return;
+
                     DatabaseManager.getInstance(VideoPlayerActivity.this).updateMedia(
                             mLocation,
                             DatabaseManager.mediaColumn.MEDIA_SPUTRACK,
-                            position);
-                    mLibVLC.setSpuTrack(position);
+                            trackID);
+                    mLibVLC.setSpuTrack(trackID);
                     dialog.dismiss();
                 }
             })
@@ -1262,9 +1278,9 @@ public class VideoPlayerActivity extends Activity {
             mLibVLC.setAudioTrack(mLastAudioTrack);
             mLastAudioTrack = -1;
         }
-        if (mLastSpuTrack >= 0) {
+        if (mLastSpuTrack >= -1) {
             mLibVLC.setSpuTrack(mLastSpuTrack);
-            mLastSpuTrack = -1;
+            mLastSpuTrack = -2;
         }
     }
 
@@ -1280,13 +1296,11 @@ public class VideoPlayerActivity extends Activity {
                 mAudioTrack.setOnClickListener(null);
             }
         }
-        if (mSubtitleTracksLibVLC == null) {
-            mSubtitleTracksLibVLC = mLibVLC.getSpuTrackDescription();
-            if (mSubtitleTracksLibVLC != null && mSubtitleTracksLibVLC.length > 0) {
-                mSubtitleTracksList = new ArrayList<String>(Arrays.asList(mSubtitleTracksLibVLC));
+        if (mSubtitleTracksList == null) {
+            if (mLibVLC.getSpuTracksCount() > 0) {
+                mSubtitleTracksList = mLibVLC.getSpuTrackDescription();
                 mSubtitle.setOnClickListener(mSubtitlesListener);
                 mSubtitle.setVisibility(View.VISIBLE);
-
             }
             else {
                 mSubtitle.setVisibility(View.GONE);



More information about the Android mailing list