如何在MainActivity.java中循环VideoView元素?

时间:2019-04-02 09:30:03

标签: java android android-videoview

我编写了一个播放视频的应用程序,直到您点击屏幕并退出。基本的屏幕保护程序。 该应用程序将启动并播放视频,但会在最后一帧而不是循环播放时停止播放。

我的MainActivity.java下面的代码

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.VideoView;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        VideoView videoView = findViewById(R.id.videoView);
        Uri uri=Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.hab2);
        videoView.setVideoURI(uri);
        videoView.requestFocus();

        videoView.start();
        videoView.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                finish();
            }
        });    }
}

2 个答案:

答案 0 :(得分:0)

使用MediaPlayer可以轻松处理

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

答案 1 :(得分:0)

如果要在完成后开始播放视频,请在setOnCompletionListener上使用VideoView

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);

    final VideoView videoView = findViewById(R.id.videoView);

    Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.hab2);

    videoView.setVideoURI(uri);
    videoView.requestFocus();

    // TODO: Add these lines of code.
    videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            // TODO: When the video is completed, we will star it again.
            videoView.start();
        }
    });

    videoView.start();

    videoView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });
}