自定义音乐播放进度条
2010-04-13 15:49
309 查看
[前提]
* android 自身也提供了该接口 似乎是:MediaController 发现极丑
[要求]
1. 进度条控件打算使用系统提供的SeekBar
2. SeekBar 要支持拖拉功能 即:定点播放
3. SeekBar 要反映播放位置 即:播放到哪 SeekBar 就在哪
[原理]
1. 音乐定点播放:MediaPlayer.seekTo(int msecond) //单位:毫秒
2. 音乐文件播放时间:MediaPlayer.getDuration()
3. SeekBar 获取位置:SeekBar.getProgress()
4. SeekBar 最大值: SeekBar.getMax()
[代码 步骤]
定义界面:main.xml
* Button
:
播放控制
如:
暂
停
继续
* TextView
:
显
示播放百分比
* SeekBar
:
进
度条
* RadioGroup
:
显
示所有sdcard
音
乐
文件
View初始化
拖动SeekBar,且播放指定位置的音乐
刷新播放位置,且使其实时变化
//mediaplayer没有播放进度的回调函数,所以只能开辟一个Thread定时使其刷新
Thread具体实现代码:
样图:
* android 自身也提供了该接口 似乎是:MediaController 发现极丑
[要求]
1. 进度条控件打算使用系统提供的SeekBar
2. SeekBar 要支持拖拉功能 即:定点播放
3. SeekBar 要反映播放位置 即:播放到哪 SeekBar 就在哪
[原理]
1. 音乐定点播放:MediaPlayer.seekTo(int msecond) //单位:毫秒
2. 音乐文件播放时间:MediaPlayer.getDuration()
3. SeekBar 获取位置:SeekBar.getProgress()
4. SeekBar 最大值: SeekBar.getMax()
[代码 步骤]
定义界面:main.xml
* Button
:
播放控制
如:
暂
停
继续
* TextView
:
显
示播放百分比
* SeekBar
:
进
度条
* RadioGroup
:
显
示所有sdcard
音
乐
文件
1. <?xml version="1.0" encoding="utf-8"?> 2. <LinearLayout xmlns:android="<a href="http://schemas.android.com/apk/res/android" mce_href="http://schemas.android.com/apk/res/android">http://schemas.android.com/apk/res/android</a>" 3. android:orientation="vertical" 4. android:layout_width="fill_parent" 5. android:layout_height="fill_parent" 6. > 7. <LinearLayout xmlns:android="<a href="http://schemas.android.com/apk/res/android" mce_href="http://schemas.android.com/apk/res/android">http://schemas.android.com/apk/res/android</a>" 8. android:orientation="horizontal" 9. android:layout_width="fill_parent" 10. android:layout_height="wrap_content" 11. > 12. <Button 13. android:id="@+id/cmd" 14. android:text="Loading..." 15. android:layout_width="90dip" 16. android:layout_height="wrap_content" 17. android:singleLine="true" 18. /> 19. <TextView 20. android:id="@+id/progress" 21. android:text="Progress.." 22. android:layout_width="50dip" 23. android:layout_height="fill_parent" 24. android:gravity="center" 25. android:singleLine="true" 26. /> 27. </LinearLayout> 28. <SeekBar 29. android:id="@+id/seekb" 30. android:max="100" 31. android:layout_width="fill_parent" 32. android:layout_height="wrap_content" 33. /> 34. </LinearLayout>
View初始化
1. public void initialize(){ 2. 3. sBar = (SeekBar)findViewById(R.id.seekb); 4. rGroup = (RadioGroup)findViewById(R.id.radio); 5. cmdButton = (Button)findViewById(R.id.cmd); 6. 7. mPlayer = new MediaPlayer(); 8. }
拖动SeekBar,且播放指定位置的音乐
1. sBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){ 2. 3. @Override 4. public void onProgressChanged(SeekBar seekBar, int progress, 5. boolean fromUser) { 6. // TODO Auto-generated method stub 7. 8. } 9. 10. @Override 11. public void onStartTrackingTouch(SeekBar seekBar) { 12. // TODO Auto-generated method stub 13. } 14. 15. @Override 16. public void onStopTrackingTouch(SeekBar seekBar) { 17. // TODO Auto-generated method stub 18. int dest = seekBar.getProgress(); 19. 20. int mMax = mPlayer.getDuration(); 21. int sMax = sBar.getMax(); 22. 23. mPlayer.seekTo(mMax*dest/sMax); 24. 25. } 26. 27. });
刷新播放位置,且使其实时变化
//mediaplayer没有播放进度的回调函数,所以只能开辟一个Thread定时使其刷新
1. public void startProgressUpdate(){ 2. //开辟Thread 用于定期刷新SeekBar 3. DelayThread dThread = new DelayThread(100); 4. dThread.start(); 5. }
Thread具体实现代码:
1. private Handler mHandle = new Handler(){ 2. @Override 3. public void handleMessage(Message msg){ 4. int position = mPlayer.getCurrentPosition(); 5. 6. int mMax = mPlayer.getDuration(); 7. int sMax = sBar.getMax(); 8. 9. sBar.setProgress(position*sMax/mMax); 10. } 11. }; 12. 13. public class DelayThread extends Thread { 14. int milliseconds; 15. 16. public DelayThread(int i){ 17. milliseconds = i; 18. } 19. public void run() { 20. while(true){ 21. try { 22. sleep(milliseconds); 23. } catch (InterruptedException e) { 24. // TODO Auto-generated catch block 25. e.printStackTrace(); 26. } 27. 28. mHandle.sendEmptyMessage(0); 29. } 30. } 31. }
样图:
相关文章推荐
- 一个可以实时显示进度的自定义view,类似于listview中音乐播放背景变化的效果
- android 播放音乐-进度条
- android 音乐播放 圆形进度条
- 自定义实现圆形播放进度条
- ios开发-AVPlayer 音乐播放自定义经典例题
- 自定义音乐播放转盘
- 音乐播放与进度拖拽原理
- 我的Android进阶之旅------>Android如何通过自定义SeekBar来实现视频播放进度条
- 圆形图片 周边进度 Progress 音乐播放进度
- iOS自定义控制中心音乐播放 锁屏界面背景图片
- Android自定义SeekBar实现视频播放进度条
- 音乐播放器--简单实现播放、暂停、随机播放、音乐进度条(第一版)
- 自定义MediaPlayer播放音乐路劲(内存,内存卡,网络)
- 音乐播放器项目技术之一音乐播放进度及音量的控制
- Android之MediaPlayer播放音乐并实现进度条实例
- Android 自定义 View - 适用于音乐播放的“条形与波浪”可视化效果
- 自定义圆形播放按钮进度条
- 初级音乐播放器——播放存储卡上的一首音乐,能够显示歌曲名和播放进度,并且能够播放和暂停(没有SD卡会报错)
- 基于vue2.0实现音乐/视频播放进度条组件的思路及具体实现方法+代码解释
- 获取音乐文件的播放时间及当前进度