2014-09-26 70 views
0

我有一個mediaplayer類,其中包含所有的方法。我只是想知道文件何時播放完畢,但我遇到了麻煩。 這裏是方法:當媒體文件播放完畢時的Android通知

public boolean isPlaying() 
{ 
    final boolean still_playing = true; 
    mediaplayer.setOnCompletionListener(new OnCompletionListener() 
    { 
     @Override 
     public void onCompletion(MediaPlayer mp) 
     { 
      still_playing = false; 
     } 
    }); 
    return still_playing; 
} 

我喜歡the final local variable cannot be assigned, since it is defined in an enclosing type收到錯誤或當我嘗試不同的東西,我得到variabledeclaratorIdexpected 乾杯

回答

0

final boolean still_playing = true;是一個常數變量,這意味着它不能在改變運行。

在以後的程序:

@Override 
public void onCompletion(MediaPlayer mp) 
{ 
    still_playing = false; 
} 

你試圖改變它的價值。

爲了解決這個問題,你應該定義boolean still_playing = true;作爲類的像母豬的成員變量:

public class classExample { 
    // member variables 
    private boolean still_playing = true; 

    public boolean isPlaying() { 

     /* I DELETED THE VARIABLE AND DECLARED IT OUTSIDE THE METHOD */ 

     mediaplayer.setOnCompletionListener(new OnCompletionListener() { 
      @Override 
      public void onCompletion(MediaPlayer mp) { 

        /* You could declare it here too without the private 
        keyword like this: 
          boolean still_playing = true; */ 

        still_playing = false; 
      } 
     }); 
     return still_playing; 
    } 
}