NativeMedia.java revision 97bdbe13fc48640babe6c1ce270660476f04c3df
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.example.nativemedia;
18
19import android.app.Activity;
20import android.media.MediaPlayer.OnPreparedListener;
21import android.media.MediaPlayer;
22import android.net.Uri;
23import android.os.Bundle;
24import android.util.Log;
25import android.view.Surface;
26import android.view.SurfaceHolder;
27import android.view.SurfaceView;
28import android.view.View.OnClickListener;
29import android.view.View;
30import android.widget.Button;
31import java.io.IOException;
32
33public class NativeMedia extends Activity {
34    public static final String TAG = "NativeMedia";
35
36    static boolean isPlayingStreaming = false;
37    MediaPlayer mp;
38    SurfaceView javaSurfaceView;
39    SurfaceView openmaxalSurfaceView;
40    SurfaceHolder javaHolder;
41    SurfaceHolder openmaxalHolder;
42
43    /** Called when the activity is first created. */
44    @Override
45    public void onCreate(Bundle icicle) {
46        super.onCreate(icicle);
47        setContentView(R.layout.main);
48
49        // initialize native media system
50
51        createEngine();
52
53        javaSurfaceView = (SurfaceView) findViewById(R.id.java_surface);
54        openmaxalSurfaceView = (SurfaceView) findViewById(R.id.openmaxal_surface);
55
56        javaHolder = javaSurfaceView.getHolder();
57        openmaxalHolder = openmaxalSurfaceView.getHolder();
58
59        openmaxalHolder.addCallback(new SurfaceHolder.Callback() {
60
61            public void surfaceChanged(SurfaceHolder holder, int format,
62                    int width, int height) {
63                Log.v(TAG, "surfaceChanged format=" + format + ", width=" + width + ", height=" +
64                        height);
65            }
66
67            public void surfaceCreated(SurfaceHolder holder) {
68                Log.v(TAG, "surfaceCreated");
69                setSurface(holder.getSurface());
70                Log.v(TAG, "surfaceCreated 2");
71            }
72
73            public void surfaceDestroyed(SurfaceHolder holder) {
74                Log.v(TAG, "surfaceDestroyed");
75            }
76
77        });
78
79        openmaxalHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
80
81        ((Button) findViewById(R.id.java_player)).setOnClickListener(new OnClickListener() {
82            public void onClick(View view) {
83                mp.setOnPreparedListener(new OnPreparedListener() {
84                    public void onPrepared(MediaPlayer xy) {
85                        int width = xy.getVideoWidth();
86                        int height = xy.getVideoHeight();
87                        Log.v(TAG, "onPrepared width=" + width + ", height=" + height);
88                        if (width!=0 && height!=0) {
89                          javaHolder.setFixedSize(width, height);
90                          xy.setOnVideoSizeChangedListener(
91                            new MediaPlayer.OnVideoSizeChangedListener() {
92                              public void onVideoSizeChanged(MediaPlayer ab, int width,
93                                int height) {
94                                  int w2 = ab.getVideoWidth();
95                                  int h2 = ab.getVideoHeight();
96                                  Log.v(TAG, "onVideoSizeChanged width=" + w2 + " (" + width +
97                                        "), height=" + h2 + " (" + height + ")");
98                                  if (w2 != 0 && h2 != 0) {
99                                      javaHolder.setFixedSize(w2, h2);
100                                  }
101                              }
102                          });
103                          xy.start();
104
105                        }
106                    }
107                });
108                mp.prepareAsync();
109
110            }
111        });
112
113        // initialize button click handlers
114
115        ((Button) findViewById(R.id.openmaxal_player)).setOnClickListener(new OnClickListener() {
116            boolean created = false;
117            public void onClick(View view) {
118                if (!created) {
119                    created = createStreamingMediaPlayer("/sdcard/Ratatouille.ts");
120                } else {
121                    isPlayingStreaming = !isPlayingStreaming;
122                    setPlayingStreamingMediaPlayer(isPlayingStreaming);
123                }
124            }
125        });
126
127        mp = new MediaPlayer();
128        mp.setDisplay(javaHolder);
129        try {
130            mp.setDataSource("/sdcard/burnAfterReading.m4v");
131        } catch (IOException e) {
132
133        }
134
135    }
136
137    /** Called when the activity is about to be paused. */
138    @Override
139    protected void onPause()
140    {
141        isPlayingStreaming = false;
142        setPlayingStreamingMediaPlayer(false);
143        super.onPause();
144    }
145
146    /** Called when the activity is about to be destroyed. */
147    @Override
148    protected void onDestroy()
149    {
150        shutdown();
151        super.onDestroy();
152    }
153
154    /** Native methods, implemented in jni folder */
155    public static native void createEngine();
156    public static native boolean createStreamingMediaPlayer(String filename);
157    public static native void setPlayingStreamingMediaPlayer(boolean isPlaying);
158    public static native void shutdown();
159    public static native void setSurface(Surface surface);
160
161    /** Load jni .so on initialization */
162    static {
163         System.loadLibrary("native-media-jni");
164    }
165
166}
167