【问题标题】:How to make a button play a sound as it launches another activity when pressed?如何使按钮在按下时启动另一个活动时播放声音?
【发布时间】:2015-07-03 05:34:20
【问题描述】:

当前代码:

   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();在方法中。

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

错误:

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

【问题讨论】:

  • 如果您没有在手机设置中指定不同的设置,当您按下按钮时,默认情况下会播放声音
  • 是声音文件在应用程序中的位置还是您想要流式传输?
  • @Blackbelt 我的意思是一个特定的声音。

标签: java android onclick playsound


【解决方案1】:

首先,您需要将 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);
    }

});

【讨论】:

  • 然后将变量设为 final 并在 onCreate 方法之外尝试,如 final MediaPlayer mp;
  • 是在 OnCreate 方法之外声明的 MediaPlayer mp。只需尝试在上面声明它** @Override protected void onCreate(Bundle savedInstanceState)**
  • 谢谢Journey,我也会投票给你,但我没有15个声望。
【解决方案2】:
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);
            }
        });

【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-11
      • 2014-11-02
      • 2015-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多