我已经在 android 中编写了视频播放器代码。但是我面临的一个问题是当我调试我的代码时视频可以正常播放但是当我运行它时,视频不会出现,只能听到声音。
如有任何帮助,我们将不胜感激。
这是我的视频播放器的主要方法。
private void playVideo(String url)
{
try
{
media.setEnabled(false);
if (player==null)
{
player=new MediaPlayer();
player.setScreenOnWhilePlaying(true);
//player.reset();
}
else
{
player.stop();
player.reset();
}
player.setOnErrorListener(this);
player.setDataSource(url);
player.setDisplay(holder);
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setOnPreparedListener(this);
player.prepareAsync();
player.setOnBufferingUpdateListener(this);
player.setOnCompletionListener(this);
}
catch(IllegalStateException ex)
{
try
{
player.reset();
player.setOnErrorListener(this);
player.setDataSource(url);
player.setDisplay(holder);
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setOnPreparedListener(this);
player.prepareAsync();
player.setOnBufferingUpdateListener(this);
player.setOnCompletionListener(this);
}
catch(Exception ex1)
{
ex1.printStackTrace();
}
}
catch (Throwable t)
{
Log.e(TAG, "Exception in media prep", t);
goBlooey(t);
}
}
最佳答案
如果播放音频但不播放视频,您可能没有正确设置表面支架,如 MediaPlayer 文档所示:
public void setDisplay (SurfaceHolder sh)
Sets the SurfaceHolder to use for displaying the video portion of the media. This call is optional. Not calling it when playing back a video will result in only the audio track being played.
Parameters sh - the SurfaceHolder to use for video display
也许您需要在 onCreate() 中执行如下操作:
mPreview = (SurfaceView) findViewById(R.id.surface);
holder = mPreview.getHolder();
关于安卓视频播放器问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3377690/