可以在MediaPlayer.release()之后重用SurfaceView吗?

时间:2012-04-08 02:55:19

标签: android media-player

假设我们有以下内容(来自Android ApiDemos):

<?xml version="1.0" encoding="utf-8"?>

<SurfaceView android:id="@+id/surface"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center">
</SurfaceView>

在MediaPlayer使用SurfaceView并调用其release()之后,可以重用SurfaceView吗? 我试图创建一个新的MediaPlayer实例来重用SurfaceView,但是setDisplay()会出现以下错误:  java.lang.IllegalArgumentException:表面已被释放

代码如下:

    mPreview = (SurfaceView) findViewById(R.id.surface);
holder = mPreview.getHolder();
holder.addCallback(this);

mMediaPlayer = new MediaPlayer();
MediaPlayer.setDataSource(url);

mMediaPlayer.setDisplay(holder);

3 个答案:

答案 0 :(得分:2)

试试这个:

    @Override
public void surfaceCreated(SurfaceHolder holder) {
    System.out.println("===== * surfaceCreated * =====");
    if(mMediaPlayer == null)
        mMediaPlayer = new MediaPlayer();
    mMediaPlayer .setDisplay(holder);
}

答案 1 :(得分:0)

试试这个:

mPreview  = (SurfaceView) findViewById(R.id.PlaySurfaceView);
surfaceHolder = mPreview.getHolder();
surfaceHolder.addCallback(this);
try {
 mMediaPlayer = new MediaPlayer();
 mMediaPlayer.setDataSource(url);  
 mMediaPlayer.setDisplay(holder); 
 } catch (IllegalArgumentException e1) {
     e1.printStackTrace();
   } catch (SecurityException e1) {
     e1.printStackTrace();
   } catch (IllegalStateException e1) {
     e1.printStackTrace();
   } catch (IOException e1) {
     e1.printStackTrace();
   }

答案 2 :(得分:0)

我遇到了这个问题并通过添加我的发布功能visible =去了surfaceview:

解决了这个问题
  public void release() {
    if (mMediaPlayer != null) {
    setVisibility(View.GONE);
      mMediaPlayer.reset();
      mMediaPlayer.release();
      mMediaPlayer = null;
      mCurrentState = STATE_IDLE;}
  }

并在onprepared函数中设置visible = visible:

   videoView.setOnPreparedListener(new OnPreparedListener() {
            public void onPrepared(MediaPlayer mp) {
                audio=false; video=false; int ty=mp.getTrackInfo().length;
                for (int i=0; i<ty;i++)
                {
                    if (mp.getAudioTrack()>-1) {audio=true;}
                    if (mp.getVideoTrack()>-1) {video=true;}
                }

                if (((audio==false)&&(skip==true))||((video==false)&&(skip2==true))||((video==true)&&(skip4==true))) 
                {   notifybar("...");
                    nexttr();} else {
                if (vis==true) {
                if (video==false) {
                    if (mVisualizerView.getVisibility()!=View.VISIBLE) {mVisualizerView.setVisibility(View.VISIBLE);}
                        mVisualizerView.link(videoView.getAudioSessionId());
                        vis2=true;
                        } else if (vis2==true){
                       mVisualizerView.release();
                       mVisualizerView.setVisibility(View.GONE);
                      vis2=false;
                        }}
                //this
                if (video==true) {
                if (videoView.getVisibility()!=View.VISIBLE) {videoView.setVisibility(View.VISIBLE);}
                } else {if (videoView.getVisibility()!=View.INVISIBLE) {videoView.setVisibility(View.INVISIBLE);}
                }