Android 进度条的实现---seekbar

实现效果比较不错,接下来粘一下代码


Service代码:(主要是使用Binder提供对外方法)

public class testDetailService extends Service {
    private static final String TAG = "PlayerService";
    private String url;
    private MediaPlayer mediaPlayer;

    public testDetailService() {
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        ///storage/emulated/0/netease/cloudmusic/Music/孟大宝 房东的猫 - 秋又来了.mp3
        mediaPlayer = MediaPlayer.create(testDetailService.this, Uri.parse("file://" + "/storage/emulated/0/netease/cloudmusic/Music/孟大宝 房东的猫 - 秋又来了.mp3"));
        mediaPlayer.setLooping(true);
        mediaPlayer.start();

        mediaPlayer.getCurrentPosition();
        return super.onStartCommand(intent, flags, startId);
    }


    @Override
    public IBinder onBind(Intent intent) {
        return new MyBind();
    }


    public class MyBind extends Binder {
        //获取歌曲长度
        public int getMusicDuration() {
            int duration = 0;
            if (mediaPlayer != null) {
                duration = mediaPlayer.getDuration();
            }
            return duration;
        }

        //获取当前播放位置
        public int getMusicCurrentPosition() {
            int pos = 0;
            if (mediaPlayer != null) {
                pos = mediaPlayer.getCurrentPosition();
            }
            return pos;
        }

        //设置指定位置
        public void seekTo(int position) {
            if (mediaPlayer != null) {
                mediaPlayer.seekTo(position);
            }
        }
    }
}



detail.java中的代码---主要是与service进行通信(主要还是获取到刚才暴露出来的方法的值)

btn_startService.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(DetailActivity.this, testDetailService.class);
                startService(intent);

                if (serviceConnection == null) {
                    serviceConnection = new ServiceConnection() {
                        @Override
                        public void onServiceConnected(ComponentName name, IBinder service) {
                            myBind = (testDetailService.MyBind) service;
                            //设置进度条的最大长度
                            int max = myBind.getMusicDuration();
                            seekBar.setMax(max);
                            Log.e(TAG, "onServiceConnected: max---" + max);
                            seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                                @Override
                                public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                                    if(isTouch){
                                        myBind.seekTo(progress);
                                    }
                                    //myBind.seekTo(progress);
                                }

                                @Override
                                public void onStartTrackingTouch(SeekBar seekBar) {
                                    isTouch = true;
//                                    Toast.makeText(DetailActivity.this, "on", Toast.LENGTH_SHORT).show();
                                }

                                @Override
                                public void onStopTrackingTouch(SeekBar seekBar) {
                                    isTouch = false;
//                                    Toast.makeText(DetailActivity.this, "out", Toast.LENGTH_SHORT).show();
                                }
                            });
                            Timer timer = new Timer();
                            TimerTask timerTask = new TimerTask() {
                                @Override
                                public void run() {
                                    seekBar.setProgress(myBind.getMusicCurrentPosition());
                                }
                            };
                            timer.schedule(timerTask,0,10);
                        }

                        @Override
                        public void onServiceDisconnected(ComponentName name) {

                        }
                    };
                    bindService(intent,serviceConnection, Context.BIND_AUTO_CREATE);
                }
            }
        });


大概就是这样

你可能感兴趣的:(Android文章,MTMusic制作心得)