我正在制作直播视频流,但我无法播放视频并获得以下错误
12-19 10:54:38.860: ERROR/MediaPlayer(483): Attempt to call getDuration without a valid mediaplayer
12-19 10:54:38.860: ERROR/MediaPlayer(483): error (-38, 0)
12-19 10:54:38.860: ERROR/MediaPlayer(483): Error (-38,0)
12-19 10:54:38.860: ERROR/MediaPlayer(483): stop called in state 0
12-19 10:54:38.860: ERROR/MediaPlayer(483): error (-38, 0)
12-19 10:54:38.860: ERROR/MediaPlayer(483): Error (-38,0)
12-19 10:54:38.860: ERROR/MediaPlayer(483): stop called in state 0
12-19 10:54:38.860: ERROR/MediaPlayer(483): error (-38, 0)
12-19 10:54:38.860: ERROR/MediaPlayer(483): Error (-38,0)
12-19 10:54:38.860: ERROR/MediaPlayer(483): stop called in state 0
12-19 10:54:38.860: ERROR/MediaPlayer(483): error (-38, 0)
12-19 10:54:38.860: ERROR/MediaPlayer(483): Error (-38,0)
12-19 10:54:38.860: ERROR/MediaPlayer(483): stop called in state 0
12-19 10:54:38.860: ERROR/MediaPlayer(483): error (-38, 0)
12-19 10:54:38.860: ERROR/MediaPlayer(483): Error (-38,0)
12-19 10:54:38.860: ERROR/MediaPlayer(483): stop called in state 0
12-19 10:54:38.860: ERROR/MediaPlayer(483): error (-38, 0)
12-19 10:54:38.860: ERROR/MediaPlayer(483): Error (-38,0)
12-19 10:54:38.860: ERROR/MediaPlayer(483): stop called in state 0
12-19 10:54:38.860: ERROR/MediaPlayer(483): error (-38, 0)
12-19 10:54:38.860: ERROR/MediaPlayer(483): Error (-38,0)
12-19 10:54:38.860: ERROR/MediaPlayer(483): stop called in state 0
12-19 10:54:38.860: ERROR/MediaPlayer(483): error (-38, 0)
12-19 10:54:38.860: ERROR/MediaPlayer(483): Error (-38,0)
我的代码在
之下public class VideoSample extends Activity implements OnSeekBarChangeListener, Callback, OnPreparedListener, OnCompletionListener, OnBufferingUpdateListener,
OnClickListener, OnSeekCompleteListener, AnimationListener {
private TextView textViewPlayed;
private TextView textViewLength;
private SeekBar seekBarProgress;
private SurfaceView surfaceViewFrame;
private ImageView imageViewPauseIndicator;
private MediaPlayer player;
private SurfaceHolder holder;
private ProgressBar progressBarWait;
private Timer updateTimer;
private Bundle extras;
private Animation hideMediaController;
private LinearLayout linearLayoutMediaController;
private static final String TAG = "log_tag";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.videosample);
extras = getIntent().getExtras();
linearLayoutMediaController = (LinearLayout) findViewById(R.id.linearLayoutMediaController);
linearLayoutMediaController.setVisibility(View.GONE);
hideMediaController = AnimationUtils.loadAnimation(this, R.anim.disapearing);
hideMediaController.setAnimationListener(this);
imageViewPauseIndicator = (ImageView) findViewById(R.id.imageViewPauseIndicator);
imageViewPauseIndicator.setVisibility(View.GONE);
if (player != null) {
if (!player.isPlaying()) {
imageViewPauseIndicator.setVisibility(View.VISIBLE);
}
}
textViewPlayed = (TextView) findViewById(R.id.textViewPlayed);
textViewLength = (TextView) findViewById(R.id.textViewLength);
surfaceViewFrame = (SurfaceView) findViewById(R.id.surfaceViewFrame);
surfaceViewFrame.setOnClickListener(this);
surfaceViewFrame.setClickable(false);
seekBarProgress = (SeekBar) findViewById(R.id.seekBarProgress);
seekBarProgress.setOnSeekBarChangeListener(this);
seekBarProgress.setProgress(0);
progressBarWait = (ProgressBar) findViewById(R.id.progressBarWait);
holder = surfaceViewFrame.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
player = new MediaPlayer();
player.setOnPreparedListener(this);
player.setOnCompletionListener(this);
player.setOnBufferingUpdateListener(this);
player.setOnSeekCompleteListener(this);
player.setScreenOnWhilePlaying(true);
player.setDisplay(holder);
}
private void playVideo() {
if (extras.getString("video_path").equals("VIDEO_URI")) {
showToast("Please, set the video URI in HelloAndroidActivity.java in onClick(View v) method");
} else {
new Thread(new Runnable() {
public void run() {
try {
player.setDataSource(VideoSample.this, Uri.parse("rtsp://184.72.239.149/vod/mp4:BigBuckBunny_115k.mov"));
player.prepareAsync();
} catch (IllegalArgumentException e) {
showToast("Error while playing video");
Log.i(TAG, e.getMessage());
e.printStackTrace();
} catch (IllegalStateException e) {
showToast("Error while playing video");
Log.i(TAG, e.getMessage());
e.printStackTrace();
} catch (IOException e) {
showToast("Error while playing video. Please, check your network connection.");
Log.i(TAG, e.getMessage());
e.printStackTrace();
}
}
}).start();
}
}
private void showToast(final String string) {
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(VideoSample.this, string, Toast.LENGTH_LONG).show();
finish();
}
});
}
private void hideMediaController() {
new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(5000);
runOnUiThread(new Runnable() {
public void run() {
linearLayoutMediaController.startAnimation(hideMediaController);
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
Log.i(TAG, "========== onProgressChanged : " + progress + " from user: " + fromUser);
if (!fromUser) {
textViewPlayed.setText(Utils.durationInSecondsToString(progress));
}
}
public void onStartTrackingTouch(SeekBar seekBar) {
}
public void onStopTrackingTouch(SeekBar seekBar) {
if (player.isPlaying()) {
progressBarWait.setVisibility(View.VISIBLE);
player.seekTo(seekBar.getProgress() * 1000);
Log.i(TAG, "========== SeekTo : " + seekBar.getProgress());
}
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
public void surfaceCreated(SurfaceHolder holder) {
playVideo();
}
public void surfaceDestroyed(SurfaceHolder holder) {
}
public void onPrepared(MediaPlayer mp) {
if (!player.isPlaying()) {
player.start();
updateMediaProgress();
linearLayoutMediaController.setVisibility(View.VISIBLE);
hideMediaController();
}
Log.i(TAG, "========== onPrepared ===========");
int duration = mp.getDuration() / 1000; // duration in seconds
seekBarProgress.setMax(duration);
textViewLength.setText(Utils.durationInSecondsToString(duration));
progressBarWait.setVisibility(View.GONE);
// Get the dimensions of the video
int videoWidth = player.getVideoWidth();
int videoHeight = player.getVideoHeight();
float videoProportion = (float) videoWidth / (float) videoHeight;
Log.i(TAG, "VIDEO SIZES: W: " + videoWidth + " H: " + videoHeight + " PROP: " + videoProportion);
// Get the width of the screen
int screenWidth = getWindowManager().getDefaultDisplay().getWidth();
int screenHeight = getWindowManager().getDefaultDisplay().getHeight();
float screenProportion = (float) screenWidth / (float) screenHeight;
Log.i(TAG, "VIDEO SIZES: W: " + screenWidth + " H: " + screenHeight + " PROP: " + screenProportion);
// Get the SurfaceView layout parameters
android.view.ViewGroup.LayoutParams lp = surfaceViewFrame.getLayoutParams();
if (videoProportion > screenProportion) {
lp.width = screenWidth;
lp.height = (int) ((float) screenWidth / videoProportion);
} else {
lp.width = (int) (videoProportion * (float) screenHeight);
lp.height = screenHeight;
}
// Commit the layout parameters
surfaceViewFrame.setLayoutParams(lp);
// Start video
surfaceViewFrame.setClickable(true);
}
public void onCompletion(MediaPlayer mp) {
mp.stop();
if (updateTimer != null) {
updateTimer.cancel();
}
//finish();
}
/**
* Change progress of mediaController
* */
private void updateMediaProgress() {
updateTimer = new Timer("progress Updater");
updateTimer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
seekBarProgress.setProgress(player.getCurrentPosition() / 1000);
}
});
}
}, 0, 1000);
}
public void onBufferingUpdate(MediaPlayer mp, int percent) {
int progress = (int) ((float) mp.getDuration() * ((float) percent / (float) 100));
seekBarProgress.setSecondaryProgress(progress / 1000);
}
public void onClick(View v) {
if (v.getId() == R.id.surfaceViewFrame) {
if (linearLayoutMediaController.getVisibility() == View.GONE) {
linearLayoutMediaController.setVisibility(View.VISIBLE);
hideMediaController();
} else if (player != null) {
if (player.isPlaying()) {
player.pause();
imageViewPauseIndicator.setVisibility(View.VISIBLE);
} else {
player.start();
imageViewPauseIndicator.setVisibility(View.GONE);
}
}
}
}
public void onSeekComplete(MediaPlayer mp) {
progressBarWait.setVisibility(View.GONE);
}
public void onAnimationEnd(Animation animation) {
}
public void onAnimationRepeat(Animation animation) {
}
public void onAnimationStart(Animation animation) {
linearLayoutMediaController.setVisibility(View.GONE);
}
}
答案 0 :(得分:1)
我怀疑在异步准备完成之前,您正在调用onBufferingUpdate
。据我所知,如果准备完成异步并且缓冲尚未填满缓冲区缓存或到达EOS,AwesomePlayer可以在准备完成之前通知您缓冲更新。
这将导致您在MediaPlayer对象处于IDLE状态时调用getDuration
,这是不允许的(请参阅https://developer.android.com/reference/android/media/MediaPlayer.html)。
我的建议是拥有一个最初设置为FALSE的布尔标志,并在获得onPrepared
时更改为TRUE。在此标志为TRUE之前,您不应对要求其处于(至少)准备状态的MediaPlayer对象进行任何调用,例如getDuration
。