2011-11-03 11 views
0

這是我的java編碼..請幫助我..我正在爲兒童做一個項目。如何匹配Android中聲音對應的圖像?

package com.VRG; 

import java.util.ArrayList; 
import java.util.List; 
import java.util.Random; 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.DialogInterface; 
import android.media.MediaPlayer; 
import android.media.MediaPlayer.OnCompletionListener; 
import android.os.Bundle; 
import android.os.CountDownTimer; 
import android.util.Log; 
import android.view.KeyEvent; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.ImageView; 
import android.widget.TextView; 
import android.widget.Toast; 

public class Animalsgame extends Activity implements OnClickListener { 
    // int answer=0; 
    Button voice; 
    MediaPlayer mp; 
    TextView time;// count down timer 
    ImageView img_view1; 
    ImageView img_view2; 
    ImageView img_view3; 
    ImageView img_view4; 
    static int index = 0; 
    static int index1 = 0; 
    static int noOfLapse; 
    static int count = 0; 
    static boolean isSoundPlay; 
    CountDownTimer CountDownTimer; 
    int idx; 
    Integer next; 
    int temp1, temp2; 

    private Random random; 
    private ArrayList<MediaPlayer> sounds; 
    private ArrayList<MediaPlayer> tempSounds; 

    final int[] imageViews = { R.id.img_view1, R.id.img_view2, R.id.img_view3, 
      R.id.img_view4 };// Array of image views 

    int[] Animals_images = { R.drawable.cow, R.drawable.lion, R.drawable.dog, 
      R.drawable.sheep, R.drawable.elephant, R.drawable.camel, 
      R.drawable.rhinos, R.drawable.horse, R.drawable.donkey, 
      R.drawable.wolves }; // Array of Animal images 

    String Animals_images_string[] = { "cow", "lion", "dog", "sheep", 
      "elephant", "camel" }; 

    String mfile_sounds_string[] = { "cow", "lion", "dog", "sheep", "elephant", 
      "camel" }; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.game); 

     time = (TextView) this.findViewById(R.id.time); 
     img_view1 = (ImageView) this.findViewById(R.id.img_view1); 
     img_view2 = (ImageView) this.findViewById(R.id.img_view2); 
     img_view3 = (ImageView) this.findViewById(R.id.img_view3); 
     img_view4 = (ImageView) this.findViewById(R.id.img_view4); 

     img_view1.setOnClickListener(this); 
     img_view2.setOnClickListener(this); 
     img_view3.setOnClickListener(this); 
     img_view4.setOnClickListener(this); 

     // Count down timer 
     voice = (Button) this.findViewById(R.id.voice);// Animals sounds 

     voice.setOnClickListener(this); 

     // Initialise Lapse counter 
     noOfLapse = 0; 
     isSoundPlay = true; 

     random = new Random(); 

     sounds = new ArrayList<MediaPlayer>(); 
     sounds.add(MediaPlayer.create(this, R.raw.cow)); 
     sounds.add(MediaPlayer.create(this, R.raw.lion)); 
     sounds.add(MediaPlayer.create(this, R.raw.dog)); 
     sounds.add(MediaPlayer.create(this, R.raw.sheep)); 
     sounds.add(MediaPlayer.create(this, R.raw.elephant)); 
     sounds.add(MediaPlayer.create(this, R.raw.camel)); 

     tempSounds = (ArrayList<MediaPlayer>) sounds.clone(); 

     enableDisableImageView(false); 

    }// End of Oncreate method 

    public void setDuration(int duration) { 

    } 

    @Override 
    public void onClick(View v) { 
     enableDisableImageView(true); 

     if (v == img_view1) { 

      if (mp != null) { 
       if (mp.isPlaying()) { 
        mp.stop(); 
       } 
      } 
      count++; 
      time.setText(""); 
      if (CountDownTimer != null) { 
       CountDownTimer.cancel(); 
      } 
      enableDisableButton(true); 
     } 
     if (v == voice) { 
      loadImages(); 
      // answer++; 
      freezeButtonAndPlaySoundThenUnfreezeButton(); 
      countDown(); 
      // enableDisableButton(true); 
     } 

     if (v == img_view2) { 
      if (mp != null) { 
       if (mp.isPlaying()) { 
        mp.stop(); 
       } 
      } 
      count++; 
      enableDisableButton(true); // Re-enable buttons 
     } else if (v == voice) { 
      loadImages(); 
      countDown(); 
      enableDisableButton(false); 
     } 

     if (v == img_view3) { 
      if (mp != null) { 
       if (mp.isPlaying()) { 
        mp.stop(); 
       } 
      } 
      count++; 
      enableDisableButton(true); // Re-enable buttons 
     } else if (v == voice) { 
      loadImages(); 
      countDown(); 
      enableDisableButton(false); 
     } 

     if (v == img_view4) { 
      if (mp != null) { 
       if (mp.isPlaying()) { 
        mp.stop(); 
       } 
      } 
      count++; 
      enableDisableButton(true); // Re-enable buttons 
     } else if (v == voice) { 
      loadImages(); 
      countDown(); 
      enableDisableButton(false); 
     } 

     if (count > 4) { 
      AlertDialog.Builder alertbox = new AlertDialog.Builder(this); 
      setDuration(0); 
      mp = MediaPlayer.create(this, R.raw.claps); 
      if (mp != null) { 
       if (mp.isPlaying()) { 
        mp.stop(); 
       } 
      } 
      mp.start(); 
      Toast.makeText(getApplicationContext(), "Your Score : " + count, 
        Toast.LENGTH_SHORT).show(); 
      // set the message to display 
      alertbox.setMessage("Game Over..! Do you want to play again?"); 
      // set a positive/yes button and create a listener 
      alertbox.setPositiveButton("Yes", 
        new DialogInterface.OnClickListener() { 

         // do something when the button is clicked 
         public void onClick(DialogInterface arg0, int arg1) { 
          if (mp != null) { 
           if (mp.isPlaying()) { 
            mp.stop(); 
           } 
          } 
          count = 0; 
          // Intent i = new Intent(VRGActivity2.this, 
          // VRGActivity2.class); 
          // startActivity(i); 
          finish(); 
         } 
        }); 

      // set a negative/no button and create a listener 
      alertbox.setNegativeButton("No", 
        new DialogInterface.OnClickListener() { 

         // do something when the button is clicked 
         public void onClick(DialogInterface arg0, int arg1) { 
          if (mp != null) { 
           if (mp.isPlaying()) { 
            mp.stop(); 
           } 
          } 
          finish(); 
          // Intent i = new Intent(VRGActivity2.this, 
          // VRGActivity1.class); 
          // startActivity(i); 

         } 
        }); 

      // display box 
      alertbox.show(); 
      enableDisableButton(false); 
     } 
    }// End of voice on click 

    private void countDown() { 

     CountDownTimer = new CountDownTimer(6000, 10000) { 

      public void onTick(long millisUntilFinished) { 
       if (isSoundPlay) { 
        // freezeButtonAndPlaySoundThenUnfreezeButton(); 
        isSoundPlay = false; 
       } 
       time.setText("" + "" + millisUntilFinished/10000); 

      } 

      public void onFinish() { 

       // time.setText("UP"); 
       setDuration(0); 
       // Toast.makeText(getApplicationContext(), "Time Up", 
       // Toast.LENGTH_SHORT).show(); 

       isSoundPlay = true; 

      }// End of on finish 
     }.start(); // End of countDown Constructor 
    }// End of countDown method 

    public void enableDisableButton(Boolean state) { 
     voice.setEnabled(state); 

    } 

    public void enableDisableImageView(Boolean state) { 
     img_view1.setEnabled(state); 
     img_view2.setEnabled(state); 
     img_view3.setEnabled(state); 
     img_view4.setEnabled(state); 
    } 

    private void loadImages() { 
     // img_view1.setImageResource(Compulsory_Animals_images[answer]); 

     Random rng = new Random(); // Random function 
     List<Integer> generated = new ArrayList<Integer>(); 
     for (int i = 0; i < 4; i++) { 
      while (true) { 
       next = rng.nextInt(9); 
       index = next; 
       if (!generated.contains(next)) { 
        generated.add(next); 
        ImageView iv = (ImageView) findViewById(imageViews[i]); 
        iv.setImageResource(Animals_images[next]); 

        break; 
       }// End of if 
      }// While loop 
     }// End of for loop 
    } 

    public void freezeButtonAndPlaySoundThenUnfreezeButton() { 
     enableDisableButton(false); // disable buttons 
     int randomInt = random.nextInt(tempSounds.size()); 
     mp = tempSounds.get(randomInt); 

     // Toast.makeText(getApplicationContext(), "Index: "+sound_index, 
     // Toast.LENGTH_SHORT).show(); 
     temp1 = randomInt; 
     temp2 = temp1; 
     tempSounds.remove(randomInt); 
     temp1 = temp2; 
     mp.seekTo(0); 
     mp.start(); 
     mp.setOnCompletionListener(new OnCompletionListener() { 

      @Override 
      public void onCompletion(MediaPlayer mpalmost) { 
       // mpalmost.release(); 
      } 
     }); 

    } 

    @Override 
    // while back pressed the task is refreshed 
    public void onBackPressed() { 
     Log.v("44444", "onBackPressed Called"); 
     if (mp != null) { 
      if (mp.isPlaying()) { 
       mp.stop(); 
      } 
     } 
    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
     // saveState(); 
     Log.v("11111111", "onPause Called"); 

    } 

    @Override 
    protected void onStart() { 
     super.onStart(); 
     Log.v("111111", "onStart Called"); 

    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     Log.v("222222", "onResume Called"); 

     // freezeButtonAndPlaySoundThenUnfreezeButton(); 

    } 

    @Override 
    // if key down happens 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
     { 
      Log.d("333333", "onKeyDown Called"); 
      onBackPressed(); 
      finish(); 
      // calling the OnBackpressed method called 
     } 

     return true; 
    } 

}// } 

我使用四個圖像視圖和一個聲音按鈕。 其中,如果我點擊聲音按鈕隨機聲音 得到提高,並突然四個圖像視圖將 由四個隨機獲得的圖像填充。 現在我必須匹配聲音對應的聲音。

+0

對不起,我不明白。 「匹配」是什麼意思?你想讓孩子告訴正確的動物的聲音是這樣嗎? – Huang

+0

是的!實際上,孩子必須按下聲音按鈕,一個動物的聲音纔會升起,同時會顯示四個動物圖像。孩子必須在這裏聽到動物的聲音,並且必須選擇與聽到的聲音相對應的動物圖像......! PLZ幫助我..! – Yasir

+0

嘿,你是程序員,所以你應該知道正確的匹配,對吧?在您的程序中,您可以在每個聲音和圖像資源上設置一個ID,並對這兩個值組進行散列。現在,當孩子播放聲音時,你知道相應的圖像應該是什麼。 – Huang

回答

0

當我說散列,我不是說你必須設置散列表。這不是一個輕量級的解決方案。 「散列」意味着您必須在這兩個值組之間建立關係。在這裏,在Android上,每一個資源已經被編譯在R級的ID,您可以使用兩個int [] variables.I.e:

//keeps the element at the same position matching. 
final int[] animals = new Int[]{R.drawable.cow, R.drawable.lion, ...}; 
final int[] sounds = new Int[] {R.raw.cow,R.raw.lion, ...}; 

當孩子按下按鈕,你會得到一個隨機的聲音文件ID。即

int index = new Random.nextInt(0,TOTAL_RESOURCE_NUMBER); 

現在播放聲音「MediaPlayer.create(sounds [index]);」 將正確的imageView鏈接到該索引。 int imageIndex = new Random(0,4); imageViews[imageIndex].setDrawable(animals[index]);

現在你知道imageView與imageIndex是我們想要的imageView,你可以檢查。

+0

其實,你的答案是針對一個圖像..但孩子需要選擇根據所產生的聲音從四個圖像的集合中產生一個圖像..! – Yasir

+0

嗯,我只是設置正確的imageView匹配的聲音。設置其他3個imageViews並不困難。類似於for(int i = 0; i <4; i ++){if(i!= imageIndex)imageViews [i] .setDrawable();}當然,您需要在此循環中排除正確的圖像資源。 – Huang

+0

在這個論壇上,沒有人應該爲你編寫所有的代碼。你應該實現你自己的邏輯。這些人可能會給你一些提示或提示。希望你能理解。 – Huang