gpt4 book ai didi

java - Android MediaPlayer 重叠运行

转载 作者:行者123 更新时间:2023-12-01 16:14:33 36 4
gpt4 key购买 nike

感谢阅读这篇文章,我开发的应用程序功能之一有问题,即我在原始文件夹中运行的 MediaPlayer。当我尝试同时播放 2 个 MediaPlayer 时,就会出现问题,当另一个 MediaPlayer 运行时,另一个 MediaPlayer 应该自动停止,但即使退出时也不会。 Activity ,MediaPlayer 仍在运行,是否有与此相关的解决方案。这是我的代码

    MediaPlayer mediaPlayer1,mediaPlayer2,mediaPlayer3;
ImageButton btn1, btn2, btn3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hukum_ra_tarqiq);

btn1 = findViewById(R.id.play_toggle_ratarqiq1);
btn1.setOnClickListener(this);
mediaPlayer1 = MediaPlayer.create(this,R.raw.madtarqiqc1);
btn2 = findViewById(R.id.play_toggle_ratarqiq2);
btn2.setOnClickListener(this);
mediaPlayer2 = MediaPlayer.create(this,R.raw.madtarqiqc2);
btn3 = findViewById(R.id.play_toggle_ratarqiq3);
btn3.setOnClickListener(this);
mediaPlayer3 = MediaPlayer.create(this,R.raw.madtarqiqc3);

}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.play_toggle_ratarqiq1:
setmpFirst();
break;
case R.id.play_toggle_ratarqiq2:
setmpSecond();
break;
case R.id.play_toggle_ratarqiq3:
setmpThird();
break;
}
}
private void setmpFirst(){
if (mediaPlayer1.isPlaying()){
if (mediaPlayer1 != null){
mediaPlayer1.pause();
if (isFinishing()){
mediaPlayer1.stop();
mediaPlayer1.release();
}
}
btn1.setImageResource(R.drawable.ic_pause_circle_outline_black_24dp);
}else {
mediaPlayer1.start();
btn1.setImageResource(R.drawable.ic_play_circle_outline_black_24dp);
}
}

private void setmpSecond(){
if (mediaPlayer2.isPlaying()){
if (mediaPlayer2 != null){
mediaPlayer2.pause();
if (isFinishing()){
mediaPlayer2.stop();
mediaPlayer2.release();
}
}
btn2.setImageResource(R.drawable.ic_pause_circle_outline_black_24dp);
}
else {
mediaPlayer2.start();
btn2.setImageResource(R.drawable.ic_play_circle_outline_black_24dp);
}
}

private void setmpThird(){
if (mediaPlayer3.isPlaying()){
if (mediaPlayer3 != null){
mediaPlayer3.pause();
if (isFinishing()){
mediaPlayer3.stop();
mediaPlayer3.release();
}
}
btn3.setImageResource(R.drawable.ic_pause_circle_outline_black_24dp);
}
else {
mediaPlayer3.start();
btn3.setImageResource(R.drawable.ic_play_circle_outline_black_24dp);
}
}

我想要的是,当上面的MediaPlayer同时运行时,首先点击MediaPlayer运行,其他停止,以及当我退出activity时, MediaPlayer自动停止,有没有解决方案或者上面的代码是否有简化?

最佳答案

制作您的播放列表。使用数组。

int[] playList = new int[3];
playList[0] = R.raw.madtarqiqc1;
playList[1] = R.raw.madtarqiqc2;
playList[2] = R.raw.madtarqiqc3;

并在全局变量中设置播放位置

int play = 0;

您可以使用mediaPlayer.setOnCompletionListener()

进行控制

例如,

MediaPlayer mediaPlayer = MediaPlayer.create(this, playList[play]);
mediaPlayer.start();
mediaPlayer.setOnCompletionListener(completionListener);
MediaPlayer.OnCompletionListener completionListener = new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
play++;
if (play < playList.length) {
mediaPlayer = MediaPlayer.create(BackGroundSound.this, playList[play]);
mediaPlayer.start();
} else {
play = 0;
mediaPlayer = MediaPlayer.create(BackGroundSound.this, playList[play]);
mediaPlayer.start();
}
}
};

当您制作下一个按钮(或单击事件)时。只是

mediaPlayer.stop(); 
mediaPlayer = MediaPlayer.create(this, playList[position]);
mediaPlayer.start();

p.s]我修改了你的代码。检查一下。

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

MediaPlayer mediaPlayer;
ImageButton btn1, btn2, btn3;
int[] playList;
int play = 0;

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

btn1 = (ImageButton) findViewById(R.id.play_toggle_ratarqiq1);
btn2 = (ImageButton) findViewById(R.id.play_toggle_ratarqiq2);
btn3 = (ImageButton) findViewById(R.id.play_toggle_ratarqiq3);

int[] playList = new int[3];
playList[0] = R.raw.madtarqiqc1;
playList[1] = R.raw.madtarqiqc2;
playList[2] = R.raw.madtarqiqc3;

mediaPlayer = MediaPlayer.create(this, playList[play]);
mediaPlayer.setOnCompletionListener(completionListener);

}

MediaPlayer.OnCompletionListener completionListener = new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
play++;
if (play < playList.length) {
mediaPlayer = MediaPlayer.create(MainActivity.this, playList[play]);
mediaPlayer.start();
} else {
play = 0;
mediaPlayer = MediaPlayer.create(MainActivity.this, playList[play]);
mediaPlayer.start();
}
}
};

@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.play_toggle_ratarqiq1:
playMusic(0);
break;
case R.id.play_toggle_ratarqiq2:
playMusic(1);
break;
case R.id.play_toggle_ratarqiq3:
playMusic(2);
break;
}
}

private void playMusic(int position) {
mediaPlayer.stop();
mediaPlayer = MediaPlayer.create(this, playList[position]);
mediaPlayer.start();

changeView(position);
}

private void changeView(int position) {
if (position == 0) {
btn1.setImageResource(R.drawable.ic_play_circle_outline_black_24dp);
btn2.setImageResource(R.drawable.ic_pause_circle_outline_black_24dp);
btn3.setImageResource(R.drawable.ic_pause_circle_outline_black_24dp);
} else if (position == 1) {
btn1.setImageResource(R.drawable.ic_pause_circle_outline_black_24dp);
btn2.setImageResource(R.drawable.ic_play_circle_outline_black_24dp);
btn3.setImageResource(R.drawable.ic_pause_circle_outline_black_24dp);
} else if (position == 2) {
btn1.setImageResource(R.drawable.ic_pause_circle_outline_black_24dp);
btn2.setImageResource(R.drawable.ic_pause_circle_outline_black_24dp);
btn3.setImageResource(R.drawable.ic_play_circle_outline_black_24dp);
}
}

}

关于java - Android MediaPlayer 重叠运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62439972/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com