如何让一个按钮在按下时发出另一个 activity 的声音?

How to make a button play a sound as it launches another activity when pressed?

当前代码:

   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);
        }

    });

那么按钮如何启动一个新的 activity 并在启动时播放声音?

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

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

错误:

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

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);
            }
        });

首先你需要在raw文件夹中放一个sound.mp3
媒体播放器 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);
    }

});

别忘了发布它: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();