diff --git a/gst-sdk/tutorials/android-tutorial-1/jni/tutorial-1.c b/gst-sdk/tutorials/android-tutorial-1/jni/tutorial-1.c index df74084563..7ff846aa1f 100755 --- a/gst-sdk/tutorials/android-tutorial-1/jni/tutorial-1.c +++ b/gst-sdk/tutorials/android-tutorial-1/jni/tutorial-1.c @@ -345,8 +345,11 @@ void gst_native_set_uri (JNIEnv* env, jobject thiz, jstring uri) { if (!data) return; const jbyte *char_uri = (*env)->GetStringUTFChars (env, uri, NULL); GST_DEBUG ("Setting URI to %s", char_uri); + if (data->target_state >= GST_STATE_READY) + gst_element_set_state (data->pipeline, GST_STATE_READY); g_object_set(data->pipeline, "uri", char_uri); (*env)->ReleaseStringUTFChars (env, uri, char_uri); + data->is_live = (gst_element_set_state (data->pipeline, data->target_state) == GST_STATE_CHANGE_NO_PREROLL); } void gst_native_play (JNIEnv* env, jobject thiz) { diff --git a/gst-sdk/tutorials/android-tutorial-1/res/layout/main.xml b/gst-sdk/tutorials/android-tutorial-1/res/layout/main.xml index 4c3e1000c2..c3baac85a6 100755 --- a/gst-sdk/tutorials/android-tutorial-1/res/layout/main.xml +++ b/gst-sdk/tutorials/android-tutorial-1/res/layout/main.xml @@ -29,6 +29,14 @@ android:contentDescription="@string/button_stop" android:src="@android:drawable/ic_media_pause" android:text="@string/button_stop" /> + + - \ No newline at end of file + diff --git a/gst-sdk/tutorials/android-tutorial-1/res/values/strings.xml b/gst-sdk/tutorials/android-tutorial-1/res/values/strings.xml index 60c84d95a7..b79c80827b 100755 --- a/gst-sdk/tutorials/android-tutorial-1/res/values/strings.xml +++ b/gst-sdk/tutorials/android-tutorial-1/res/values/strings.xml @@ -3,4 +3,5 @@ Android tutorial 1 Play Stop + Select diff --git a/gst-sdk/tutorials/android-tutorial-1/src/com/gst_sdk_tutorials/tutorial_1/Tutorial1.java b/gst-sdk/tutorials/android-tutorial-1/src/com/gst_sdk_tutorials/tutorial_1/Tutorial1.java index 666a77dcaf..5243b84e43 100755 --- a/gst-sdk/tutorials/android-tutorial-1/src/com/gst_sdk_tutorials/tutorial_1/Tutorial1.java +++ b/gst-sdk/tutorials/android-tutorial-1/src/com/gst_sdk_tutorials/tutorial_1/Tutorial1.java @@ -22,8 +22,13 @@ import java.util.TimeZone; import com.gst_sdk.GStreamer; import android.app.Activity; +import android.content.Intent; +import android.database.Cursor; import android.util.Log; +import android.net.Uri; import android.os.Bundle; +import android.os.Environment; +import android.provider.MediaStore; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; @@ -54,10 +59,11 @@ public class Tutorial1 extends Activity implements SurfaceHolder.Callback, OnSee private Bundle initialization_data; - private final String mediaUri = "http://docs.gstreamer.com/media/sintel_trailer-480p.ogv"; - - /* Called when the activity is first created. - @Override */ + private String mediaUri = "http://docs.gstreamer.com/media/sintel_trailer-480p.ogv"; + static private final int PICK_FILE_CODE = 1; + + /* Called when the activity is first created. */ + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -87,6 +93,15 @@ public class Tutorial1 extends Activity implements SurfaceHolder.Callback, OnSee nativePause(); } }); + + ImageButton select = (ImageButton) this.findViewById(R.id.button_select); + select.setOnClickListener(new OnClickListener() { + public void onClick(View v) { + Intent i = new Intent(Intent.ACTION_PICK); + i.setDataAndType(Uri.fromFile(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES)), "video/*;audio/*"); + startActivityForResult(i, PICK_FILE_CODE); + } + }); SurfaceView sv = (SurfaceView) this.findViewById(R.id.surface_video); SurfaceHolder sh = sv.getHolder(); @@ -104,10 +119,11 @@ public class Tutorial1 extends Activity implements SurfaceHolder.Callback, OnSee } protected void onSaveInstanceState (Bundle outState) { - Log.d ("GStreamer", "Saving state, playing:" + is_playing_desired + " position:" + position); + Log.d ("GStreamer", "Saving state, playing:" + is_playing_desired + " position:" + position + " uri: " + mediaUri); outState.putBoolean("playing", is_playing_desired); outState.putInt("position", position); outState.putInt("duration", duration); + outState.putString("mediaUri", mediaUri); } protected void onDestroy() { @@ -124,27 +140,30 @@ public class Tutorial1 extends Activity implements SurfaceHolder.Callback, OnSee } }); } - - /* Called from native code */ - private void onGStreamerInitialized () { + + private void setMediaUri() { nativeSetUri (mediaUri); if (mediaUri.startsWith("file://")) is_local_media = true; + } + /* Called from native code */ + private void onGStreamerInitialized () { if (initialization_data != null) { is_playing_desired = initialization_data.getBoolean("playing"); int milliseconds = initialization_data.getInt("position"); Log.i ("GStreamer", "Restoring state, playing:" + is_playing_desired + " position:" + milliseconds + " ms."); + mediaUri = initialization_data.getString ("mediaUri"); /* Actually, move to one millisecond in the future. Otherwise, due to rounding errors between the * milliseconds used here and the nanoseconds used by GStreamer, we would be jumping a bit behind * where we were before. This, combined with seeking to keyframe positions, would skip one keyframe * backwards on each iteration. */ nativeSetPosition(milliseconds + 1); - if (is_playing_desired) { - nativePlay(); - } else { - nativePause(); - } + } + + setMediaUri (); + if (is_playing_desired) { + nativePlay(); } else { nativePause(); } @@ -244,4 +263,17 @@ public class Tutorial1 extends Activity implements SurfaceHolder.Callback, OnSee if (!is_local_media) nativeSetPosition(desired_position); if (is_playing_desired) nativePlay(); } + + @Override + protected void onActivityResult(int requestCode, int resultCode, Intent data) + { + if (resultCode == RESULT_OK && requestCode == PICK_FILE_CODE) { + String[] proj = { MediaStore.Video.Media.DATA }; + Cursor c = managedQuery(data.getData(), proj, null, null, null); + int column_index = c.getColumnIndexOrThrow(MediaStore.Video.Media.DATA); + c.moveToFirst(); + mediaUri = "file://" + c.getString(column_index); + setMediaUri(); + } + } }