Add a SeekBar to show current position / duration
authorXavi Artigas <xartigas@fluendo.com>
Thu, 13 Sep 2012 14:35:51 +0000 (16:35 +0200)
committerXavi Artigas <xartigas@fluendo.com>
Thu, 13 Sep 2012 14:35:51 +0000 (16:35 +0200)
gst-sdk/tutorials/android-tutorial-1/jni/tutorial-1.c
gst-sdk/tutorials/android-tutorial-1/res/layout/main.xml
gst-sdk/tutorials/android-tutorial-1/src/com/gst_sdk_tutorials/tutorial_1/Tutorial1.java

index c35141c..c5d037d 100755 (executable)
@@ -96,6 +96,28 @@ static void set_current_position (gint64 position, gint64 duration, CustomData *
   }
 }
 
+static gboolean refresh_ui (CustomData *data) {
+  GstFormat fmt = GST_FORMAT_TIME;
+  gint64 current = -1;
+
+  /* We do not want to update anything unless we are in the PLAYING state */
+  if (!data->playing)
+    return TRUE;
+   
+  /* If we didn't know it yet, query the stream duration */
+  if (!GST_CLOCK_TIME_IS_VALID (data->duration)) {
+    if (!gst_element_query_duration (data->pipeline, &fmt, &data->duration)) {
+      GST_WARNING ("Could not query current duration");
+    }
+  }
+   
+  if (gst_element_query_position (data->pipeline, &fmt, &data->position)) {
+    /* Java expects these values in milliseconds, and Gst provides nanoseconds */
+    set_current_position (data->position/1000000, data->duration/1000000, data);
+  }
+  return TRUE;
+}
+
 static void error_cb (GstBus *bus, GstMessage *msg, CustomData *data) {
   GError *err;
   gchar *debug_info;
@@ -112,6 +134,7 @@ static void error_cb (GstBus *bus, GstMessage *msg, CustomData *data) {
 
 static void eos_cb (GstBus *bus, GstMessage *msg, CustomData *data) {
   set_message (GST_MESSAGE_TYPE_NAME (msg), data);
+  refresh_ui (data);
   gst_element_set_state (data->pipeline, GST_STATE_NULL);
 }
 
@@ -124,28 +147,6 @@ static void state_changed_cb (GstBus *bus, GstMessage *msg, CustomData *data) {
   }
 }
 
-static gboolean refresh_ui (CustomData *data) {
-  GstFormat fmt = GST_FORMAT_TIME;
-  gint64 current = -1;
-
-  /* We do not want to update anything unless we are in the PLAYING state */
-  if (!data->playing)
-    return TRUE;
-   
-  /* If we didn't know it yet, query the stream duration */
-  if (!GST_CLOCK_TIME_IS_VALID (data->duration)) {
-    if (!gst_element_query_duration (data->pipeline, &fmt, &data->duration)) {
-      GST_WARNING ("Could not query current duration");
-    }
-  }
-   
-  if (gst_element_query_position (data->pipeline, &fmt, &data->position)) {
-    /* Java expects these values in milliseconds, and Gst provides nanoseconds */
-    set_current_position (data->position/1000000, data->duration/1000000, data);
-  }
-  return TRUE;
-}
-
 static void *app_function (void *userdata) {
   JavaVMAttachArgs args;
   GstBus *bus;
index edf04e3..4c3e100 100755 (executable)
             android:id="@+id/textview_time"\r
             android:layout_width="wrap_content"\r
             android:layout_height="wrap_content"\r
-            android:layout_gravity="center_vertical" />\r
+            android:layout_gravity="center_vertical"\r
+            android:layout_marginLeft="5dip"\r
+            android:layout_marginRight="5dip" />\r
+\r
+        <SeekBar\r
+            android:id="@+id/seek_bar"\r
+            android:layout_width="0dip"\r
+            android:layout_height="wrap_content"\r
+            android:layout_gravity="center_vertical"\r
+            android:layout_weight="1"\r
+            android:indeterminate="false" />\r
     </LinearLayout>\r
 \r
     <SurfaceView\r
index 3da3d75..57dd455 100755 (executable)
@@ -27,6 +27,7 @@ import android.view.SurfaceView;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.widget.ImageButton;
+import android.widget.SeekBar;
 import android.widget.TextView;
 
 public class Tutorial1 extends Activity implements SurfaceHolder.Callback {
@@ -82,14 +83,17 @@ public class Tutorial1 extends Activity implements SurfaceHolder.Callback {
         });
     }
 
-    private void setCurrentPosition(long position, long duration) {
+    private void setCurrentPosition(final long position, final long duration) {
         final TextView tv = (TextView) this.findViewById(R.id.textview_time);
+        final SeekBar sb = (SeekBar) this.findViewById(R.id.seek_bar);
         SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
         df.setTimeZone(TimeZone.getTimeZone("UTC"));
         final String message = df.format(new Date (position)) + " / " + df.format(new Date (duration));
         runOnUiThread (new Runnable() {
           public void run() {
             tv.setText(message);
+            sb.setMax((int)duration);
+            sb.setProgress((int)position);
           }
         });
     }