当前位置:   article > 正文

Android 第二十一章 VideoView_android videoview videoview.getduration() 为-1

android videoview videoview.getduration() 为-1

继承
java.lang.Object
android.view.View
android.view.SurfaceView
android.widget.VideoView

public class MainActivity extends AppCompatActivity {

    private VideoView mVideoView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
    }

    private void initView() {
        mVideoView = findViewById(R.id.videoView);
    }

    private void initData() {


        //播放本地视频
        mVideoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.video));
        mVideoView.start();

        //网络视频
//        mVideoView.setVideoPath("http://xx.mp4");
//        mVideoView.start();


//        常用方法

//        开始
//        mVideoView.start();
//        暂停
//        mVideoView.pause();
//        重播
//        mVideoView.resume();
//        停止
//        mVideoView.stopPlayback();
//        获取文件长度
//        mVideoView.getDuration();
//        获取当前播放位置
//        mVideoView.getCurrentPosition();
//        判断视频是否播放
//        mVideoView.isPlaying();
//        从第几秒开始播放
//        mVideoView.seekTo();
//        视频播放完回调
        mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                Log.d("videoView", "setOnCompletionListener");
                mVideoView.resume();
            }
        });

//        视频播放错误回调
        mVideoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                Log.d("videoView", "setOnErrorListener");
                return false;
            }
        });
//        播放信息回调
        mVideoView.setOnInfoListener(new MediaPlayer.OnInfoListener() {
            @Override
            public boolean onInfo(MediaPlayer mp, int what, int extra) {
                Log.d("videoView", "setOnInfoListener");
                return false;
            }
        });
        //视频加载完成回调
        mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                Log.d("videoView", "setOnPreparedListener");
                mVideoView.seekTo(mVideoView.getDuration() / 2);
            }
        });

    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity">


    <VideoView
        android:id="@+id/videoView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/499331
推荐阅读
相关标签
  

闽ICP备14008679号