10

オンライン ビデオ用のメディア プレーヤーを作成していますが、実行しようとすると、「サーフェスが解放されました」というエラーが表示されます。これが私のコードです:

public class VideoSample1 extends Activity implements  Callback, 
    OnPreparedListener, OnCompletionListener, 
    OnClickListener, OnSeekCompleteListener, android.view.SurfaceHolder.Callback
    {
        public String video_path = "My video URL";
        private SurfaceView surfaceViewFrame;
        private MediaPlayer player;
        private SurfaceHolder holder;
        private Bundle extras;
        private static final String TAG = "log_tag";
        private boolean b =false;

        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.customvideoview);
            extras = getIntent().getExtras();

            surfaceViewFrame = (SurfaceView) findViewById(R.id.surfaceViewFrame);
            surfaceViewFrame.setOnClickListener(this);
            surfaceViewFrame.setClickable(false);

            holder = surfaceViewFrame.getHolder();
            holder.addCallback(this);
            holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

            player = new MediaPlayer();
            player.setOnPreparedListener(this);
            player.setOnCompletionListener(this);

            player.setOnSeekCompleteListener(this);
            player.setScreenOnWhilePlaying(true);
            player.setDisplay(holder);
        }
        @Override
        protected void onDestroy()
        {
            super.onDestroy();
            player.stop();
            player.release();
            player = null;
            Toast.makeText(VideoSample1.this, "back",Toast.LENGTH_SHORT).show();
            finish();
        }
        private void playVideo()
        {

            new Thread(new Runnable() 
            {
                public void run() 
                {
                    try
                    {

                        player.setDataSource(VideoSample1.this, Uri.parse(extras.getString("Video URL")));
                        player.prepareAsync();
                    }
                    catch (IllegalArgumentException e) 
                    {
                        Log.d("admin","Error while playing video");
                        e.printStackTrace();
                        Log.i(TAG,"tag"+ e.getMessage());
                    } 
                    catch (IllegalStateException e) 
                    {
                        Log.d("admin","Error1 while playing video");
                        e.printStackTrace();
                        Log.i(TAG, "tag"+e.getMessage());
                    } 
                    catch (IOException e) 
                    {
                        e.printStackTrace();
                        Log.d("admin","Error while playing video.Please, check your network connection");
                        Log.i(TAG, "tag"+e.getLocalizedMessage());
                    }
                }
            }).start();
        }


        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
        {

        }

        public void surfaceCreated(SurfaceHolder holder) 
        {
            playVideo();
        }

        public void surfaceDestroyed(SurfaceHolder holder) 
        {

        }

        public void onPrepared(MediaPlayer mp)
        {
            if (!player.isPlaying())
            {
                b = true;
                player.start();
            }

        }

        public void onCompletion(MediaPlayer mp)
        {
            mp.stop();

            finish();
        }


        public void onSeekComplete(MediaPlayer mp)
        {

        }
        @Override
        public void invalidateDrawable(Drawable who) 
        {

        }

        @Override
        public void scheduleDrawable(Drawable who, Runnable what, long when)
        {
    // TODO Auto-generated method stub

        }

        @Override
        public void unscheduleDrawable(Drawable who, Runnable what) 
        {
    // TODO Auto-generated method stub

        }


        @Override
        public void onClick(View v)
        {
    // TODO Auto-generated method stub

        }

}

私はこれを参照し、他のスタックオーバーフローサイトを参照しましたが、何も機能しませんでした:-(。エラーは setdisplay(holder) の近くにあります。そして、ここに私の Logcat メッセージがあります:

08-31 10:18:01.512: E/AndroidRuntime(1162): FATAL EXCEPTION: main
08-31 10:18:01.512: E/AndroidRuntime(1162): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.msense.msenseplayer/com.msense.msenseplayer.VideoSample1}: java.lang.IllegalArgumentException: The surface has been released
08-31 10:18:01.512: E/AndroidRuntime(1162):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
08-31 10:18:01.512: E/AndroidRuntime(1162):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
08-31 10:18:01.512: E/AndroidRuntime(1162):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
08-31 10:18:01.512: E/AndroidRuntime(1162):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
08-31 10:18:01.512: E/AndroidRuntime(1162):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-31 10:18:01.512: E/AndroidRuntime(1162):     at android.os.Looper.loop(Looper.java:137)
08-31 10:18:01.512: E/AndroidRuntime(1162):     at android.app.ActivityThread.main(ActivityThread.java:4429)
08-31 10:18:01.512: E/AndroidRuntime(1162):     at java.lang.reflect.Method.invokeNative(Native Method)
08-31 10:18:01.512: E/AndroidRuntime(1162):     at java.lang.reflect.Method.invoke(Method.java:511)
08-31 10:18:01.512: E/AndroidRuntime(1162):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
08-31 10:18:01.512: E/AndroidRuntime(1162):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
08-31 10:18:01.512: E/AndroidRuntime(1162):     at dalvik.system.NativeStart.main(Native Method)
08-31 10:18:01.512: E/AndroidRuntime(1162): Caused by: java.lang.IllegalArgumentException: The surface has been released
08-31 10:18:01.512: E/AndroidRuntime(1162):     at android.media.MediaPlayer._setVideoSurface(Native Method)
08-31 10:18:01.512: E/AndroidRuntime(1162):     at android.media.MediaPlayer.setDisplay(MediaPlayer.java:641)
08-31 10:18:01.512: E/AndroidRuntime(1162):     at com.msense.msenseplayer.VideoSample1.onCreate(VideoSample1.java:53)
08-31 10:18:01.512: E/AndroidRuntime(1162):     at android.app.Activity.performCreate(Activity.java:4578)
08-31 10:18:01.512: E/AndroidRuntime(1162):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
08-31 10:18:01.512: E/AndroidRuntime(1162):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
08-31 10:18:01.512: E/AndroidRuntime(1162):     ... 11 more
4

2 に答える 2

11

こんにちはついに私は私のエラーを解決しました。次のような surfacecreated メソッドの下に player.setdisplay を追加したのは簡単です

public void surfaceCreated(SurfaceHolder holder) 
{ 
playVideo(); 
player.setDisplay(holder); 
} 

これで私の問題は解決しました。

于 2013-09-03T11:16:18.723 に答える
3

問題は、SurfaceView を作成する前に、SurfaceHolder を MediaPlayer に設定していることです。Surface View が作成されるまで待つか、surfaceCreated メソッド内で setDisplay メソッドを呼び出します。

于 2013-10-01T08:11:18.407 に答える