当我在屏幕上按下图像时,它会像预期的那样调用powerButton.OnClickListener(),并且在缓冲了几秒钟之后,流就可以正常播放了。但是,人们希望显示一个简短的烤面包弹出窗口,以通知用户“广播流正在连接,请稍候...”

这就是问题所在,无论我尝试过什么以及将行放置到创建吐司弹出窗口的位置上,它都不会在进入缓冲之前显示。

任何帮助将不胜感激,我已经添加了大多数代码,并在必要时提供更多代码。

        // run on powerButton click
    powerButton.setOnClickListener(new OnClickListener()
        {
        public void onClick(View view)
        {
            // check if the player is started or stopped
            if (isPlaying) // player is streaming
            {
                // stop the stream and set isPlaying to false
                mediaPlayer.stop();

                // release the media player
                releaseMediaPlayer();

                // update notification
                mNotificationManager.cancel(SIMPLE_NOTFICATION_ID);

                // set power button to "powered off" image
                powerButton.setImageResource(R.drawable.power_off);
            }
            else // player not streaming
            {
                // notify the user that the stream is loading
                final Toast streamLoading = toast.makeText(getBaseContext(), "Radio Stream Connecting, Please Wait...", Toast.LENGTH_SHORT);
                streamLoading.show();

                // try catch block to attempt connecting to radioUrl
                try
                {
                    // create new instance of media player
                    mediaPlayer = new MediaPlayer();

                    // set media player to handle audio streams
                    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

                    // connect to radio stream and fill buffer
                    mediaPlayer.setDataSource(radioUrl);
                    mediaPlayer.prepare(); // might take long depending on buffering speed

                    // start the media player and set isPlaying to true
                    mediaPlayer.start();
                    isPlaying = true;

                    // update notification, clear stream message
                    createNotification();

                    // set power button to "powered on" image
                    powerButton.setImageResource(R.drawable.power_on);
                }
                catch (IllegalArgumentException e) // cannot connect to stream
                {
                    // clear streaming text and notify user of failure
                    final Toast streamError1 = Toast.makeText(MainMenu.this, "Failed to load: Unable to connect to stream!", Toast.LENGTH_SHORT);
                    streamError1.show();

                    // release the media player and display error
                    releaseMediaPlayer();
                    mNotificationManager.cancel(SIMPLE_NOTFICATION_ID);
                    e.printStackTrace();
                }
                catch (IllegalStateException e) // stream cannot play audio
                {
                    // clear streaming text and notify user of failure
                    final Toast streamError2 = Toast.makeText(getBaseContext(), "Failed to load: cannot play stream!", Toast.LENGTH_SHORT);
                    streamError2.show();

                    // release the media player and display error
                    releaseMediaPlayer();
                    mNotificationManager.cancel(SIMPLE_NOTFICATION_ID);
                    e.printStackTrace();
                }
                catch (IOException e) // general connection issue
                {
                    // clear streaming text and notify user of failure
                    final Toast streamError3 = Toast.makeText(getBaseContext(), "Failed to Load: Connection issue!", Toast.LENGTH_SHORT);
                    streamError3.show();

                    // release the media player and display error
                    releaseMediaPlayer();
                    mNotificationManager.cancel(SIMPLE_NOTFICATION_ID);
                    e.printStackTrace();
                }
            }

        }
    });

最佳答案

那是因为直到您从onClick返回之前,吐司才会显示,因为那是UI线程。 I在缓冲开始之前,您不会这样做。我认为要获得所需的效果,请检查AsynchTask在后台线程中进行缓冲,然后立即从onClick返回。

实际上,您的操作方式是,如果缓冲时间过长,Android会将您的应用标记为无响应。

07-24 21:42