ubuntu 在 R40e 上 還有 Debian 在 Sempron 2600 上

2013年7月10日 星期三

OMXPlayer, MediaSanner, MediaPlayer, VideoView...

OMXPlayer is an Interface Only:
-- framework/base/include/media/OMXPlayer.h
class OMXPlayer : public MediaPlayerInterface
{
public:
                        OMXPlayer(int nMediaType = 0);
    virtual             ~OMXPlayer();

    virtual status_t    initCheck();
    virtual status_t    setDataSource(const char *url, const KeyedVector *headers);
    virtual status_t    setDataSource(int fd, int64_t offset, int64_t length);
    virtual status_t    setVideoSurface(const sp& surface);
    virtual status_t    setVideoSurface(const android::sp& surface);
    virtual status_t    setVideoSurfaceTexture(const android::sp& surfaceTexture);
    virtual status_t    prepare();
    virtual status_t    prepareAsync();
    virtual status_t    start();
    virtual status_t    stop();
    virtual status_t    pause();
    virtual bool        isPlaying();
....

實做的大概是: frameworks/base/media/libmediaplayerservice/MediaPlayerService.cpp

..
實際上,這也是個空殼而已,真的 player 是在 prebuild .so 。在 device 下的 propriety/libs 下有一些 _omx_player_..


用較簡單的 Gallery 來看,啟動到最後,是 MovieView.java
這是一個 Activity,onCreate 後就直接播放影片了。

MovieView 只有實做三個 callback:
  • onCreate : new MovieViewControl
  • onPause : call MovieViewControl.onPause
  • onResume : call MovieViewControl.onResume
  • onWindowFocusChanged : call MovieViewControl.onResume/li>

Video 播放直接關聯到 VideoView 這個 layout,
在 res/layout/movie_view.xml 中放入一個 object:
    <VideoView android:id="@+id/surface_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerInParent="true" />

然後在 source:
    private final VideoView mVideoView;
     .....
       mVideoView = (VideoView) rootView.findViewById(R.id.surface_view);
 
取得 VideoView 元件後,就可以對 VideoView 操作:
如果有時作幾個 需要的interface,就可以接著設定:
        mVideoView.setOnErrorListener(this);
        mVideoView.setOnCompletionListener(this);

設定要撥的file (uri),和 control.
        mVideoView.setVideoURI(mUri);
        mMediaController = new MediaController(context);
        mVideoView.setMediaController(mMediaController);
然後,就可以開始播放:
        mVideoView.start();


另外一個是在 development/samples/ApiDemos/src/com/example/android/apis/media/MeduaPlayerDemo_Video.java

還有有關 scanfile 的 sample code,在 development/samples 下也有...
development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java

這一段code:
        File path = Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES);
        File file = new File(path, "DemoPicture.jpg");

        ....
        ....
            // Tell the media scanner about the new file so that it is
            // immediately available to the user.
            MediaScannerConnection.scanFile(this,
                    new String[] { file.toString() }, null,
                    new MediaScannerConnection.OnScanCompletedListener() {
                public void onScanCompleted(String path, Uri uri) {
                    Log.i("ExternalStorage", "Scanned " + path + ":");
                    Log.i("ExternalStorage", "-> uri=" + uri);
                }
            });


所以可以把 file path 丟到這裡scan 轉成 media Uri
?


最後拿 developement/sample 的 MediaPlayerDemo_Video.java 來測試。
原來是用 file path,改成用 scan 後的 Uri。

diff 是:
diff --git a/samples/ApiDemos/src/com/example/android/apis/media/MediaPlayerDemo_Video.java b/samples/ApiDemos/src/com/example/android/apis/media/MediaPlayerDemo_Video.java
index 9853d67..de5a1ac 100644
--- a/samples/ApiDemos/src/com/example/android/apis/media/MediaPlayerDemo_Video.java
+++ b/samples/ApiDemos/src/com/example/android/apis/media/MediaPlayerDemo_Video.java
@@ -30,9 +30,13 @@ import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;
+import android.media.MediaScannerConnection;
+import android.net.Uri;
+import android.content.Context;
public class MediaPlayerDemo_Video extends Activity implements
+ MediaScannerConnection.OnScanCompletedListener,
OnBufferingUpdateListener, OnCompletionListener,
OnPreparedListener, OnVideoSizeChangedListener, SurfaceHolder.Callback {
@@ -71,14 +75,12 @@ public class MediaPlayerDemo_Video extends Activity implements
private void playVideo(Integer Media) {
doCleanUp();
- try {
-
switch (Media) {
case LOCAL_VIDEO:
/*
* TODO: Set the path variable to a local media file path.
*/
- path = "";
+ path = "/mnt/sdcard/udisk1/batman4.mp4";
if (path == "") {
// Tell the user to provide a media file URL.
Toast
@@ -118,9 +120,19 @@ public class MediaPlayerDemo_Video extends Activity implements
}
- // Create a new media player and set the listeners
+ MediaScannerConnection.scanFile(this,new String[] {path}, null,this);
+
+
+ }
+
+ public void onScanCompleted(String path, Uri uri)
+ {
+ Log.i(TAG,"path: " + path);
+ Log.i(TAG,"uri: " + uri);
+ try {
+ // Create a new media player and set the listeners
mMediaPlayer = new MediaPlayer();
- mMediaPlayer.setDataSource(path);
+ mMediaPlayer.setDataSource(getApplicationContext(),uri);
mMediaPlayer.setDisplay(holder);
mMediaPlayer.prepare();
mMediaPlayer.setOnBufferingUpdateListener(this);
@@ -161,7 +173,7 @@ public class MediaPlayerDemo_Video extends Activity implements
public void onPrepared(MediaPlayer mediaplayer) {
Log.d(TAG, "onPrepared called");
mIsVideoReadyToBePlayed = true;
- if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) {
+ if (mIsVideoReadyToBePlayed /*&& mIsVideoSizeKnown*/) {
startVideoPlayback();
}
}
view raw gistfile1.diff hosted with ❤ by GitHub

沒有留言:

標籤

網誌存檔