2011-12-03 49 views
0

當我按下屏幕上的圖像時,它會調用powerButton.OnClickListener(),就像它應該的那樣,經過幾秒鐘的緩衝後,該流播放就會很好。然而,人們想要一個簡短的吐司彈出顯示,以通知用戶「無線電流連接,請稍候...」烤麪包線永遠不會被調用

這就是問題所在,無論我嘗試過什麼,我在哪裏放置該行以創建Toast彈出窗口,它在進入緩衝之前不會顯示。

任何幫助將不勝感激,我已經添加了大部分的代碼,並將提供更多,如果有必要。

 // 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(); 
       } 
      } 

     } 
    }); 

回答

2

這是因爲吐槽將不會顯示,直到你從onClick返回,因爲這是UI線程。我直到緩衝開始後纔會這樣做。我想要得到你想要的效果,你檢查AsynchTask在後臺線程中執行緩衝並立即返回onClick。

事實上,你這樣做的方式,如果緩衝時間太長,你的應用會被android標記爲無響應。

+0

」被android標記爲無響應。「理所當然地。我們不應該在UI線程上做長時間的任務。好決定。 – Guillaume

0

嘗試使用AsyncTask,其中AsyncTask所做的第一件事是在進入緩衝過程之前顯示敬酒。 「

相關問題