【问题标题】:How do I play a sound on an ImageButton click in another Activity?如何在另一个 Activity 中单击 ImageButton 时播放声音?
【发布时间】:2019-09-24 08:26:26
【问题描述】:

我正在尝试创建一个图像按钮的方法。单击它时,背景音乐停止,图像按钮变为另一个图像。再次按下时,它会像第一次一样返回并重新播放音乐。

我正在尝试布尔值。当它为真时,音乐开始,当它为假时,音乐开始,但它不起作用!

此外,如何根据主要活动让另一个活动播放或停止音乐?

public class MainActivity extends AppCompatActivity {

    MediaPlayer mp;
    ImageButton SoundButton;
    ImageButton NoSoundButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        SoundButton = new ImageButton(this);
        NoSoundButton = new ImageButton(this);
        /*---------Image Buttons--------*/

        SoundButton=(ImageButton) findViewById(R.id.sound);
        SoundButton.setVisibility(View.GONE);
        NoSoundButton=(ImageButton) findViewById(R.id.nosound);
        NoSoundButton.setVisibility(View.VISIBLE);

        /*---------Media Player--------*/

        mp = new MediaPlayer();
        mp = MediaPlayer.create(this, R.raw.aud);
        mp.setLooping(true);
        mp.start();
    }

    public void nosound(View view) {
        SoundButton.setVisibility(View.VISIBLE);
        NoSoundButton.setVisibility(View.INVISIBLE);
        mp.stop();
        mp.prepareAsync();
    }

    public void sound(View view) {
        SoundButton.setVisibility(View.INVISIBLE);
        NoSoundButton.setVisibility(View.VISIBLE);
        mp.start();
    }
}

【问题讨论】:

  • 能否提供您目前使用的代码?
  • 我们必须知道:你用什么来播放音乐?你怎么改变你的照片了?您是否设置了 onclicklistener 等。
  • 编辑您的问题并插入代码
  • 不要放 MediaPlayer mp = new MediaPlayer();到现在的位置。放到onCreate方法中

标签: android audio onclick android-mediaplayer android-imagebutton


【解决方案1】:

1)你必须改变你的MediaPlayer的初始化位置。

 MediaPlayer mp = new MediaPlayer();

    public class MainActivity extends AppCompatActivity {

        boolean SoundStatus;
        MediaPlayer mp;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mp = new MediaPlayer();
            mp = MediaPlayer.create(this, R.raw.aud);
            mp.setLooping(true);
            mp.start();
        }
            public void sound(View view) {
            SoundStatus = true;
            if (SoundStatus) {
                mp.stop();
                SoundStatus = false;
            }
            else {
                mp.start();
                SoundStatus = true;
            }
        }}

当然还要确保音频文件存在。


2)如何传递按钮状态

您可以使用 Bundle 在以下活动之间传递按钮状态

开始活动 2

Intent intent = new Intent(this, Activity2.class);
intent.putExtra(EXTRA_NAME, VALUE);
startActivity(intent);

在活动 2 中获取该值

@Override
protected void onCreate(Bundle savedInstanceState) {
....
boolean value = getIntent().getExtras().getBoolean(EXTRA_VALUE);
}

然后像上面一样做,把它传递给 Activity 3。

或者

您可以在 Activity 1 中创建一个静态变量,然后从 Activity 3 访问它。

【讨论】:

  • mp 在 'onCreate' 中是已知的!在“声音”方法中是未知的
  • 那么把 mediaplayer mp 放到全局或者把 mediaplayer 作为参数传递
  • 更新了我的代码.. 现在试试看,顺便说一句,您是否注意到您从未调用过“声音”方法?
  • 它是通过'onClick'调用的,我尝试了你的代码,当我按下图像按钮时停止了,但是当我再次按下它时,音乐再也没有播放过! @Cédric Portmann
  • 考虑把mp.reset();在 mp.stop() 方法之后。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-03
相关资源
最近更新 更多