mediaplayer.prepare,如何避免吐司延迟。 “正在加载”

时间:2018-11-25 21:57:05

标签: java android android-mediaplayer

如何避免在mediaplayer.prepare时延迟吐司,我想在btn onclick时显示吐司。

FloatingActionButton button = findViewById(R.id.playbtnnine);

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


        if (mediaPlayer.isPlaying()){
            mediaPlayer.reset();
            Toast.makeText(getBaseContext(), "Stop" , Toast.LENGTH_SHORT ).show();
        }else {
            try {
                mediaPlayer.setDataSource(url);
                mediaPlayer.prepare();
            } catch (IOException e) {
                e.printStackTrace();
            }
            mediaPlayer.setLooping(true);
            mediaPlayer.start();
            Toast.makeText(getBaseContext(), "Loading" , Toast.LENGTH_SHORT ).show();
        }
    }
});

1 个答案:

答案 0 :(得分:-1)

您应该尝试

 FloatingActionButton button = findViewById(R.id.playbtnnine);

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

            Toast.makeText(getBaseContext(), "Loading" , Toast.LENGTH_SHORT ).show();

            if (mediaPlayer.isPlaying()){
                mediaPlayer.reset();
                Toast.makeText(getBaseContext(), "Stop" , Toast.LENGTH_SHORT ).show();
            }else {
                try {
                    mediaPlayer.setDataSource(url);
                    mediaPlayer.prepare();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                mediaPlayer.setLooping(true);
                mediaPlayer.start();
                 }
        }
    });