gpt4 book ai didi

java - 音频暂停时暂停变化的图像

转载 作者:行者123 更新时间:2023-12-01 09:06:59 24 4
gpt4 key购买 nike

我正在构建一个应用程序,其中图像在播放音频时开始变化。当单击按钮播放(开始)音频时,图像也开始以不同的延迟变化,我也可以暂停音频。我面临并感到困惑的问题是,当我暂停音频时,

How can i pause my images as well ,so that when i click the button again,my audio resumes and images starts changing next from where they were paused.

private Button btn_play;
MediaPlayer mp;
int duration;
private ImageView imageView;
Handler handler = new Handler();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_names);
int resID = getResources().getIdentifier("audio", "raw", getPackageName());
mp = MediaPlayer.create(NamesOfALLAH.this, resID);
duration = mp.getDuration();
btn_play = (Button) findViewById(R.id.btn_play);
imageView = (ImageView) findViewById(R.id.imageView_Names);
btn_play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mp.isPlaying()) {

mp.pause();
btn_play.setText("Play");

} else {
mp.start();
btn_play.setText("Pause");
changeImage1();
changeImage2();
changeImage3();
changeImage4();
changeImage5();


}


}
});

}


public void changeImage1() {

handler.postDelayed(new Runnable() {
@Override
public void run() {

imageView.setImageResource(R.drawable.a1);

}

}, 6000);

}

public void changeImage2() {


handler.postDelayed(new Runnable() {
@Override
public void run() {
imageView.setImageResource(R.drawable.a2);


}
}, 8000);

}

public void changeImage3() {




handler.postDelayed(new Runnable() {
@Override
public void run() {
imageView.setImageResource(R.drawable.a3);


}
}, 10000);
}




public void changeImage4() {

handler.postDelayed(new Runnable() {
@Override
public void run() {
imageView.setImageResource(R.drawable.a4);


}
}, 11000);

}

public void changeImage5() {

handler.postDelayed(new Runnable() {
@Override
public void run() {
imageView.setImageResource(R.drawable.a5);


}
}, 12000);
}

`

最佳答案

使用 ImageSwitcher ,它会为您完成一些工作。无论如何,如果您想自己创建动画,则必须将所有更改作为一个整体来考虑。例如,您可以将可运行对象重新发布到处理程序。伪代码:

handler.postDelayed(new Runnable() {
@Override
public void run() {
// set the image you want
if (mustContinue)
handler.postDelayed(this, time);
}
}, 10000);

每次按下播放/暂停按钮时切换 boolean 值“mustContinue”。

编辑:检查this举个例子。

关于java - 音频暂停时暂停变化的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41191833/

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