我的xml文件:
<SurfaceView
android:id="@+id/surfaceView"
android:layout_marginTop="50dp"
android:layout_width="fill_parent"
android:layout_height="300dp" />
我的setDisplay功能:
public void playVideo() {
MediaPlayer mp = new MediaPlayer();
SurfaceView sv = (SurfaceView) this.findViewById(R.id.surfaceView);
try {
mp.setDataSource("sdcard/test/a.3gp");
SurfaceHolder sh = sv.getHolder();
mp.setDisplay(sh);***----the exception occured here***
mp.prepare();
mp.start();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
日志如下:
04-24 22:19:33.645: W/System.err(16106): java.lang.IllegalArgumentException: The surface has been released
04-24 22:19:33.645: W/System.err(16106): at android.media.MediaPlayer._setVideoSurface(Native Method)
04-24 22:19:33.645: W/System.err(16106): at android.media.MediaPlayer.setDisplay(MediaPlayer.java:698)
我在这里发现了一些类似的问题,但所有这些都不适合我。等待你的答案。非常感谢。
答案 0 :(得分:13)
表面可以被破坏。这就是为什么你需要将public void surfaceDestroyed(SurfaceHolder holder)
添加到SurfaceView的实现中,如下所示:
@Override public void surfaceDestroyed(SurfaceHolder holder) { synchronized (this) { hasActiveHolder = false; synchronized(this) { this.notifyAll(); } } }
您还应该添加一个处理Surface创建的函数:
@Override public void surfaceCreated(SurfaceHolder holder) { synchronized (this) { hasActiveHolder = true; this.notifyAll() } }
以这种方式修改自己的功能:
mp.setDataSource("sdcard/test/a.3gp");
SurfaceHolder sh = sv.getHolder();
synchronized (this) {
while (!hasActiveHolder) {
try {
this.wait();
} catch (InterruptedException e) {
//Print something
}
}
mp.setDisplay(sh);
mp.prepare();
}
您还有另一个选项,就是Google建议您使用SurfaceView的方式:在一个单独的主题中。
答案 1 :(得分:10)
它与执行顺序有关,因为在设置MediaPlayer
的显示之前必须先创建表面,所以你必须将回调方法surfaceCreated
覆盖到以下内容:
@Override
public void surfaceCreated(SurfaceHolder holder) {
mp.setDisplay(sh); // now "mp" is defined as a class variable
}
现在你的play方法中没有setDisplay:
private MediaPlayer mp; // to use it inside surfaceCreated callback method
public void playVideo() {
mp = new MediaPlayer();
SurfaceView sv = (SurfaceView) this.findViewById(R.id.surfaceView);
try {
mp.setDataSource("sdcard/test/a.3gp");
SurfaceHolder sh = sv.getHolder();
mp.prepare();
mp.start();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
答案 2 :(得分:3)
使用SurfaceHolder.Callback,如下所示
SurfaceView mSurfaceView = (SurfaceView) findViewById(R.id.surfaceView);
SurfaceHolder holder = mSurfaceView.getHolder();
final MediaPlayer player = new MediaPlayer();
holder.addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
player.setDisplay(holder);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
});
String UrlPath="android.resource://"+getActivity().getPackageName()+"/"+R.raw.your_file_name_without_extension;
try {
player.setDataSource(getActivity(),Uri.parse(UrlPath));
player.prepareAsync();
} catch (IOException e) {
e.printStackTrace();
}
player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
答案 3 :(得分:2)
最简单的方法就是在setDisplay
中调用surfaceCreated
:
@Override
public void surfaceCreated(SurfaceHolder holder) {
mp.setDisplay(holder)
}
并且不要忘记取消绑定表面:
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
mp.setDisplay(null);
}
注意:媒体播放器之前应在某处初始化,例如onCreate
。
答案 4 :(得分:1)
对于仍有问题的人,请尝试在您的activity / fragment / etc中实现 SurfaceHolder.Callback ,并在 onCreate / onCreateView 方法中调用 addCallback( SurfaceHolder.Callback回调)方法,使用您的activity / fragment / etc作为参数回调。
答案 5 :(得分:0)
我们可以查看VideoView源代码:
SurfaceHolder.Callback mSHCallback = new SurfaceHolder.Callback(){
...
public void surfaceCreated(SurfaceHolder holder)
{
mSurfaceHolder = holder;
openVideo();
}
private void openVideo() {
...
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDisplay(mSurfaceHolder);
}
}
因此,我们可以了解应该在mediaplayer.setDisplay()
方法中使用surfaceCreate
。