Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to play an Audio file when a certain button is clicked from previous activiy

I am making a language app for my learning purposes and to practice in Android I want to store all lessons audio in an array and i have an activity for each lesson when i press for example Audio 1 it popups a new activity with a player (AudioPopup.java) but the problem i dont't want to make a popup to play certain audio for each lesson what i need is to recognize which button was pressed and open the AudioPopup which i created and play that certain 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();


        }
    }

}

this only plays audio i insert it doesn't recognize from which button in the first activity came from

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 to press for each audio in each lesson which they are 24 lesson actually :(

like image 269
Ahmed Sedik Avatar asked Nov 09 '22 11:11

Ahmed Sedik


1 Answers

what you need to do is pass the selected lesson from first activity and load play proper audio in second one

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

simple one is numeric 1 , 2 ,3 ... each number represent lesson or you can pass resource id of audio file and just play it in next activity this option is better because you don't have to use if statement in next activity to know which lesson index 4 represents.

so here we go

for audio1 button, pass 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 audio2 button, pass 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 second activity, 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 refactor the code (if you have too many buttons) by creating a method that starts the popup activity, and call it from onClick 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);
    }
});
like image 87
Yazan Avatar answered Nov 14 '22 23:11

Yazan