3

假设我们有以下内容(来自 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: The surface has been released

代码如下:

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

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

mMediaPlayer.setDisplay(holder);
4

3 回答 3

2

尝试这个:

    @Override
public void surfaceCreated(SurfaceHolder holder) {
    System.out.println("===== * surfaceCreated * =====");
    if(mMediaPlayer == null)
        mMediaPlayer = new MediaPlayer();
    mMediaPlayer .setDisplay(holder);
}
于 2013-02-14T11:33:13.163 回答
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();
   }
于 2012-04-08T05:17:45.190 回答
0

我遇到了这个问题,并通过将我的发布功能 visible=gone 添加到 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);}
                }
于 2014-08-21T13:48:52.380 回答