简体   繁体   English

单击时将音频按钮从播放更改为暂停,反之亦然

[英]Changing audio button from play to pause when clicked and vice versa

I want a play button in my app that when clicked plays the audio and changes to pause, and when the pause button is clicked the audio stops and the button changes to play again and so on. 我想要我的应用程序中的一个播放按钮,当单击该按钮时,它会播放音频并更改为暂停,而当单击暂停按钮时,音频会停止并且该按钮更改为再次播放,依此类推。 But the button is not working as expected. 但是该按钮无法正常工作。 Please help. 请帮忙。 I'm adding my java code below. 我在下面添加我的Java代码。

public class surah extends AppCompatActivity {

    MediaPlayer mp = new MediaPlayer();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_surah);
        mp=MediaPlayer.create(surah.this, R.raw.surahkahf);


        final ImageView audio = (ImageView)findViewById(R.id.btn);

        audio.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(mp.isPlaying()){
                    mp.stop();
                    audio.setBackgroundResource(R.drawable.play);
                    try {
                        mp.prepare();
                    }catch (IllegalStateException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                else {
                    mp.start();
                    audio.setImageResource(R.drawable.pause);

                }
            }
        });
    }
}

First, as aborocz mentions in comments, you probably intend to pause playback instead of stop it, so the method you want to use is pause() . 首先,正如aborocz在评论中提到的那样,您可能打算暂停播放而不是停止播放,因此要使用的方法是pause() In that case you would not need to prepare the MediaPlayer again, and it will start from the same place it was paused when playback is resumed. 在这种情况下,您无需再次准备MediaPlayer ,它将从恢复播放时暂停的位置开始。

Second, the isPlaying() method is not particularly appropriate for this purpose. 其次, isPlaying()方法不适用于此目的。 There are race conditions that prevent the desired behavior. 有些比赛条件阻止了期望的行为。 From the Android MediaPlayer documentation : 从Android MediaPlayer文档中

Note that the transition from the Started state to the Paused state and vice versa happens asynchronously in the player engine. 请注意,在播放器引擎中,异步发生从“开始”状态到“暂停”状态的转换,反之亦然。 It may take some time before the state is updated in calls to isPlaying(), and it can be a number of seconds in the case of streamed content. 在对isPlaying()的调用中更新状态之前,可能需要花费一些时间,对于流式传输的内容,状态更新可能需要花费几秒钟的时间。

Instead, store your own boolean value. 而是存储您自己的布尔值。

public class surah extends AppCompatActivity {

    private MediaPlayer mp;
    private boolean isMediaPlaying = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_surah);
        mp = MediaPlayer.create(surah.this, R.raw.surahkahf);

        final ImageView audio = (ImageView)findViewById(R.id.btn);

        audio.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isMediaPlaying) {
                    mp.pause();
                    audio.setBackgroundResource(R.drawable.play);
                    isMediaPlaying = false;
                } else {
                    mp.start();
                    audio.setImageResource(R.drawable.pause);
                    isMediaPlaying = true;
                }
            }
        });
    }
}

暂无
暂无

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

相关问题 当我单击“暂停”按钮时,它暂停了,但是当我再次单击“播放”按钮时,它从头开始播放歌曲 - When I click pause button it paused but when i clicked again play button , it starts the song from start Android:单击Button时,它将聚焦并移至其他editText [反之亦然] - Android: When Button is clicked it will focus and move to other editText [vice-versa] 单击 Android 时更改播放/暂停按钮图标 - Android change play/pause button icon when clicked 如果从文件夹中打开后单击主屏幕图标,Android应用程序将重新启动,反之亦然,但只有从Play Market下载后, - Android application restarts if homescreen icon clicked after opened from folder and vice versa but only if downloaded from Play Market 如何从上一个活动中单击某个按钮时播放音频文件 - How to play an Audio file when a certain button is clicked from previous activiy 从 iOS 上传的音频无法在 android 上播放,反之亦然 - Audio uploaded from iOS not playing on android and vice versa 在同一个按钮上播放和暂停音频 Kotlin - Play and pause audio on same button Kotlin 填充编辑文本和复选框时启用按钮,反之亦然 - Enable button when edit text and checkbox are are fill and vice versa 点击后如何将按钮图像从开启切换到关闭,反之亦然? - How to switch button image from on to off and vice versa upon click? 从我的应用程序播放和暂停外部音频? - Play and Pause external audio from my app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM