How to play an audio file when a button from a previous activity is pressed

I am making a language application for my training purposes and training on Android. I want to store all audio lessons in an array, and I have activity for each lesson, when I click, for example, Audio 1, it issues a new action using the player (AudioPopup.java ) but the problem is I don’t want the pop-up window to play a specific sound for each lesson I need to find out which button was pressed and open the AudioPopup that I created and played some mp3

AudioPopup Code

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(); } } } 

it only reproduces the sound that I insert, it does not recognize which button appeared in the first action

 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); } }); 

I have many buttons for each audio in each lesson in which they are actually 24 lessons :(

+5
source share
1 answer

what you need to do is transfer the selected lesson from the first activity and load the correct sound in the second one.

you can recognize which button (lesson) to play in several ways,

simple is numeric 1, 2, 3 ... each number is a lesson, or you can pass the resource identifier of the audio file and just play it in the next step, this option is better because you do not need to use the if statement in the next step to find out what is a lesson 4 index.

so we go

for the audio1 button, pass the res id of lesson 1

 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); } }); 

and for the audio2 button, pass the res id of lesson 2

 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); } }); 

now in the second step, get this passed value and play the audio.

 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); 

you can reorganize the code (if you have too many buttons) by creating a method that triggers a pop-up action and calling it from onClick the passing audio 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); } 

and onClick will be

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

All Articles