我正在为在线视频创建一个媒体播放器,但当我尝试运行它时,我收到此错误:“表面已被释放”。这是我的代码:
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
}
}
我提到了this和其他堆栈溢出网站,但没有任何对我有用:-(。错误接近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
答案 0 :(得分:11)
嗨,我终于解决了我的错误。很简单,我在surfacecreated方法下添加了player.setdisplay,如
public void surfaceCreated(SurfaceHolder holder)
{
playVideo();
player.setDisplay(holder);
}
这解决了我的问题。
答案 1 :(得分:3)
问题是你在创建SurfaceView之前将SurfaceHolder设置为MediaPlayer。要么等到Surface View创建,要么在surfaceCreated方法中调用setDisplay方法。