MediaPlayer.setLooping(true)
时间: 2023-07-14 08:08:08 浏览: 251
如果你想让MediaPlayer循环播放视频,可以使用`setLooping(true)`方法。这样设置后,当视频播放完毕时,MediaPlayer会自动重新开始播放。
下面是修改后的示例代码:
```java
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class VideoPlayerActivity extends AppCompatActivity implements View.OnClickListener {
private MediaPlayer mediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_player);
Button button1 = findViewById(R.id.button1);
Button button2 = findViewById(R.id.button2);
Button button3 = findViewById(R.id.button3);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
playVideo(R.raw.video1);
break;
case R.id.button2:
playVideo(R.raw.video2);
break;
case R.id.button3:
playVideo(R.raw.video3);
break;
}
}
private void playVideo(int resourceId) {
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
}
mediaPlayer = MediaPlayer.create(this, resourceId);
mediaPlayer.setLooping(true); // 设置循环播放
mediaPlayer.start();
}
}
```
现在当你点击按钮播放视频时,MediaPlayer会循环播放视频直到手动停止。
阅读全文