600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > 57.音乐播放器的进度条

57.音乐播放器的进度条

时间:2021-02-19 04:43:09

相关推荐

57.音乐播放器的进度条

给音乐播放器添加进度条:

SeekBar:除了反应播放进度,还可以拖动

ProgressBar:只能显示当前的进度,不能进行拖动编辑

自动改变进度条

音乐播放器通过下面的方法来获取进度条的总时间和当前播放的时间,为了防止阻塞主线程,通过Timer来在子线程中实现,再在主线程中刷新UI。

获取进度条的状态,同时通过Message把数据发送到主线程里面进行刷新,这里面使用bundle封装逐句,key-value的形式封装管理要发送的数据:

public void addTimer(){//计时器Timer,第二个参数是延迟,指代码执行以后5ms以后第一次执行run方法,以后每//500ms执行一次。最后一个参数是执行run方法的间隔时间500,if(timer == null){timer = new Timer();timer.schedule(new TimerTask(){@Overridepublic void run() {// TODO Auto-generated method stub//获取歌曲的时长,单位是毫秒int length = player.getDuration();//获取歌曲当前的播放进度int currentLength = player.getCurrentPosition();//想MainActivity发消息携带数据Message msg = MainActivity.handler.obtainMessage();//把进度封装到对象中Bundle bundle = new Bundle();bundle.putInt("length", length);bundle.putInt("currentLength", currentLength);msg.setData(bundle);//发送到主线程MainActivity.handler.sendMessage(msg);}}, 5, 500);}}

主线程中刷新UI,把Handle设置成静态的就可以获取到服务里面的数据,获取的数据以后,设置进度条的长度和当前的进度:

//定义成静态来获取mediaService的进度条static Handler handler = new Handler(){public void HandleMessage(android.os.Message msg){//刷新seekbar的UIBundle bundle = msg.getData();int length = bundle.getInt("length");int currentLength = bundle.getInt("currentLength");sb.setMax(length);sb.setProgress(currentLength);}};

实现进度条的拖动:

主线程中监听进度条,然后在服务中实现进度条的拖动方法

监听:

//监听seekbar,实现seekbar进度的改变sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){@Overridepublic void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {System.out.println("手指滑动");}@Overridepublic void onStartTrackingTouch(SeekBar seekBar) {System.out.println("手指按下");}@Overridepublic void onStopTrackingTouch(SeekBar seekBar) {System.out.println("手指抬起");//根据拖动进度改变进度条int progress = seekBar.getProgress();//改变进度mi.seekTo(progress);}});

更新进度条调用seekTo来改变当前的进度

//改变当前的进度public void seekTo(int process){player.seekTo(process);}

案例展示:

activity_main.xml

<LinearLayout xmlns:android="/apk/res/android"xmlns:tools="/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context=".MainActivity" android:orientation="vertical"><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="播放" android:onClick="play"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="继续播放" android:onClick="continuePlay"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="暂停" android:onClick="pause"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="退出" android:onClick="quit"/><SeekBar android:id="@+id/sb"android:layout_width="match_parent"android:layout_height="wrap_content"/></LinearLayout>

mediaInterface.java

package com.ldw.mediaPlayer;public interface mediaInterface {void play();void pause();void continuePlay();void seekTo(int progress);}

mediaService.java

package com.ldw.mediaPlayer;import java.util.Timer;import java.util.TimerTask;import android.app.Service;import android.content.Intent;import android.media.MediaPlayer;import android.media.MediaPlayer.OnPreparedListener;import android.os.Binder;import android.os.Bundle;import android.os.IBinder;import android.os.Message;public class mediaService extends Service {MediaPlayer player;Timer timer;@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubreturn new zhongjian();}@Overridepublic void onCreate(){super.onCreate();player = new MediaPlayer();}//停止播放在服务销毁的时候调用@Overridepublic void onDestroy(){super.onDestroy();//关闭播放器player.stop();//释放资源,释放掉player对象player.release();player = null;//停掉计时器if(timer != null){timer.cancel();timer = null;}}//必须继承binder才能作为中间对象被返回class zhongjian extends Binder implements mediaInterface{public void play(){mediaService.this.play();}public void pause(){mediaService.this.pause();}@Overridepublic void continuePlay() {// TODO Auto-generated method stubmediaService.this.continuePlay();}@Overridepublic void seekTo(int progress) {// TODO Auto-generated method stubmediaService.this.seekTo(progress);}}//播放音乐public void play(){//重置player.reset();try {//player.setDataSource("sdcard/1.mp3");//设置加载网络资源player.setDataSource("sdcard/1.mp3");//异步准备player.prepareAsync();//准备侦听,开始播放player.setOnPreparedListener(new OnPreparedListener(){@Overridepublic void onPrepared(MediaPlayer mp) {// TODO Auto-generated method stubplayer.start();addTimer();}});//player.prepare();//player.start();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}//暂停播放public void pause(){player.pause();}//继续播放public void continuePlay(){player.start();}//改变当前的进度public void seekTo(int process){player.seekTo(process);}public void addTimer(){//计时器Timer,第二个参数是延迟,指代码执行以后5ms以后第一次执行run方法,以后每//500ms执行一次。最后一个参数是执行run方法的间隔时间500,if(timer == null){timer = new Timer();timer.schedule(new TimerTask(){@Overridepublic void run() {// TODO Auto-generated method stub//获取歌曲的时长,单位是毫秒int length = player.getDuration();//获取歌曲当前的播放进度int currentLength = player.getCurrentPosition();//想MainActivity发消息携带数据Message msg = MainActivity.handler.obtainMessage();//把进度封装到对象中Bundle bundle = new Bundle();bundle.putInt("length", length);bundle.putInt("currentLength", currentLength);msg.setData(bundle);//发送到主线程MainActivity.handler.sendMessage(msg);}}, 5, 500);}}}

MainActivity.java

package com.ldw.mediaPlayer;import android.app.Activity;import ponentName;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.Handler;import android.os.IBinder;import android.view.View;import android.widget.SeekBar;import android.widget.SeekBar.OnSeekBarChangeListener;public class MainActivity extends Activity {//定义成静态来获取mediaService的进度条static Handler handler = new Handler(){public void HandleMessage(android.os.Message msg){//刷新seekbar的UIBundle bundle = msg.getData();int length = bundle.getInt("length");int currentLength = bundle.getInt("currentLength");//刷新进度条sb.setMax(length);sb.setProgress(currentLength);}};mediaInterface mi;private mediaServiceConn conn;private Intent intent;private static SeekBar sb;@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//获取seekbarsb = (SeekBar)findViewById(R.id.sb);//监听seekbar,实现seekbar进度的改变sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){@Overridepublic void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {System.out.println("手指滑动");}@Overridepublic void onStartTrackingTouch(SeekBar seekBar) {System.out.println("手指按下");}@Overridepublic void onStopTrackingTouch(SeekBar seekBar) {System.out.println("手指抬起");//根据拖动进度改变进度条int progress = seekBar.getProgress();//改变进度mi.seekTo(progress);}});intent = new Intent(this, com.ldw.mediaPlayer.mediaService.class);conn = new mediaServiceConn();//混合调用service,避免服务avtivy关闭服务就关闭了。顺序是先startService再bindService,关闭是先解绑再停止//把服务所在的进程变成服务进程startService(intent);//拿到中间对象bindService(intent, conn, BIND_AUTO_CREATE);}class mediaServiceConn implements ServiceConnection{@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {// TODO Auto-generated method stubmi = (mediaInterface) service;}@Overridepublic void onServiceDisconnected(ComponentName name) {// TODO Auto-generated method stub}}//开始播放按钮public void play(View v){mi.play();}//暂停播放按钮public void pause(View v){mi.pause();}//继续播放public void continuePlay(View v){mi.continuePlay();}//退出public void quit(View v){unbindService(conn);stopService(intent);}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。