- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个播放本地音频文件的 MediaPlayer
对象。我正在使用 TimerTask
在播放音频时更新 TextView 上 SeekBar 和文本的位置。
只有一个按钮。当用户按下按钮时,音频开始。当他们第二次按下时,音频停止。当他们第三次按下它再次开始播放时,应用程序崩溃并出现错误
'causedby: TimerTask is scheduled already and InvocationTargetException'.
这是我的代码:
public void onAudioClicked(View v) {
if (mainPlayer == null) {
mainPlayer = MediaPlayer.create(this, R.raw.session1);
// Get audio duration and set textview
int d = mainPlayer.getDuration();
TextView t = (TextView)findViewById(R.id.totalTime);
t.setText(String.format("%d:%d",
TimeUnit.MILLISECONDS.toMinutes(d),
TimeUnit.MILLISECONDS.toSeconds(d) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(d))));
sb.setProgress(0);
sb.setMax(mainPlayer.getDuration());
mainPlayer.start();
timer = new Timer();
timer.schedule(task, 1000);
} else {
mainPlayer.stop();
mainPlayer.release();
mainPlayer = null;
timer.cancel();
task.cancel();
timer = null;
}
}
TimerTask task = new TimerTask(){
public void run(){
int currentPosition = 0;
int total = mainPlayer.getDuration();
sb.setMax(total);
while (mainPlayer != null && currentPosition < total) {
try {
Thread.sleep(1000);
currentPosition = mainPlayer.getCurrentPosition();
runOnUiThread(updateControlsRunnable);
} catch (InterruptedException e) {
return;
} catch (Exception e) {
return;
}
sb.setProgress(currentPosition);
}
}
};
final Runnable updateControlsRunnable = new Runnable () {
public void run(){
int d = mainPlayer.getCurrentPosition();
TextView t = (TextView)findViewById(R.id.currentTime);
t.setText(String.format("%d:%d",
TimeUnit.MILLISECONDS.toMinutes(d),
TimeUnit.MILLISECONDS.toSeconds(d) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(d))));
}
};
最佳答案
请试试这个代码块:
sb.setProgress(0);
sb.setMax(mainPlayer.getDuration());
mainPlayer.start();
timer = new Timer();
MyTask task=new MyTask();
timer.schedule(task,0, 1000);
private class MyTask extends TimerTask {
public void run() {
//Your code...
}
}
关于java - TimerTask 已经安排好了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16509503/
我正在用 Java 编写应用程序,但遇到以下问题:一段时间后,显示了一个 inputDialog(JOptionPane)。因为间隔很短,即 10 秒,如果我延迟与第一个对话的交互,那么第二个将在我之
基本上我想做的是制作一个计时器,它在 x 秒后运行一个特定的 TimerTask,但是 TimerTask 可以重新安排计时器在 y 秒后执行任务。下面是示例,它在我尝试在 TimerTask 运行中
在下面的代码中,注释掉的部分会导致异常,该异常向我们解释了 timerTask2 已经被调度或取消,但是没有什么可以阻止 if 被执行器在下面的代码中重用和运行注释掉了部分。怎么解释呢? Timer
我编写了一个连接到服务器并向他发送 ping 命令的应用程序,服务器使用 pong 命令进行应答。 我想实现连接超时机制。我认为它将如下: 客户端发送 ping 并使用 timertask 和 del
我遇到以下问题。每隔 2 秒,程序就会进入 if 语句。在这个 if 语句中,我想要一个计时器,它会在 15 秒后给我一条消息。计时器应延迟 1 秒运行。但是当我用计时器“等待”时,if 语句将再执行
我正在开发一个 ODL 应用程序,并从我的主函数中安排了一个任务 ( MonitorLinksTask ),以便监视拓扑的所有链接中的延迟。 我想为每个链接发送一个数据包,并在 Controller
尝试使用 ScheduledExecutorService 来执行 TimerTask。安排一个延迟 10 秒的任务,并调用 task.cancel。但任务仍在运行,不确定会发生什么,并且取消方法似乎
无论如何,我正在尝试制作类似聊天程序的东西,有人告诉我使用此代码来检查新消息,同时允许用户提交消息: timer.schedule(new TimerTask() { @Override pu
看看这段代码: public Reminder() { a[0]=1000; a[1]=3000; a[2]=1000; a[3]=5000; timer = n
我有以下设计: 有一个任务扩展了TimerTask,并且计划每分钟运行一次。此任务将尝试从中央队列(作为单个消费者)获取项目并将其表示形式写入文件中。 此外,还有多个生产者不时将项目放入中央队列。 我
我在打印特定数组的每个值(在一定时间内)时遇到问题。例如,我有一个包含值的数组:“Value1”、“Value2”、“Value3”。我想输出“Value1”,5秒后“Value2”,5秒后“Valu
我对这个标题感到抱歉,但我不确定还有什么方法可以描述这个问题。我有一个 SingletonBean,其中有一个扩展 TimerTask 的类。要点是,当应用程序启动时,它开始循环运行一组计划的后台任务
我有一个程序,它启动一个计时器,然后在停止之前每秒重复一次代码。发生的情况是它不会停止来自 main 的执行。我假设它只是一个与之一起运行的线程,但通过查看帖子,我仍然不确定如何暂时停止它。 fina
我正在尝试使用每秒更新标签的时间(因此它显示倒计时)但它只似乎“滴答作响”一次,我无法弄清楚我做错了什么! public class Puzzle extends UiApplication {
我设置了 TimerTask UpdateTask,但它只在我启动程序时触发一次。为什么不继续触发? 这里的一些方法在其他类中,如果您需要它们,请随时告诉我。 import java.awt.Grap
我又碰壁了。让我的键输入工作后,我已经绞尽脑汁几个小时,我想创建一个暂停功能,这样如果再次按下同一个键,timertask 将停止运行(即游戏暂停) JPanel component = (JPane
我有一个 android 应用程序,我必须在 20-30 秒的非常短的时间间隔内执行一些任务。这是一项要求,它在某种程度上是一个“监控”应用程序,不会用于商业目的,因此无需担心电池和数据使用。 我正在
这是我的计时器类,该类旨在不断更新 View 中的计时器。但是,当我运行该应用程序时,第一条 toast 消息显示在屏幕上,但从未到达第二条消息(永远不会执行 timerTask 的“运行”方法)。我
我在我的android代码中添加了一个TimerTask: private class MyTimerTask extends TimerTask { @Override public
我有一个名为 timer 的任务: timer.schedule(new task1(), 1000*minutes); 任务: class task1 extends TimerTask {
我是一名优秀的程序员,十分优秀!