我使用下面的代码从URL播放MP4视频(H.264,AAC编解码器)(网址非常好,没有重定向,404或任何其他内容)。但是,我不断收到错误“尝试在没有有效媒体播放器的情况下调用getduration”或ERROR / MediaPlayer(382):error(1,-2147483648)。有谁知道如何解决它?感谢
VideoView video = (VideoView) findViewById(R.id.myvideo);
Intent videoint=getIntent();
String url = videoint.getStringExtra("url"); //The url pointing to the mp4
video.setVideoPath(url);
video.requestFocus();
video.setMediaController(new MediaController(this));
video.start();
答案 0 :(得分:25)
从onPrepared回调中检索持续时间...这将确保在您尝试获取视频的持续时间之前正确加载视频。
final VideoView video = (VideoView) findViewById(R.id.videoplayer);
final MediaController controller = new MediaController(this);
video.setVideoURI(Uri.parse(getIntent().getStringExtra("url")));
video.setMediaController(controller);
controller.setMediaPlayer(video);
video.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
int duration = video.getDuration();
video.requestFocus();
video.start();
controller.show();
}
});
答案 1 :(得分:1)
确保在设置和准备好后,设置与媒体播放器交互的所有控件。例如:
mediaPlayer.setDataSource(dataSource);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setOnCompletionListener(this);
mediaPlayer.prepare();
progressBar = (ProgressBar) findViewById(R.id.progbar);
progressBar.setVisibility(ProgressBar.VISIBLE);
progressBar.setProgress(0);
progressBar.setMax(mp.getDuration());
mediaPlayer.start();
答案 2 :(得分:0)
就我而言,问题在于搜索栏。在我的服务类中,我改变了:
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();}
public getDur() { return mediaPalyer.getDuration}
到
int dr; //at the top inside Service class
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
dr = player.getDuration();}
public getDur() { return dr}