我正在为我的学习目的制作一个语言应用程序并在Android中练习 我想将所有课程音频存储在一个数组中,当我按下例如音频1时,我会为每节课有一个活动它会弹出一个与玩家一起的新活动(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();
}
}
}
这只播放音频我插入它并不能识别第一个活动中的哪个按钮来自
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课:(
答案 0 :(得分:1)
您需要做的是从第一个活动传递选定的课程 并加载在第二个播放适当的音频
您可以通过多种方式识别要播放的按钮(课程),
简单的一个是数字1,2,3 ...每个数字代表课程 或者您可以传递音频文件的资源ID,并在下一个活动中播放它 此选项更好,因为您不必在下一个活动中使用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);
}
});
现在在第二个活动中,获取此传递的值,然后播放音频。
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);
你可以重构代码(如果你有太多的按钮) 通过创建一个启动弹出活动的方法,并从onClick传递音频res id
调用它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);
}
});