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,如需转载请自行联系原作者
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值