简体   繁体   English

在按下按钮时启动另一个活动时,如何使按钮播放声音?

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

Current code: 当前代码:

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

    });

So how would a button start a new activity and play a sound while doing so? 那么,一个按钮如何开始一个新的活动并同时播放声音呢?

What I've tried: various methods like playSound( ); 我尝试过的方法:playSound()等各种方法; in the method. 在方法中。

It only plays the default android sound. 它只播放默认的Android声音。 I want a specific sound stored in the raw directory. 我想要特定的声音存储在原始目录中。 So when the button get's pressed it launches both the intent to launch the activity as well as the specific sound. 因此,当按下获取按钮时,它既可以启动活动,也可以启动特定的声音。

Error: 错误:

When I try to put MediaPlayer mp; 当我尝试放置MediaPlayer mp时; above the button, it states variable mp is already defined. 在按钮上方,它表明变量mp已经定义。 I just need someone to append the activity launch code so that it will play the sound as well. 我只需要某人附加活动启动代码,以便它也可以播放声音。

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

First you need to put a sound.mp3 in raw folder 首先,您需要在原始资料夹中放入sound.mp3
MediaPlayer mp; 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);
    }

});

Don't forget to release it : http://developer.android.com/reference/android/media/MediaPlayer.html#release%28%29 别忘了发布它: 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM