gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 02:40:33 25 4
gpt4 key购买 nike

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

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

});

最佳答案

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

}

关于android - 在android中同时播放图像和音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25822125/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com