Add a SeekBar to show current position / duration

This commit is contained in:
Xavi Artigas 2012-09-13 16:35:51 +02:00
parent 8f8258e4b9
commit 3bc044cba4
3 changed files with 45 additions and 30 deletions

View file

@ -96,34 +96,6 @@ static void set_current_position (gint64 position, gint64 duration, CustomData *
} }
} }
static void error_cb (GstBus *bus, GstMessage *msg, CustomData *data) {
GError *err;
gchar *debug_info;
gchar *message_string;
gst_message_parse_error (msg, &err, &debug_info);
message_string = g_strdup_printf ("Error received from element %s: %s", GST_OBJECT_NAME (msg->src), err->message);
g_clear_error (&err);
g_free (debug_info);
set_message (message_string, data);
g_free (message_string);
gst_element_set_state (data->pipeline, GST_STATE_NULL);
}
static void eos_cb (GstBus *bus, GstMessage *msg, CustomData *data) {
set_message (GST_MESSAGE_TYPE_NAME (msg), data);
gst_element_set_state (data->pipeline, GST_STATE_NULL);
}
static void state_changed_cb (GstBus *bus, GstMessage *msg, CustomData *data) {
GstState old_state, new_state, pending_state;
gst_message_parse_state_changed (msg, &old_state, &new_state, &pending_state);
if (GST_MESSAGE_SRC (msg) == GST_OBJECT (data->pipeline)) {
set_message (gst_element_state_get_name (new_state), data);
data->playing = (new_state == GST_STATE_PLAYING);
}
}
static gboolean refresh_ui (CustomData *data) { static gboolean refresh_ui (CustomData *data) {
GstFormat fmt = GST_FORMAT_TIME; GstFormat fmt = GST_FORMAT_TIME;
gint64 current = -1; gint64 current = -1;
@ -146,6 +118,35 @@ static gboolean refresh_ui (CustomData *data) {
return TRUE; return TRUE;
} }
static void error_cb (GstBus *bus, GstMessage *msg, CustomData *data) {
GError *err;
gchar *debug_info;
gchar *message_string;
gst_message_parse_error (msg, &err, &debug_info);
message_string = g_strdup_printf ("Error received from element %s: %s", GST_OBJECT_NAME (msg->src), err->message);
g_clear_error (&err);
g_free (debug_info);
set_message (message_string, data);
g_free (message_string);
gst_element_set_state (data->pipeline, GST_STATE_NULL);
}
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);
}
static void state_changed_cb (GstBus *bus, GstMessage *msg, CustomData *data) {
GstState old_state, new_state, pending_state;
gst_message_parse_state_changed (msg, &old_state, &new_state, &pending_state);
if (GST_MESSAGE_SRC (msg) == GST_OBJECT (data->pipeline)) {
set_message (gst_element_state_get_name (new_state), data);
data->playing = (new_state == GST_STATE_PLAYING);
}
}
static void *app_function (void *userdata) { static void *app_function (void *userdata) {
JavaVMAttachArgs args; JavaVMAttachArgs args;
GstBus *bus; GstBus *bus;

View file

@ -34,7 +34,17 @@
android:id="@+id/textview_time" android:id="@+id/textview_time"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center_vertical" /> android:layout_gravity="center_vertical"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip" />
<SeekBar
android:id="@+id/seek_bar"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:indeterminate="false" />
</LinearLayout> </LinearLayout>
<SurfaceView <SurfaceView

View file

@ -27,6 +27,7 @@ import android.view.SurfaceView;
import android.view.View; import android.view.View;
import android.view.View.OnClickListener; import android.view.View.OnClickListener;
import android.widget.ImageButton; import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView; import android.widget.TextView;
public class Tutorial1 extends Activity implements SurfaceHolder.Callback { 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 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"); SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
df.setTimeZone(TimeZone.getTimeZone("UTC")); df.setTimeZone(TimeZone.getTimeZone("UTC"));
final String message = df.format(new Date (position)) + " / " + df.format(new Date (duration)); final String message = df.format(new Date (position)) + " / " + df.format(new Date (duration));
runOnUiThread (new Runnable() { runOnUiThread (new Runnable() {
public void run() { public void run() {
tv.setText(message); tv.setText(message);
sb.setMax((int)duration);
sb.setProgress((int)position);
} }
}); });
} }