Android:postDelayed(模拟音乐播放进度模块)

简介:

postDelayed的用法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
public  class  MainActivity  extends  Activity  implements  OnClickListener
{
     /**
      * 音乐播放模块
      */
     private  ImageButton imageButton;
     private  ProgressBar progressBar;
     private  TextView textView;
     private  boolean  isPlay =  false ;
                     
     @Override
     protected  void  onCreate(Bundle savedInstanceState)
     {
         super .onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
                         
         imageButton = (ImageButton) findViewById(R.id.imageButton1);
         progressBar = (ProgressBar) findViewById(R.id.progressBar1);
         textView = (TextView) findViewById(R.id.textView1);
                         
         imageButton.setOnClickListener( this );
     }
                     
     @Override
     public  boolean  onCreateOptionsMenu(Menu menu)
     {
         getMenuInflater().inflate(R.menu.activity_main, menu);
         return  true ;
     }
                     
     @Override
     public  void  onClick(View v)
     {
         switch  (v.getId())
         {
         case  R.id.imageButton1:
             imgClick();
             break ;
                         
         default :
             break ;
         }
     }
                     
     private  void  imgClick()
     {
         isPlay = !isPlay;
         if (isPlay) //更换播放按钮图片
         {
             imageButton.setImageResource(R.drawable.ic_pause);
             play(); //调用播放方法
         }
         else
         {
             imageButton.setImageResource(R.drawable.ic_play);
         }
     }
                     
     private  int  playTime;
     private  Runnable action =  null ;
                     
     private  void  play()
     {
         new  Thread()
         {
             public  void  run()
             {
                                 
                 //使用postDelayed定时发送消息
                 action =  new  Runnable() // 创建action时并不执行run()方法
                 {
                     @Override
                     public  void  run()
                     {
                         if (isPlay)
                         {
                             playTime++;
                             textView.setText(playTime +  "" );
                             imageButton.postDelayed(action,  1000 ); // 回调action
                             progressBar.setProgress(playTime);
                         }
                     }
                 };
                 imageButton.postDelayed(action,  0 ); // 创建action后执行此方法,回调Runnable()里的run方法
                             
                                 
             };
         }.start();
     }
}



模拟音乐播放计时和进度条:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
public  class  MainActivity  extends  Activity  implements  OnClickListener,
         OnSeekBarChangeListener
{
     private  ImageButton imageButton;
     private  SeekBar seekBar;
     private  TextView textView_playtime;
     private  boolean  isPlay =  false ;
     private  Chronometer mTimer;
     private  TextView textView_lasttime;
     private  long  firstClickTime =  0 ;
          
          
          
     @Override
     protected  void  onCreate(Bundle savedInstanceState)
     {
         super .onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
              
         imageButton = (ImageButton) findViewById(R.id.imageButton1);
         seekBar = (SeekBar) findViewById(R.id.seekBar1);
         textView_playtime = (TextView) findViewById(R.id.textView_playtime);
         textView_lasttime = (TextView) findViewById(R.id.textView_lasttime);
         mTimer = (Chronometer) findViewById(R.id.chronometer1);
              
         imageButton.setOnClickListener( this );
         seekBar.setOnSeekBarChangeListener( this );
         seekBar.setMax(lastTime());
     }
          
     @Override
     public  boolean  onCreateOptionsMenu(Menu menu)
     {
         getMenuInflater().inflate(R.menu.activity_main, menu);
         return  true ;
     }
          
     @Override
     public  void  onClick(View v)
     {
         switch  (v.getId())
         {
             case  R.id.imageButton1:
                 imgClick();
                 break ;
                  
             default :
                 break ;
         }
     }
          
     private  void  imgClick()
     {
         //用户点击间隔在1秒之内,就不响应(不再启动新线程,避免连续点击的加速问题)
         long  secondClickTime = System.currentTimeMillis();
         if (secondClickTime - firstClickTime <  1000 )
         {
             return ;
         }
         firstClickTime = System.currentTimeMillis();
              
         isPlay = !isPlay;
         if  (isPlay) // 更换播放按钮图片
         {
             imageButton.setImageResource(R.drawable.ic_play);
             play(); // 调用播放方法
         }
         else
         {
             imageButton.setImageResource(R.drawable.ic_pause);
         }
     }
          
     private  int  playTime;
     private  Runnable action =  null ;
          
     private  void  play()  // 播放音乐
     {
         new  Thread()
         {
             public  void  run()
             {
                 action =  new  Runnable()
                 {
                     @Override
                     public  void  run()
                     {
                              
                         if  (isPlay)
                         {
                             if  (playTime > lastTime())
                             {
                                 prostop();
                             }
                             else
                             {
                                 textView_playtime.setText(progresstime(playTime));
                                 seekBar.setProgress(playTime);
                                 imageButton.postDelayed(action,  1000 );
                                 playTime +=  1000 ;
                             }
                         }
                     }
                 };
                 imageButton.postDelayed(action,  0 );
             };
         }.start();
     }
          
     private  void  prostop() //播放结束
     {
         new  Thread()
         {
             @Override
             public  void  run()
             {
                 Runnable actionStop =  new  Runnable()
                 {
                     @Override
                     public  void  run()
                     {
                         imageButton.setImageResource(R.drawable.ic_pause);
                         isPlay =  false ;
                         playTime =  0 ;
                         seekBar.setProgress(playTime);
                     }
                 };
                 imageButton.post(actionStop);
             }
         }.start();
     }
          
     // 得到结束时间的毫秒数
     private  int  lastTime()
     {
         String[] str = textView_lasttime.getText().toString().split( ":" );
         return  (Integer.parseInt(str[ 1 ]) + Integer.parseInt(str[ 0 ]) *  60 ) *  1000 ;
     }
          
     // 监听进度条方法
     @Override
     public  void  onProgressChanged(SeekBar seekBar,  int  progress,
             boolean  fromUser)
     {
         playTime = progress;
     }
          
     @Override
     public  void  onStartTrackingTouch(SeekBar seekBar)
     {
              
     }
          
     @Override
     public  void  onStopTrackingTouch(SeekBar seekBar)
     {
              
     }
          
     // 将毫秒数转换为时间格式
     private  String progresstime( int  progress)
     {
         Date date =  new  Date(progress);
         SimpleDateFormat format =  new  SimpleDateFormat( "mm:ss" );
         return  format.format(date);
     }
          
}








本文转自 glblong 51CTO博客,原文链接:http://blog.51cto.com/glblong/1206266,如需转载请自行联系原作者
目录
相关文章
|
7月前
|
编解码 监控 开发工具
如何同时启动Android平台GB28181设备接入模块和轻量级RTSP服务模块?
为什么要设计GB28181设备接入模块?GB28181接入SDK,实现不具备国标音视频能力的 Android终端,通过平台注册接入到现有的GB/T28181—2016服务,可用于如智能监控、智慧零售、智慧教育、远程办公、生产运输、智慧交通、车载或执法记录仪等场景。Android终端除支持常规的音视频数据接入外,还可以支持移动设备位置(MobilePosition)订阅和通知、语音广播和语音对讲、云台控制和预置位查询等。
|
7月前
|
编解码 Android开发 数据安全/隐私保护
Android平台外部编码数据(H264/H265/AAC/PCMA/PCMU)实时预览播放技术实现
好多开发者可能疑惑,外部数据实时预览播放,到底有什么用? 是的,一般场景是用不到的,我们在开发这块前几年已经开发了非常稳定的RTMP、RTSP直播播放模块,不过也遇到这样的场景,部分设备输出编码后(视频:H.264/H.265,音频:AAC/PCMA/PCMU)的数据,比如无人机或部分智能硬件设备,回调出来的H.264/H.265数据,除了想转推到RTMP、轻量级RTSP服务或GB28181外,还需要本地预览甚至对数据做二次处理(视频分析、实时水印字符叠加等,然后二次编码),基于这样的场景诉求,我们开发了Android平台外部编码数据实时预览播放模块。
|
4月前
|
XML Java Android开发
Android Studio App开发中使用录音机、MediaRecorder录制音频和MediaPlayer播放音频讲解及实战(附源码)
Android Studio App开发中使用录音机、MediaRecorder录制音频和MediaPlayer播放音频讲解及实战(附源码)
74 0
|
1天前
|
Android开发 内存技术
Android 通过tinyalsa调试解决录制和播放音频问题
Android 通过tinyalsa调试解决录制和播放音频问题
11 1
|
2天前
|
Android开发
Android RIL 动态切换 4G 模块适配
Android RIL 动态切换 4G 模块适配
5 0
|
2月前
|
XML Java API
安卓逆向 -- Xposed模块编写
安卓逆向 -- Xposed模块编写
16 0
|
7月前
|
编解码 开发工具 Android开发
Android平台如何实现外部RTSP|RTMP流注入轻量级RTSP服务模块(内网RTSP网关)
今天分享的是外部RTSP或RTMP流,拉取后注入到本地轻量级RTSP服务模块,供内网小并发场景下使用,这里我们叫做内网RTSP网关模块。
|
3月前
|
开发工具 Android开发
Android平台RTMP推送|轻量级RTSP服务|GB28181设备接入模块之实时快照保存JPG还是PNG?
Android平台RTMP推送|轻量级RTSP服务|GB28181设备接入模块之实时快照保存JPG还是PNG?
|
4月前
|
XML Java 调度
Android开发音效增强中铃声播放Ringtone及声音池调度SoundPool的讲解及实战(超详细 附源码)
Android开发音效增强中铃声播放Ringtone及声音池调度SoundPool的讲解及实战(超详细 附源码)
51 0
|
4月前
|
XML Java Android开发
Android App开发实战项目之仿手机QQ动感影集动画播放(附源码和演示视频 可直接使用)
Android App开发实战项目之仿手机QQ动感影集动画播放(附源码和演示视频 可直接使用)
29 0