通过单击Android中的“下一步”和“上一步”按钮更改阵列图像和音频

时间:2014-02-01 13:57:08

标签: java android arrays eclipse audio

我在Android上制作简单,它有99个图像和99个音频声音,我为它们创建了单独的数组。我想当我点击下一个按钮然后“R.drawable.p1”图像出现并产生“R.raw.a1”声音,当我点击下一个按钮然后“R.drawable.p2”图像出现并产生“R” .raw.a2声音“。在这种情况下,我的图像阵列正如我想要的那样完美,但问题是我的音频工作不正常,我的下一个按钮音频工作正常,但当我回到上一个按钮然后它首先产生下一个按钮声音,然后来到以前。我想像image1 = sound1,image2 = sound2,image3 = sound3,但问题是我点击上一个按钮,它看起来像这个image2 = sound3,image1 = sound2。

其次,还有一个问题是,当一个循环完成后,它会将所有声音静音,为什么?

如果你知道任何方法,任何if-else OR Switch-case逻辑然后plz回复我,任何帮助将不胜感激。 这是我的代码:

public class MainActivity extends Activity {

    private ImageView hImageViewPic;
    private Button iButton, gButton;
    MediaPlayer ourSong;

    private int currentImage = 0;
    public int currentAudio = 0;

    int[] images = { R.drawable.p1, R.drawable.p2, R.drawable.p3,
            R.drawable.p4, R.drawable.p5, R.drawable.p6, R.drawable.p7,
            R.drawable.p8, R.drawable.p9, R.drawable.p10, R.drawable.p11,
            R.drawable.p12, R.drawable.p13, R.drawable.p14, R.drawable.p15,
            R.drawable.p16, R.drawable.p17, R.drawable.p18, R.drawable.p19,
            R.drawable.p20, R.drawable.p21, R.drawable.p22, R.drawable.p23,
            R.drawable.p24, R.drawable.p25, R.drawable.p26, R.drawable.p27,
            R.drawable.p28, R.drawable.p29, R.drawable.p30, R.drawable.p31,
            R.drawable.p32, R.drawable.p33, R.drawable.p34, R.drawable.p35,
            R.drawable.p36, R.drawable.p37, R.drawable.p38, R.drawable.p39,
            R.drawable.p40, R.drawable.p41, R.drawable.p42, R.drawable.p43,
            R.drawable.p44, R.drawable.p45, R.drawable.p46, R.drawable.p47,
            R.drawable.p48, R.drawable.p49, R.drawable.p50, R.drawable.p51,
            R.drawable.p52, R.drawable.p53, R.drawable.p54, R.drawable.p55,
            R.drawable.p56, R.drawable.p57, R.drawable.p58, R.drawable.p59,
            R.drawable.p60, R.drawable.p61, R.drawable.p62, R.drawable.p63,
            R.drawable.p64, R.drawable.p65, R.drawable.p66, R.drawable.p67,
            R.drawable.p68, R.drawable.p69, R.drawable.p70, R.drawable.p71,
            R.drawable.p72, R.drawable.p73, R.drawable.p74, R.drawable.p75,
            R.drawable.p76, R.drawable.p77, R.drawable.p78, R.drawable.p79,
            R.drawable.p80, R.drawable.p81, R.drawable.p82, R.drawable.p83,
            R.drawable.p84, R.drawable.p85, R.drawable.p86, R.drawable.p87,
            R.drawable.p88, R.drawable.p89, R.drawable.p90, R.drawable.p91,
            R.drawable.p92, R.drawable.p93, R.drawable.p94, R.drawable.p95,
            R.drawable.p96, R.drawable.p97, R.drawable.p98, R.drawable.p99 };

    int[] audios = { R.raw.a1, R.raw.a2, R.raw.a3, R.raw.a4, R.raw.a5,
            R.raw.a6, R.raw.a7, R.raw.a8, R.raw.a9, R.raw.a10, R.raw.a11,
            R.raw.a12, R.raw.a13, R.raw.a14, R.raw.a15, R.raw.a16, R.raw.a17,
            R.raw.a18, R.raw.a19, R.raw.a20, R.raw.a21, R.raw.a22, R.raw.a23,
            R.raw.a24, R.raw.a25, R.raw.a26, R.raw.a27, R.raw.a28, R.raw.a29,
            R.raw.a30, R.raw.a31, R.raw.a32, R.raw.a33, R.raw.a34, R.raw.a35,
            R.raw.a36, R.raw.a37, R.raw.a38, R.raw.a39, R.raw.a40, R.raw.a41,
            R.raw.a42, R.raw.a43, R.raw.a44, R.raw.a45, R.raw.a46, R.raw.a47,
            R.raw.a48, R.raw.a49, R.raw.a50, R.raw.a51, R.raw.a52, R.raw.a53,
            R.raw.a54, R.raw.a55, R.raw.a56, R.raw.a57, R.raw.a58, R.raw.a59,
            R.raw.a60, R.raw.a61, R.raw.a62, R.raw.a63, R.raw.a64, R.raw.a65,
            R.raw.a66, R.raw.a67, R.raw.a68, R.raw.a69, R.raw.a70, R.raw.a71,
            R.raw.a72, R.raw.a73, R.raw.a74, R.raw.a75, R.raw.a76, R.raw.a77,
            R.raw.a78, R.raw.a79, R.raw.a80, R.raw.a81, R.raw.a82, R.raw.a83,
            R.raw.a84, R.raw.a85, R.raw.a86, R.raw.a87, R.raw.a88, R.raw.a89,
            R.raw.a90, R.raw.a91, R.raw.a92, R.raw.a93, R.raw.a94, R.raw.a95,
            R.raw.a96, R.raw.a97, R.raw.a98 };

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        hImageViewPic = (ImageView) findViewById(R.id.idImageViewPic);
        iButton = (Button) findViewById(R.id.bIleri);
        gButton = (Button) findViewById(R.id.bGeri);
        // Just set one Click listener for the image

        iButton.setOnClickListener(iButtonChangeImageListener);
        gButton.setOnClickListener(gButtonChangeImageListener);

    }

    View.OnClickListener iButtonChangeImageListener = new OnClickListener() {

        public void onClick(View v) {
            try {
                // Increase Counter to move to next Image
                currentImage++;

                currentImage = currentImage % images.length;

                hImageViewPic.setImageResource(images[currentImage]);

                ourSong = MediaPlayer.create(MainActivity.this,
                        audios[currentAudio]);
                currentAudio++;
                ourSong.start();
            } catch (Exception e) {

            }

        }

    };

    View.OnClickListener gButtonChangeImageListener = new OnClickListener() {

        public void onClick(View v) {
            try {
                // Decrease Counter to move to previous Image
                currentImage--;

                currentImage = (currentImage + images.length) % images.length;

                hImageViewPic.setImageResource(images[currentImage]);

                MediaPlayer.create(MainActivity.this, audios[currentAudio]);
                currentAudio--;
                ourSong.start();

            } catch (Exception e) {

            }
        }
    };

    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        ourSong.release();
        finish();
    }
}

2 个答案:

答案 0 :(得分:0)

只需点击按钮即可设置:

int current_img = 0;
int current_aud = 0;

btn_next.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            current_img ++;
            if(current_img == pets.length){
                current_img = 0;
            }
            itemimage.setImageResource(pets[current_img]);
            if(mp != null && mp.isPlaying()){
                mp.stop();
            }
            current_aud++;
            if(current_aud == petsAudio.length){
                current_aud = 0;
            }
            mp.reset();
            mp = MediaPlayer.create(ViewPagerActivity.this, petsAudio[current_aud]);
            current_aud --;
            mp.start();
        }
    });

答案 1 :(得分:-1)

您必须在代码中添加此方法

public void cleanup() {
    if (mp != null && mp.isPlaying()) {
        mp.stop();
        mp.release();
        mp = null;
    }
}

并在

之前的两个按钮侦听器中调用此方法
MediaPlayer.create(MainActivity.this, audios[currentAudio]);