0

当前代码:

   MediaPlayer mp;

    button1=(ImageButton)findViewById(R.id.button1);
    button1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent i = new Intent(getApplicationContext(), Activity1.class);
            startActivity(i);
        }

    });

那么按钮如何启动一个新的活动并在这样做的同时播放声音呢?

我试过的:各种方法,比如 playSound(); 在方法中。

它只播放默认的安卓声音。我想要存储在原始目录中的特定声音。因此,当按下按钮时,它会启动启动活动的意图以及特定的声音。

错误:

当我尝试把 MediaPlayer mp; 在按钮上方,它表明变量 mp 已经定义。我只需要有人附加活动启动代码,以便它也能播放声音。

4

3 回答 3

0
MediaPlayer mp = MediaPlayer.create(getApplicationContext(), Uri.parse(""));
        mp.setOnPreparedListener(new OnPreparedListener() {

            @Override
            public void onPrepared(MediaPlayer player) {
                // TODO Auto-generated method stub
                player.start();
                Intent i = new Intent(getApplicationContext(), Activity1.class);
                startActivity(i);
            }
        });
于 2015-03-24T14:54:58.943 回答
0

首先,您需要将 sound.mp3 放在原始文件夹
MediaPlayer mp 中;

button1=(ImageButton)findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        try {
                    mp = MediaPlayer.create(Music.this, R.raw.sound);
                } catch (IllegalArgumentException e) {
                    Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
                } catch (SecurityException e) {
                    Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
                } catch (IllegalStateException e) {
                    Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
                }
                try {
                    mp.prepare();
                } catch (IllegalStateException e) {
                    Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
                } catch (IOException e) {
                    Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
                }
                mp.start();
        Intent i = new Intent(getApplicationContext(), Activity1.class);
        startActivity(i);
    }

});
于 2015-03-24T15:09:28.203 回答
0

不要忘记发布它:http: //developer.android.com/reference/android/media/MediaPlayer.html#release%28%29

Mediaplayer mediaPlayer = MediaPlayer.create(this, R.raw.YOURMP3NAMEFILE);
    mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            if(mp != null){
                mp.release();
                mediaPlayer = null;
                Log.d(TAG, "release mediaplayer");
            }
        }
    });
mediaPlayer.start();
launchSecondActivity();
于 2015-07-02T21:34:20.387 回答