0

从 /samples/android-16 >/ApiDemos 上传了一个示例。一个项目中有几个例子。从那里提取一个-MediaPlayerDemo_Video,像这样:

Mediaplayer_video.java

package com.example.mediaplayer_video;
    import android.app.Activity;
    import android.media.AudioManager;
    import android.media.MediaPlayer;
    import android.media.MediaPlayer.OnBufferingUpdateListener;
    import android.media.MediaPlayer.OnCompletionListener;
    import android.media.MediaPlayer.OnPreparedListener;
    import android.media.MediaPlayer.OnVideoSizeChangedListener;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.SurfaceHolder;
    import android.view.SurfaceView;
              import android.widget.Toast;


    public class Mediaplayer_video extends Activity implements
            OnBufferingUpdateListener, OnCompletionListener,
            OnPreparedListener, OnVideoSizeChangedListener, SurfaceHolder.Callback {

        private static final String TAG = "MediaPlayerVideo";
        private int mVideoWidth;
        private int mVideoHeight;
        private MediaPlayer mMediaPlayer;
        private SurfaceView mPreview;
        private SurfaceHolder holder;
        private String path;
        private Bundle extras;
        private static final String MEDIA = "media";
        private boolean mIsVideoSizeKnown = false;
        private boolean mIsVideoReadyToBePlayed = false;

        /**
         * 
         * Called when the activity is first created.
         */

        @SuppressWarnings("deprecation")
        @Override
        public void onCreate(Bundle icicle) {
            super.onCreate(icicle);
            setContentView(R.layout.mediaplayer_2);
            mPreview = (SurfaceView) findViewById(R.id.surface);
            holder = mPreview.getHolder();
            holder.addCallback(this);
            holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
            extras = getIntent().getExtras();

        }

        private void playVideo(Integer Media) {
            doCleanUp();
            try {

                        path = "sdcard/scheen.mp4";
                        if (path == "") {
                            // Tell the user to provide a media file URL.
                            Toast
                                    .makeText(
                                            Mediaplayer_video.this,
                                            "Please edit Mediaplayer_Video Activity, "
                                                    + "and set the path variable to your media file path."
                                                    + " Your media file must be stored on sdcard.",
                                            Toast.LENGTH_LONG).show();

                        }

                // Create a new media player and set the listeners
                mMediaPlayer = new MediaPlayer();
                mMediaPlayer.setDataSource(path);
                mMediaPlayer.setDisplay(holder);
                mMediaPlayer.prepare();
                mMediaPlayer.setOnBufferingUpdateListener(this);
                mMediaPlayer.setOnCompletionListener(this);
                mMediaPlayer.setOnPreparedListener(this);
                mMediaPlayer.setOnVideoSizeChangedListener(this);
                mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

            } catch (Exception e) {
                Log.e(TAG, "error: " + e.getMessage(), e);
            }
        }

        public void onBufferingUpdate(MediaPlayer arg0, int percent) {
            Log.d(TAG, "onBufferingUpdate percent:" + percent);

        }

        public void onCompletion(MediaPlayer arg0) {
            Log.d(TAG, "onCompletion called");
        }

        public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
            Log.v(TAG, "onVideoSizeChanged called");
            if (width == 0 || height == 0) {
                Log.e(TAG, "invalid video width(" + width + ") or height(" + height + ")");
                return;
            }
            mIsVideoSizeKnown = true;
            mVideoWidth = width;
            mVideoHeight = height;
            if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) {
                startVideoPlayback();
            }
        }

        public void onPrepared(MediaPlayer mediaplayer) {
            Log.d(TAG, "onPrepared called");
            mIsVideoReadyToBePlayed = true;
            if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) {
                startVideoPlayback();
            }
        }

        public void surfaceChanged(SurfaceHolder surfaceholder, int i, int j, int k) {
            Log.d(TAG, "surfaceChanged called");

        }

        public void surfaceDestroyed(SurfaceHolder surfaceholder) {
            Log.d(TAG, "surfaceDestroyed called");
        }


        public void surfaceCreated(SurfaceHolder holder) {
            Log.d(TAG, "surfaceCreated called");
            playVideo(extras.getInt(MEDIA));

        }

        @Override
        protected void onPause() {
            super.onPause();
            releaseMediaPlayer();
            doCleanUp();
        }

        @Override
        protected void onDestroy() {
            super.onDestroy();
            releaseMediaPlayer();
            doCleanUp();
        }

        private void releaseMediaPlayer() {
            if (mMediaPlayer != null) {
                mMediaPlayer.release();
                mMediaPlayer = null;
            }
        }

        private void doCleanUp() {
            mVideoWidth = 0;
            mVideoHeight = 0;
            mIsVideoReadyToBePlayed = false;
            mIsVideoSizeKnown = false;
        }

        private void startVideoPlayback() {
            Log.v(TAG, "startVideoPlayback");
            holder.setFixedSize(mVideoWidth, mVideoHeight);
            mMediaPlayer.start();
        }
}

mediaplayer_2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".Mediaplayer_video" >
    <SurfaceView
        android:id="@+id/surface"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" >
    </SurfaceView>
</LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.mediaplayer_video"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="16" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.mediaplayer_video.Mediaplayer_video"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

带有此徽标的瀑布:

07-20 17:54:04.597: E/Trace(5713): error opening trace file: No such file or directory (2)
07-20 17:54:07.006: D/MediaPlayerVideo(5713): surfaceCreated called
07-20 17:54:07.106: D/AndroidRuntime(5713): Shutting down VM
07-20 17:54:07.106: W/dalvikvm(5713): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
07-20 17:54:07.236: E/AndroidRuntime(5713): FATAL EXCEPTION: main
07-20 17:54:07.236: E/AndroidRuntime(5713): java.lang.NullPointerException
07-20 17:54:07.236: E/AndroidRuntime(5713):     at com.example.mediaplayer_video.Mediaplayer_video.surfaceCreated(Mediaplayer_video.java:128)
07-20 17:54:07.236: E/AndroidRuntime(5713):     at android.view.SurfaceView.updateWindow(SurfaceView.java:543)
07-20 17:54:07.236: E/AndroidRuntime(5713):     at android.view.SurfaceView.access$000(SurfaceView.java:81)
07-20 17:54:07.236: E/AndroidRuntime(5713):     at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:169)
07-20 17:54:07.236: E/AndroidRuntime(5713):     at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:671)
07-20 17:54:07.236: E/AndroidRuntime(5713):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1820)
07-20 17:54:07.236: E/AndroidRuntime(5713):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
07-20 17:54:07.236: E/AndroidRuntime(5713):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4214)
07-20 17:54:07.236: E/AndroidRuntime(5713):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
07-20 17:54:07.236: E/AndroidRuntime(5713):     at android.view.Choreographer.doCallbacks(Choreographer.java:555)
07-20 17:54:07.236: E/AndroidRuntime(5713):     at android.view.Choreographer.doFrame(Choreographer.java:525)
07-20 17:54:07.236: E/AndroidRuntime(5713):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
07-20 17:54:07.236: E/AndroidRuntime(5713):     at android.os.Handler.handleCallback(Handler.java:615)
07-20 17:54:07.236: E/AndroidRuntime(5713):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-20 17:54:07.236: E/AndroidRuntime(5713):     at android.os.Looper.loop(Looper.java:137)
07-20 17:54:07.236: E/AndroidRuntime(5713):     at android.app.ActivityThread.main(ActivityThread.java:4745)
07-20 17:54:07.236: E/AndroidRuntime(5713):     at java.lang.reflect.Method.invokeNative(Native Method)
07-20 17:54:07.236: E/AndroidRuntime(5713):     at java.lang.reflect.Method.invoke(Method.java:511)
07-20 17:54:07.236: E/AndroidRuntime(5713):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-20 17:54:07.236: E/AndroidRuntime(5713):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-20 17:54:07.236: E/AndroidRuntime(5713):     at dalvik.system.NativeStart.main(Native Method)

有想法,怎么了?

4

1 回答 1

0

我发现了我的错误。日志指示第 128 行:

07-20 17:54:07.236: E/AndroidRuntime(5713): java.lang.NullPointerException 
07-20 17:54:07.236: E/AndroidRuntime(5713): at com.example.mediaplayer_video.Mediaplayer_video.surfaceCreated(Mediaplayer_video.java:128)

在第 128 行 Mediaplayer_video.java 这个:

    public void surfaceCreated(SurfaceHolder holder) {
        Log.d(TAG, "surfaceCreated called");
        playVideo(extras.getInt(MEDIA));

PlayVideo() 方法定义为 playVideo(Integer Media),并且 Media = null。

修复了 playVideo() 上的 playVideo(Integer Media) 和 playVideo(extras.getInt(MEDIA)) 以及获得的所有内容。

于 2013-07-21T11:59:21.233 回答