1 package org.freedesktop.gstreamer.tutorials.tutorial_3;
3 import android.app.Activity;
4 import android.os.Bundle;
5 import android.util.Log;
6 import android.view.SurfaceHolder;
7 import android.view.SurfaceView;
8 import android.view.View;
9 import android.view.View.OnClickListener;
10 import android.widget.ImageButton;
11 import android.widget.TextView;
12 import android.widget.Toast;
14 import org.freedesktop.gstreamer.GStreamer;
16 public class Tutorial3 extends Activity implements SurfaceHolder.Callback {
17 private native void nativeInit(); // Initialize native code, build pipeline, etc
18 private native void nativeFinalize(); // Destroy pipeline and shutdown native code
19 private native void nativePlay(); // Set pipeline to PLAYING
20 private native void nativePause(); // Set pipeline to PAUSED
21 private static native boolean nativeClassInit(); // Initialize native class: cache Method IDs for callbacks
22 private native void nativeSurfaceInit(Object surface);
23 private native void nativeSurfaceFinalize();
24 private long native_custom_data; // Native code will use this to keep private data
26 private boolean is_playing_desired; // Whether the user asked to go to PLAYING
28 // Called when the activity is first created.
30 public void onCreate(Bundle savedInstanceState)
32 super.onCreate(savedInstanceState);
34 // Initialize GStreamer and warn if it fails
37 } catch (Exception e) {
38 Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
43 setContentView(R.layout.main);
45 ImageButton play = (ImageButton) this.findViewById(R.id.button_play);
46 play.setOnClickListener(new OnClickListener() {
47 public void onClick(View v) {
48 is_playing_desired = true;
53 ImageButton pause = (ImageButton) this.findViewById(R.id.button_stop);
54 pause.setOnClickListener(new OnClickListener() {
55 public void onClick(View v) {
56 is_playing_desired = false;
61 SurfaceView sv = (SurfaceView) this.findViewById(R.id.surface_video);
62 SurfaceHolder sh = sv.getHolder();
65 if (savedInstanceState != null) {
66 is_playing_desired = savedInstanceState.getBoolean("playing");
67 Log.i ("GStreamer", "Activity created. Saved state is playing:" + is_playing_desired);
69 is_playing_desired = false;
70 Log.i ("GStreamer", "Activity created. There is no saved state, playing: false");
73 // Start with disabled buttons, until native code is initialized
74 this.findViewById(R.id.button_play).setEnabled(false);
75 this.findViewById(R.id.button_stop).setEnabled(false);
80 protected void onSaveInstanceState (Bundle outState) {
81 Log.d ("GStreamer", "Saving state, playing:" + is_playing_desired);
82 outState.putBoolean("playing", is_playing_desired);
85 protected void onDestroy() {
90 // Called from native code. This sets the content of the TextView from the UI thread.
91 private void setMessage(final String message) {
92 final TextView tv = (TextView) this.findViewById(R.id.textview_message);
93 runOnUiThread (new Runnable() {
100 // Called from native code. Native code calls this once it has created its pipeline and
101 // the main loop is running, so it is ready to accept commands.
102 private void onGStreamerInitialized () {
103 Log.i ("GStreamer", "Gst initialized. Restoring state, playing:" + is_playing_desired);
104 // Restore previous playing state
105 if (is_playing_desired) {
111 // Re-enable buttons, now that GStreamer is initialized
112 final Activity activity = this;
113 runOnUiThread(new Runnable() {
115 activity.findViewById(R.id.button_play).setEnabled(true);
116 activity.findViewById(R.id.button_stop).setEnabled(true);
122 System.loadLibrary("gstreamer_android");
123 System.loadLibrary("tutorial-3");
127 public void surfaceChanged(SurfaceHolder holder, int format, int width,
129 Log.d("GStreamer", "Surface changed to format " + format + " width "
130 + width + " height " + height);
131 nativeSurfaceInit (holder.getSurface());
134 public void surfaceCreated(SurfaceHolder holder) {
135 Log.d("GStreamer", "Surface created: " + holder.getSurface());
138 public void surfaceDestroyed(SurfaceHolder holder) {
139 Log.d("GStreamer", "Surface destroyed");
140 nativeSurfaceFinalize ();