【问题标题】:Sound stops when button is pressed again再次按下按钮时声音停止
【发布时间】:2017-01-12 15:40:36
【问题描述】:

我正在尝试制作一个 android 音板。当我触摸任何按钮时它会发出声音,但是当我再次触摸它时,不仅声音停止,而且其他按钮都不起作用。我希望它一次播放一个声音。这里是我在按钮上调用播放功能的主要活动。

public class MainActivity extends AppCompatActivity {

    MediaPlayer whine, cry, weed, chup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        whine = MediaPlayer.create(this, R.raw.gone);
        cry = MediaPlayer.create(this, R.raw.mock);
        weed = MediaPlayer.create(this, R.raw.phen);
        chup = MediaPlayer.create(this, R.raw.rg);
    }
    public void playwhine(View view) {
        if (cry.isPlaying())
            cry.stop();
        if (weed.isPlaying())
            weed.stop();
        if (chup.isPlaying())
            chup.stop();
        whine.start();
    }

    public void playcry(View view) {
        if (whine.isPlaying())
            whine.stop();
        if (weed.isPlaying())
            weed.stop();
        if (chup.isPlaying())
            chup.stop();
        cry.start();
    }

    public void playweed(View view) {
        if (cry.isPlaying())
            cry.stop();
        if (whine.isPlaying())
            whine.stop();
        if (chup.isPlaying())
            chup.stop();
        weed.start();
    }

    public void playchup(View view) {
        if (cry.isPlaying())
            cry.stop();
        if (whine.isPlaying())
            whine.stop();
        if (weed.isPlaying())
            weed.stop();
        chup.start();
    }
}

【问题讨论】:

    标签: java android audio


    【解决方案1】:

    此示例取自您的上一个方法:

    public void playchup(View view) {
        if (cry.isPlaying())
            cry.stop();
        if (whine.isPlaying())
            whine.stop();
        if (weed.isPlaying())
            weed.stop();
        chup.start();
    }
    

    如果您查看最后三个 if 语句,您会发现您停止了声音。如果有任何声音正在播放,则在按下按钮时它会停止。

    让我进一步解释:

    1. 按钮被按下
    2. 调用四种方法之一(play())
    3. 你停止其他声音
    4. 并为相关方法播放声音

    可能的问题

    理论上,当播放 MediaPLayer 时,您必须重新创建它。请参阅this question,了解如何循环播放声音,而不必每次都重新创建媒体播放器。

    意思是你的代码应该是这样的:

    whine = MediaPlayer.create(this, R.raw.gone);
    cry = MediaPlayer.create(this, R.raw.mock);
    weed = MediaPlayer.create(this, R.raw.phen);
    chup = MediaPlayer.create(this, R.raw.rg);
    
    whine.setLooping(true);
    cry.setLooping(true);
    weed.setLooping(true);
    chup.setLooping(true);
    

    如果您不想在按下一个按钮时停止所有其他声音,则必须删除检查其他声音是否正在播放并停止它们的 if 语句。

    MediaPlayer 的替代品

    如果您要处理多个声音,您可以使用SoundPool,因为它旨在一次处理多个重叠的声音。

    【讨论】:

      【解决方案2】:

      因为您正在创建 MusicPlayerobjects 而不是释放它们

      声明一个接口并覆盖setOnCompletion调用release()方法。

      示例

       private class musicCompletionListener implements MediaPlayer.OnCompletionListener {
              @Override
              public void onCompletion(MediaPlayer mediaPlayer) {
                  mediaPlayer.release();
              }
          }
      

      然后为每个 mediaPlayer 对象设置 setOnCompletion 侦听器。

      public void playwhine(View view) {
          if (cry.isPlaying())
              cry.stop();
          if (weed.isPlaying())
              weed.stop();
          if (chup.isPlaying())
              chup.stop();
      whine.setOnCompletionListener(new musicCompletionListener());
          whine.start();
      }
      
      public void playcry(View view) {
          if (whine.isPlaying())
              whine.stop();
          if (weed.isPlaying())
              weed.stop();
          if (chup.isPlaying())
              chup.stop();
      cry.setOnCompletionListener(new musicCompletionListener());
          cry.start();
      }
      
      public void playweed(View view) {
          if (cry.isPlaying())
              cry.stop();
          if (whine.isPlaying())
              whine.stop();
          if (chup.isPlaying())
              chup.stop();
      weed.setOnCompletionListener(new musicCompletionListener());
          weed.start();
      }
      
      public void playchup(View view) {
          if (cry.isPlaying())
              cry.stop();
          if (whine.isPlaying())
              whine.stop();
          if (weed.isPlaying())
              weed.stop();
      chup.setOnCompletionListener(new musicCompletionListener());
          chup.start();
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多