Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/188.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在android中匹配与声音对应的图像?_Android - Fatal编程技术网

如何在android中匹配与声音对应的图像?

如何在android中匹配与声音对应的图像?,android,Android,这是我的java代码。。请帮帮我。。我正在为孩子们做一个项目 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.Med

这是我的java代码。。请帮帮我。。我正在为孩子们做一个项目

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;
    }

}// }
package com.VRG;
导入java.util.ArrayList;
导入java.util.List;
导入java.util.Random;
导入android.app.Activity;
导入android.app.AlertDialog;
导入android.content.DialogInterface;
导入android.media.MediaPlayer;
导入android.media.MediaPlayer.OnCompletionListener;
导入android.os.Bundle;
导入android.os.CountDownTimer;
导入android.util.Log;
导入android.view.KeyEvent;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.Button;
导入android.widget.ImageView;
导入android.widget.TextView;
导入android.widget.Toast;
公共类Animalsgame扩展活动实现OnClickListener{
//int-answer=0;
按钮声音;
MediaPlayer mp;
text查看时间;//倒计时
图像视图img_视图1;
图像视图img_视图2;
图像视图img_视图3;
图像视图img_视图4;
静态int指数=0;
静态int index1=0;
静态内翻;
静态整数计数=0;
静态布尔播放;
倒计时倒计时;
int-idx;
整数次;
int temp1,temp2;
私有随机;
私人ArrayList声音;
私人ArrayList tempSounds;
final int[]imageview={R.id.img_视图1,R.id.img_视图2,R.id.img_视图3,
R.id.img_view4};//图像视图数组
int[]动物图片={R.drawable.cow,R.drawable.lion,R.drawable.dog,
绵羊,大象,骆驼,
可牵拉的犀牛,可牵拉的马,可牵拉的驴,
R.drawable.wolfs};//动物图像数组
字符串动物\u图像\u字符串[]={“牛”、“狮子”、“狗”、“羊”,
“大象”、“骆驼”};
字符串mfile_发音_字符串[]={“牛”、“狮子”、“狗”、“羊”、“大象”,
“骆驼”};
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
time=(TextView)this.findViewById(R.id.time);
img_view1=(ImageView)this.findviewbyd(R.id.img_view1);
img_view2=(ImageView)this.findviewbyd(R.id.img_view2);
img_view3=(ImageView)this.findviewbyd(R.id.img_view3);
img_view4=(ImageView)this.findviewbyd(R.id.img_view4);
img_view1.setOnClickListener(此);
img_view2.setOnClickListener(此);
img_view3.setOnClickListener(此);
img_view4.setOnClickListener(此);
//倒计时
voice=(按钮)this.findviewbyd(R.id.voice);//动物的声音
voice.setOnClickListener(这个);
//初始化失效计数器
noOfLapse=0;
isSoundPlay=正确;
随机=新随机();
sounds=新的ArrayList();
添加(MediaPlayer.create(this,R.raw.cow));
添加(MediaPlayer.create(this,R.raw.lion));
添加(MediaPlayer.create(this,R.raw.dog));
添加(MediaPlayer.create(this,R.raw.sheep));
添加(MediaPlayer.create(this,R.raw.大象));
添加(MediaPlayer.create(this,R.raw.camel));
tempsoonds=(ArrayList)sounds.clone();
enableDisableImageView(假);
}//Oncreate方法的结束
公共void setDuration(int duration){
}
@凌驾
公共void onClick(视图v){
enableDisableImageView(真);
如果(v==img_view1){
如果(mp!=null){
if(mp.isPlaying()){
mp.stop();
}
}
计数++;
time.setText(“”);
if(倒计时!=null){
倒计时。取消();
}
enableDisableButton(真);
}
if(v==语音){
loadImages();
//回答++;
冻结按钮和播放声音然后取消冻结按钮();
倒计时();
//enableDisableButton(真);
}
if(v==img_视图2){
如果(mp!=null){
if(mp.isPlaying()){
mp.stop();
}
}
计数++;
enableDisableButton(true);//重新启用按钮
}else if(v==语音){
loadImages();
倒计时();
enableDisableButton(假);
}
如果(v==img_视图3){
如果(mp!=null){
if(mp.isPlaying()){
mp.stop();
}
}
计数++;
enableDisableButton(true);//重新启用按钮
}else if(v==语音){
loadImages();
倒计时();
enableDisableButton(假);
}
if(v==img_视图4){
如果(mp!=null){
if(mp.isPlaying()){
mp.stop();
}
}
计数++;
enableDisableButton(true);//重新启用按钮
}else if(v==语音){
loadImages();
倒计时();
enableDisableButton(假);
}
如果(计数>4){
AlertDialog.Builder alertbox=新建AlertDialog.Builder(此);
设定持续时间(0);
mp=MediaPlayer.create(这个,R.raw.claps);
如果(mp!=null){
if(mp.isPlaying()){
mp.stop();
}
}
mp.start();
Toast.makeText(getApplicationContext(),“您的分数:”+count,
吐司。长度(短)。show();
//将消息设置为显示
setMessage(“游戏结束..!你想再次玩吗?”);
//设置一个肯定/是按钮并创建一个侦听器
alertbox.setPositiveButton(“是”,
新建DialogInterface.OnClickListener(){
//单击按钮时执行某些操作
//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, ...};
int index = new Random.nextInt(0,TOTAL_RESOURCE_NUMBER);