如何为媒体播放器使用意图?

时间:2019-05-29 18:25:52

标签: android audio android-intent android-mediaplayer

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

这是我的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);

            }
        });

这是我的声音活动方面:

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

sound1.start();

2 个答案:

答案 0 :(得分:0)

'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.'

答案 1 :(得分:0)

'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'