“表面已经释放”错误

我正在为在线video创build媒体播放器,但是当我尝试运行它时,出现此错误:“表面已经释放”。 这是我的代码:

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 

Solutions Collecting From Web of "“表面已经释放”错误"

嗨,最后我解决了我的错误。 它简单,我添加player.setdisplay像surfacecreate方法

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

这解决了我的问题。

问题是你在创buildSurfaceView之前将SurfaceHolder设置为MediaPlayer。 要么等待表面视图被创build,要么在surfaceCreated方法内调用setDisplay方法。