Android VideoView中getDuration()方法使用问题

Android VideoView中getDuration()方法使用问题:
调用videoView.getDuration()方法返回值为-1
VideoView.start()后调用videoView.getDuration()方法返回值也为-1,

视频开始播放后调用videoView.getDuration()方法返回值为视频的真实“长度”

看源码后发现里面会先去检查一个mCurrentState的值,然后这个变量只有OnPreparedListener的回调中才设置为正确值.
因此可知在调用setVideoPath之后,VideoView里的MediaPlayer还未处于prepared状态,因此得不到duration.
所以只要videoView.setOnPreparedListener()方法在该监听器中去获取值就行了.

发布了13 篇原创文章 · 获赞 11 · 访问量 2万+
展开阅读全文

拖动seekbar后获取的seekbar.getProgress()值有一些误差如何解决

01-17

private void setSeerBar() { mMax = mediaPlayer.getDuration(); seekbar.setMax(mMax); seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(final SeekBar seekBar, int progress, boolean fromUser) { currentProgress_seek = seekbar.getProgress(); currentProgress_media = mediaPlayer.getCurrentPosition(); long timelong = currentProgress_seek / 1000; hour_ = timelong / 60 / 60; minute_ = (timelong / 60) % 60; second_ = timelong % 60; Log.e("nowtime", "currenttime:" + hour_ + ":" + minute_ + ":" + second_); } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { if (mediaPlayer != null) { mMax = mediaPlayer.getDuration(); mediaPlayer.seekTo(currentProgress_seek); currentProgress_media = mediaPlayer.getCurrentPosition(); currentProgress_seek = seekbar.getProgress(); long timelong = currentProgress_media / 1000; hour_ = timelong / 60 / 60; minute_ = (timelong / 60) % 60; second_ = timelong % 60; Log.e("nowtime", "stoptime:" + hour_ + ":" + minute_ + ":" + second_); Log.e("SeekBarState", "stopprovess" + seekbar.getProgress()); Message message = new Message(); handler.sendEmptyMessage(message.what); } } }); } 然后打印出来的结果是: currenttime:0:49:14 01-11 11:55:54.687 10778-10778/com.example.bmvod.film E/nowtime: currenttime:0:49:15 01-11 11:55:55.691 10778-10778/com.example.bmvod.film E/nowtime: currenttime:0:49:16 01-11 11:55:56.120 10778-10778/com.example.bmvod.film E/nowtime: currenttime:0:42:15 01-11 11:55:56.213 10778-10778/com.example.bmvod.film E/nowtime: stoptime:0:42:15 01-11 11:55:56.688 10778-10778/com.example.bmvod.film E/nowtime: currenttime:0:42:10 01-11 11:55:57.688 10778-10778/com.example.bmvod.film E/nowtime: currenttime:0:42:11 01-11 11:55:58.687 10778-10778/com.example.bmvod.film E/nowtime: currenttime:0:42:12 01-11 11:55:59.689 10778-10778/com.example.bmvod.film E/nowtime: currenttime:0:42:13 这样的,, 停止的时间是42分15秒,然后下一个打印应该是42分16秒,但是打印结果是42分10秒 这个误差是如何产生的?怎么解决呢?。。 问答

结束activity出现MediaPlayer异常

02-22

在结束mediaplayer activity时出现异常,代码: 结束按钮: ImageView imageAllBack = (ImageView)this.findViewById(R.id.imageAllBack); imageAllBack.setOnClickListener(new OnClickListener() { public void onClick(View v) { //mediaPlayer.stop(); if(mediaPlayer != null) { mediaPlayer.stop(); mediaPlayer.release(); } finish(); } }); mediaPlayer事件: @Override protected void onStop() { super.onStop(); mediaPlayer.stop(); mediaPlayer.release(); } @Override public boolean onTouchEvent(MotionEvent event) { mediaController.show(); return false; } //--MediaPlayerControl methods---------------------------------------------------- public void start() { mediaPlayer.start(); } public void pause() { mediaPlayer.pause(); } public int getDuration() { return mediaPlayer.getDuration(); } public int getCurrentPosition() { return mediaPlayer.getCurrentPosition(); } public void seekTo(int i) { mediaPlayer.seekTo(i); } public boolean isPlaying() { return mediaPlayer.isPlaying(); } public int getBufferPercentage() { return 0; } public boolean canPause() { return true; } public boolean canSeekBackward() { return true; } public boolean canSeekForward() { return true; } //-------------------------------------------------------------------------------- public void onPrepared(MediaPlayer mediaPlayer) { mediaController.setMediaPlayer(this); mediaController.setAnchorView(findViewById(R.id.main_audio_view)); handler.post(new Runnable() { public void run() { mediaController.setEnabled(true); mediaController.show(); } }); } public class MyMediaController extends MediaController { public MyMediaController(Context context) { super(context); // TODO Auto-generated constructor stub } @Override public void hide() { // Do Nothing to keep the show the controller all times } } 异常: 02-21 21:03:33.829: E/AndroidRuntime(8889): FATAL EXCEPTION: main 02-21 21:03:33.829: E/AndroidRuntime(8889): java.lang.IllegalStateException 02-21 21:03:33.829: E/AndroidRuntime(8889): at android.media.MediaPlayer.isPlaying(Native Method) 02-21 21:03:33.829: E/AndroidRuntime(8889): at rce.android.naturalocal.NaturaPlayer.isPlaying(NaturaPlayer.java:126) 02-21 21:03:33.829: E/AndroidRuntime(8889): at rce.android.naturalocal.NaturaPlayer.getCurrentPosition(NaturaPlayer.java:115) 02-21 21:03:33.829: E/AndroidRuntime(8889): at android.widget.MediaController.setProgress(MediaController.java:436) 02-21 21:03:33.829: E/AndroidRuntime(8889): at android.widget.MediaController.access$500(MediaController.java:74) 02-21 21:03:33.829: E/AndroidRuntime(8889): at android.widget.MediaController$3.handleMessage(MediaController.java:407) 02-21 21:03:33.829: E/AndroidRuntime(8889): at android.os.Handler.dispatchMessage(Handler.java:99) 02-21 21:03:33.829: E/AndroidRuntime(8889): at android.os.Looper.loop(Looper.java:137) 02-21 21:03:33.829: E/AndroidRuntime(8889): at android.app.ActivityThread.main(ActivityThread.java:4441) 02-21 21:03:33.829: E/AndroidRuntime(8889): at java.lang.reflect.Method.invokeNative(Native Method) 02-21 21:03:33.829: E/AndroidRuntime(8889): at java.lang.reflect.Method.invoke(Method.java:511) 02-21 21:03:33.829: E/AndroidRuntime(8889): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 02-21 21:03:33.829: E/AndroidRuntime(8889): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 02-21 21:03:33.829: E/AndroidRuntime(8889): at dalvik.system.NativeStart.main(Native Method) 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览