在android中同时播放图像和音频

时间:2014-09-13 10:10:45

标签: android audio imageview android-mediaplayer

我正在尝试在Android中同时播放音频和图像...我有一个图像视图和两个按钮用于返回并转到下一个..当我点击下一个按钮并继续其工作完全正常但是当我点击后退按钮时,只有音频文件不会改变图像。如果我再次单击后退按钮然后图像更改但使用另一个音频文件..请帮我解决这个问题。谢谢这里是我的代码

    public class Main extends Activity{

Button btn_back,btn_play,btn_next;
ImageView img_view;
int current_img = 0;
int current_aud = 0;
MediaPlayer mp;

int[] images = {R.drawable.img1,R.drawable.img2,R.drawable.img3,R.drawable.img4,R.drawable.img5,
                R.drawable.img6,R.drawable.img7,R.drawable.img8,R.drawable.img9,R.drawable.img10,
                R.drawable.img11,R.drawable.img12};

int[] audio = {R.raw.aud,R.raw.a,R.raw.aud,R.raw.a,R.raw.aud,R.raw.a,R.raw.aud,R.raw.a,
                R.raw.aud,R.raw.a,R.raw.aud,R.raw.a};

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    btn_back = (Button)findViewById(R.id.button1);
    btn_play = (Button)findViewById(R.id.button2);
    btn_next = (Button)findViewById(R.id.button3);
    img_view = (ImageView)findViewById(R.id.imageView1);
    img_view.setImageResource(R.drawable.ic_launcher);
    mp =new MediaPlayer();

    btn_play.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
        mp.stop();  
        }
    });

    btn_next.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            current_img = current_img % images.length;
            img_view.setImageResource(images[current_img]);
            current_img ++;
        try {
            if(mp != null){
                if(mp.isPlaying()){
                    mp.stop();
                //  mp.release();
                }
            }


            current_aud = current_aud % audio.length;

            mp.reset();
            mp = MediaPlayer.create(Main.this, audio[current_aud]);
            mp.start();
             current_aud ++;





        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        }
    });

    btn_back.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            current_img = (current_img + images.length) % images.length;
            current_img --;
            img_view.setImageResource(images[current_img]);
            try {
                if(mp != null){
                    if(mp.isPlaying()){
                        mp.stop();
                        // mp.reset();
                    }
                }



                current_aud = (current_aud + audio.length)% audio.length;
                mp.reset();
                mp = MediaPlayer.create(Main.this, audio[current_aud]);
                current_aud --;
                mp.start();





            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }   
            }

    });

2 个答案:

答案 0 :(得分:1)

    public class Main extends Activity implements OnClickListener{

        Button btn_back,btn_play,btn_next;
        ImageView img_view;
        int current_img = 0;
        int current_aud = 0;
        MediaPlayer mp;

        int[] images = {R.drawable.img1,R.drawable.img2,R.drawable.img3,R.drawable.img4,R.drawable.img5,
                    R.drawable.img6,R.drawable.img7,R.drawable.img8,R.drawable.img9,R.drawable.img10,
                    R.drawable.img11,R.drawable.img12};

        int[] audio = {R.raw.aud,R.raw.a,R.raw.aud,R.raw.a,R.raw.aud,R.raw.a,R.raw.aud,R.raw.a,
                    R.raw.aud,R.raw.a,R.raw.aud,R.raw.a};

        @Override
        protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        btn_back = (Button)findViewById(R.id.button1);
        btn_play = (Button)findViewById(R.id.button2);
        btn_next = (Button)findViewById(R.id.button3);
        img_view = (ImageView)findViewById(R.id.imageView1);
        img_view.setImageResource(R.drawable.ic_launcher);
        mp =new MediaPlayer();

        btn_play.setOnClickListener(this);

        btn_next.setOnClickListener(this);

        btn_back.setOnClickListener(this);
        }

        @Override
        public void OnClick(View view){
            switch(view.getId()){
                case R.id.button1:
                    current_img --;
                    if(current_img == -1){
                        current_img = images.length-1;
                    }
                    img_view.setImageResource(images[current_img]);             
                    if(mp != null && mp.isPlaying()){
                        mp.stop();
                    }
                    current_aud--;
                    if(current_aud == -1){
                        current_aud = audio.length-1;
                    }
                    mp.reset();
                    mp = MediaPlayer.create(Main.this, audio[current_aud]);
                    current_aud --;
                    mp.start();
                    break;

                case R.id.button3:
                    current_img ++;
                    if(current_img == images.length){
                        current_img = 0;
                    }
                    img_view.setImageResource(images[current_img]);             
                    if(mp != null && mp.isPlaying()){
                        mp.stop();
                    }
                    current_aud++;
                    if(current_aud == audio.length){
                        current_aud = 0;
                    }
                    mp.reset();
                    mp = MediaPlayer.create(Main.this, audio[current_aud]);
                    current_aud --;
                    mp.start();
                    break;

                    case R.id.button2:
                        if(mp != null && mp.isPlaying()){
                            mp.stop();
                        }
                        break;
            }
        }

    }

答案 1 :(得分:0)

您只需要为Next更改此内容

 if (currentimg < (media_list.size())) {
    currentimg+= 1;

    } else {
    currentimg = 0;

    }

和后面的

if (currentimg > 0) {
currentimg -= 1;

} else {
currentimg = 0;

}

只需添加它。它应该工作