gpt4 book ai didi

java - 如何在上一个 Activity 中单击某个按钮时播放音频文件

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:11:25 26 4
gpt4 key购买 nike

我正在制作一款语言应用,用于我的学习目的和在 Android 中练习我想将所有类(class)的音频存储在一个数组中,当我按下例如音频 1 时,每节课都有一个 Activity ,它会弹出一个带有播放器的新 Activity (AudioPopup.java)但问题是我不想让弹出窗口为每节课播放某些音频我需要的是识别按下了哪个按钮并打开我创建的 AudioPopup 并播放特定的 mp3

AudioPopup代码

public class AudioPopup extends AppCompatActivity {

SeekBar seek_bar;
ImageView play_button, pause_button;
MediaPlayer player;
ImageView exitPlayer;
Handler seekHandler = new Handler();
private int [] audioFiles;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.audio_popup);

setFinishOnTouchOutside(false);
audioFiles = new int [] {R.raw.lesson1-1,R.raw.lesson1-2,R.raw.lesson1-3,R.raw.lesson1-4};


getInit();
seekUpdation();

exitPlayer = (ImageView) findViewById(R.id.audio_exit_1);
exitPlayer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
player.stop();

}
});

}

public void getInit() {
seek_bar = (SeekBar) findViewById(R.id.seekbar_1);
play_button = (ImageView) findViewById(R.id.audio_play_1);
pause_button = (ImageView) findViewById(R.id.audio_stop_1);

play_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {


PlayAudio();

}
});
pause_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
player.stop();
releaseMediaPlayer();
player = MediaPlayer.create(AudioPopup.this, lesson1-1);
play_button.setImageResource(R.drawable.ic_play_circle_filled_black_48dp);
}
});

player = MediaPlayer.create(this, R.raw.l01_audiotraining_grammatik_01);
seek_bar.setMax(player.getDuration());
seek_bar.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
seekChange(v);
return false;
}
});


}

Runnable run = new Runnable() {
@Override
public void run() {
seekUpdate();

}
};

public void seekUpdate() {


seek_bar.setProgress(player.getCurrentPosition());
seekHandler.postDelayed(run, 1000);



}

//event handler for the progress of seek bar
private void seekChange(View view) {
if (player.isPlaying()) {
SeekBar sb = (SeekBar) view;
player.seekTo(sb.getProgress());

}

}

private void releaseMediaPlayer() {
// If the media player is not null, then it may be currently playing a sound.
if (player != null) {
// Regardless of the current state of the media player, release its resources
// because we no longer need it.
player.release();

// Set the media player back to null. For our code, we've decided that
// setting the media player to null is an easy way to tell that the media player
// is not configured to play an audio file at the moment.
player = null;
}
}

private void PlayAudio(){

if (player == null) {
player = MediaPlayer.create(AudioPopup.this,R.raw.lesson1-1 );
}

if (player.isPlaying()) {

player.pause();
play_button.setImageResource(R.drawable.ic_play_circle_filled_black_48dp);
try {
player.prepare();
} catch (IllegalStateException e) {

e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

} else {


play_button.setImageResource(R.drawable.ic_pause_circle_filled_black_48dp);
player.start();


}
}

}

这只播放我插入的音频,它无法识别第一个 Activity 中来自哪个按钮

audio1 = (ImageView) popupView.findViewById(R.id.lesson1_audio_gramar_1);
assert audio1 != null;
audio1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent audio_intent = new Intent(getApplicationContext(), AudioPopup.class);
startActivity(audio_intent);

}
});

每节课的每个音频我都有很多按钮要按,实际上是 24 节课:(

最佳答案

您需要做的是通过第一个 Activity 中选定的类(class)并在第二个中加载播放正确的音频

您可以通过多种方式识别播放哪个按钮(类(class)),

简单的一个是数字 1 , 2 ,3 ... 每个数字代表类(class)或者您可以传递音频文件的资源 ID,然后在下一个 Activity 中播放此选项更好,因为您不必在下一个 Activity 中使用 if 语句来了解索引 4 代表哪一课。

我们开始吧

对于 audio1 按钮,传递第 1 课的 res id

audio1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent audio_intent = new Intent(getApplicationContext(), AudioPopup.class);
audio_intent.putExtra("lesson_res_id", R.raw.lesson1-1);
startActivity(audio_intent);

}
});

对于 audio2 按钮,传递第 2 课的 res id

audio2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent audio_intent = new Intent(getApplicationContext(), AudioPopup.class);
audio_intent.putExtra("lesson_res_id", R.raw.lesson2-2);
startActivity(audio_intent);

}
});

现在在第二个 Activity 中,获取这个传递的值,然后播放音频。

int lessonResId = getIntent().getIntExtra("lesson_res_id", -100); //-100 is default, means the key 'lesson_res_id' was not found in extras bundle
playAudio(lessonResId);

你可以重构代码(如果你有太多的按钮)通过创建一个启动弹出 Activity 的方法,并从传递音频资源 ID 的 onClick 调用它

private void openPopupActivity(int resId){
Intent audio_intent = new Intent(getApplicationContext(), AudioPopup.class);
audio_intent.putExtra("lesson_res_id", resId);
startActivity(audio_intent);
}

和 onClick 将是

audio2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openPopupActivity(R.raw.lesson2-2);
}
});

关于java - 如何在上一个 Activity 中单击某个按钮时播放音频文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38732422/

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