2012-07-30 89 views
0

我有這段代碼:
openHandlera = new ViDBOpenHandler(this);播放特定圖像時播放音樂android

  Runnable ru = new Runnable(){  
      public void run(){  

       int imageArrr[] = new int[2];  
       imageArrr[0] = R.drawable.crossone;  
       imageArrr[1] = R.drawable.blackone;  

       int n = (int)(Math.random()*2);  

       theIm.setImageResource(image[n]);  
       if(n == 0) {  
        Log.v("Result", "n is 0 now");  
        startTimea = System.currentTimeMillis();  
        final SoundGenerator task=new SoundGenerator();  
        task.keepPlaying(true);  
        task.setLoudness(audioLoudnessa);  
        task.execute(audioFrequencya);  

        new Handler().postDelayed(new Runnable() {  
          public void run()   {       
          task.keepPlaying(false);  
         }  
        }, 1000);  
       }  
       new Handler().postDelayed(new Runnable() {  
        public void run() {      

         finish();  
        }  
       }, 30000);  

        Log.v("test", "is"+ startTimea);  
        theIm.postDelayed(this, 3000); //set to go off again in 3 seconds.  
      }  
     };  
     theIm.postDelayed(ru,1500);   

在這段代碼我想說的是,如果我的形象是等於imageArrr [1],然後播放音樂一秒鐘,並設置當前時間開始的時間。問題是我用if(n == 0)。但它似乎並不承認這種實施方式。因爲它不會去,如果我聽不到聲音,甚至它不打印任何東西。任何人都可以指導我該怎麼做?

回答

1
int n = (int)(Math.random()*2);  

你是隨機產生的n價值,你沒有得到任何時候

以使條件if(n == 0)從未變成真。你沒有得到正確的輸出。

編輯:您可以使用這樣

if(n>2> 
    int image =imageArrr[0]; 
else 
    int image =imageArrr[1]; 
+0

但我想帶imageArrr之間的隨機圖像[0]和imageArrr [1]。那麼我應該如何使用隨機在這種情況下?我應該如何設定我的狀況? – androidGirl 2012-07-30 09:22:45

+0

這不是因爲索引,n可以是0或1嗎? PLZ指導我。 – androidGirl 2012-07-30 09:24:25

+0

看到我編輯的答案 – MAC 2012-07-30 09:25:23