Как использовать Intent для Media Player? - PullRequest
0 голосов
/ 29 мая 2019

Я хочу использовать намерение для моей домашней работы. Когда я нажимаю кнопку 1, открывается мое 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);

            }
        });

Это моя сторона звуковой активности:

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

sound1.start();

Ответы [ 2 ]

0 голосов
/ 29 мая 2019
'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'
0 голосов
/ 29 мая 2019
'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.'
...