如何使用 Intent for Media Player?

How to Use Intent for Media Player?

我想将意图用于我的作业应用程序。当我单击 button1 时,我的 SoundActivity 打开,播放完 sound1.mp3 文件后。但是我想当我点击 button2 时,sound2.mp3 文件在 SoundActivity..

中播放

这是我的 MainActivity.java 代码:

button1=findViewById(R.id.button1);
button2=findViewById(R.id.button2);

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

            Intent intent = new Intent(this, SoundActivity.class);

            }
        });

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

            Intent intent = new Intent(this, SoundActivity.class);

            }
        });

这是我的声音 Activity 方:

sound1 = MediaPlayer.create(this, R.raw.bell);
sound2 = MediaPlayer.create(this, R.raw.siren);

sound1.start();
'You can send your sound with intent using putt extras like'

button1=findViewById(R.id.button1);
button2=findViewById(R.id.button2);

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

            Intent intent = new Intent(this, SoundActivity.class);
            intent.putExtra("SOUND", R.raw.bell);
            startActivity(intent);

            }
        });

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

            Intent intent = new Intent(this, SoundActivity.class);
            intent.putExtra("SOUND", R.raw.siren);
            startActivity(intent);

            }
        });

'in sound actvity you just get the sound using bundle and pass it to media player like'

Bundle bundle = getIntent().getExtras();
        int sound = bundle.getInt("SOUND");

sound1 = MediaPlayer.create(this, sound);

sound1.start();




'Hope you get the solution.'
'Another way is that you just pass a key like which song you want to play on button click listener like'





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

            Intent intent = new Intent(this, SoundActivity.class);
            intent.putExtra("SOUND", "sound1");
            startActivity(intent);

            }
        });

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

            Intent intent = new Intent(this, SoundActivity.class);
            intent.putExtra("SOUND", "sound2");
            startActivity(intent);

            }
        });






'get this key in sound activity and pass the sound on the basis of condition like'






Bundle bundle = getIntent().getExtras();
        String sound = bundle.getInt("SOUND");

if(sound.equals(sound1)){
play_sound = MediaPlayer.create(this, R.raw.bell);
}else if(sound.equals(sound2)){
play_sound = MediaPlayer.create(this, R.raw.siren);
}

play_sound.start();




'Hope you get the best'