gpt4 book ai didi

Android:简单的如何在给定时间后停止媒体播放器

转载 作者:行者123 更新时间:2023-11-29 02:03:02 24 4
gpt4 key购买 nike

我正在尝试播放 mp3 文件(使用 onClickListener)并在 2 秒后停止。我尝试了下面的代码,但它不起作用。有人可以帮忙吗?

final MediaPlayer mpsound = MediaPlayer.create(this, R.raw.media_player_sound);

ImageView sound = (ImageView) findViewById(R.id.button);

sound.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mpsound.start();{
sleep(2000);
mpsound.stop();
}
}
});

最佳答案

如果您使用 mpsound 播放音频,为什么要在 mpfrog 上调用 stop()?您需要在 mpsound MediaPlayer 上调用 stop() 函数。此外,您可能希望将 @Override 注释添加到您的 onClick() 方法。

为了覆盖...

sound.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mpsound.start();{
sleep(2000);
mpsound.stop();
}
}
});

一个定时器......

Handler handler = new Handler(){
@Override
public void handleMessage(Message msg){
mpsound.stop();
}
};

//Task for timer to execute when time expires
class SleepTask extends TimerTask {
@Override
public void run(){
handler.sendEmptyMessage(0);
}
}

//then in some other function...
Timer timer = new Timer("timer",true);
timer.schedule(new SleepTask(),2000);

关于Android:简单的如何在给定时间后停止媒体播放器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11550789/

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