Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
316 views
in Technique[技术] by (71.8m points)

Android: Video View: how to play a video on a loop

I got a simple dialog box with a VideoView in it and I want to play the video in a loop.

I'm currently using a quick fix

 //Video Loop
        vv.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            public void onCompletion(MediaPlayer mp) {
                vv.start();
            }
        });

but I would like to know if there is a better way?


Edit

I'm adding more code because I don't know how I can get access to the MediaPlayer object form the VideoView:

String path = defaultPath+currentVideoRessource;


    if (path == null || path.length() == 0) {
        Log.e("extra","File URL/path is empty");
    } else {
        // If the path has not changed, just start the media player
        if (!path.equals(current) && mVideoView != null) {
                Uri pathURI = Uri.parse(defaultPath+currentVideoRessource);
                mVideoView.setVideoURI(pathURI);
    }
    current = path;
    mVideoView.setOnCompletionListener(new MyOnCompletionListener(this));
    //Video Loop
    //              mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
    //                  public void onCompletion(MediaPlayer mp) {
    //                      mVideoView.start(); //need to make transition seamless.
    //                  }
    //              });

    mVideoView.start();
    mVideoView.requestFocus();

I'm currently looking into using directly MediaPlayer and a SurfaceView bu I would like to know if there is a way with VideoView directly

question from:https://stackoverflow.com/questions/5569631/android-video-view-how-to-play-a-video-on-a-loop

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use setLooping(true) on your MediaPlayer instance.

--Edit--

How about using setOnPrepareListener instead of setOnCompletionListener? This gives you access to the MediaPlayer object.

vv.setOnPreparedListener (new OnPreparedListener() {                    
    @Override
    public void onPrepared(MediaPlayer mp) {
        mp.setLooping(true);
    }
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...