首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

android音乐播放器的惯用操作

2013-09-05 
android音乐播放器的常用操作1. [代码]变量申明01/*变量声明*/02private ImageButton playBtn null//播

android音乐播放器的常用操作

1. [代码]变量申明     01/*变量声明*/02private ImageButton playBtn = null;//播放、暂停03private ImageButton latestBtn = null;//上一首04private ImageButton nextButton = null;//下一首05private ImageButton forwardBtn = null;//快进06private ImageButton rewindBtn = null;//快退07private TextView playtime = null;//已播放时间08private TextView durationTime = null;//歌曲时间09private SeekBar seekbar = null;//歌曲进度10private Handler handler = null;//用于进度条11private Handler fHandler = null;//用于快进12private int currentPosition;//当前播放位置

2. [代码]获得上一个activity传来的数据     01@Override02protected void onCreate(Bundle savedInstanceState) {03    super.onCreate(savedInstanceState);04    setContentView(R.layout.play);05    Intent intent = this.getIntent();06    Bundle bundle = intent.getExtras();07    _ids = bundle.getIntArray("_ids");    //获得保存音乐文件_ID的数组08    position = bundle.getInt("position"); //获得应该播放的音乐的号数,既播放第几首09        //代码未完,见下面的代码10}

3. [代码]初始化控件     1playtime = (TextView)findViewById(R.id.playtime);         //显示已经播放的时间2durationTime = (TextView)findViewById(R.id.duration);     //显示歌曲总时间3playBtn = (ImageButton)findViewById(R.id.playBtn);       //开始播放、暂停播放按钮4latestBtn = (ImageButton)findViewById(R.id.latestBtn);   //播放上一首按钮5nextButton = (ImageButton)findViewById(R.id.nextBtn);    //播放下一首按钮6forwardBtn = (ImageButton)findViewById(R.id.forwardBtn); //快进按钮7rewindBtn = (ImageButton)findViewById(R.id.rewindBtn);   //快退按钮8seekbar = (SeekBar)findViewById(R.id.seekbar);           //播放进度条

4. [代码]定义各控件的回调函数     跳至 [1] [2] [3] [4] [全屏预览]view sourceprint?001playBtn.setOnClickListener(new View.OnClickListener() { //点击“播放、暂停”按钮时回调002    @Override003    public void onClick(View v) {              004        if (mp.isPlaying()){                     //如果正在播放则暂停005            pause();006            playBtn.setBackgroundResource(007                 R.drawable.play_selecor);   //更改按键状态图标008        else{                                  //如果没有播放则恢复播放009            play();010            playBtn.setBackgroundResource(011                R.drawable.pause_selecor);   //更改按键状态图标012                     013        }014    }015});016 017latestBtn.setOnClickListener(new View.OnClickListener() {//点击“播放上一首”按钮时回调          018    @Override019    public void onClick(View v) {020        int num = _ids.length;                  //获得音乐的数目021        if(position==0){                        //如果已经时第一首则播放最后一首022            position=num-1;                                    023        }else{                                  //否则播放上一首024            position-=1;025        }026        int pos = _ids[position];              //得到将要播放的音乐的_ID027        setup();                               //做播放前的准备工作028        play();                    //开始播放029    }030});031 032nextButton.setOnClickListener(new View.OnClickListener(){//点击“播放下一首”按钮时回调          033    @Override034    public void onClick(View v) {               035        int num = _ids.length;                 //获得音乐的数目036        if (position==num-1){                  //如果已经是最后一首,则播放第一首037            position=0;038        }else{039            position+=1;                  //否则播放下一首040        }041        int pos = _ids[position];             //得到将要播放的音乐的_ID042        setup();                              //做播放前的准备工作043        play();                               //开始播放044    }045});046 047forwardBtn.setOnTouchListener(new OnTouchListener() {    //点击“快进”按钮时回调048    @Override049    public boolean onTouch(View v, MotionEvent event) {050        switch (event.getAction()) {051            case MotionEvent.ACTION_DOWN:052                fHandler.post(forward); //此处使用handler对象更新进度条053                mp.pause();     //点击快进按钮时,音乐暂停播放                           054                break;055 056            case MotionEvent.ACTION_UP:057                fHandler.removeCallbacks(forward);         058                mp.start();     //松开快进按钮时,音乐暂恢复播放                          059                playBtn.setBackgroundResource(060                    R.drawable.pause_selecor);061                break;062        }063        return false;064    }065});066 067rewindBtn.setOnTouchListener(new OnTouchListener() {    //点击“快退”按钮时回调      068    @Override069    public boolean onTouch(View v, MotionEvent event) {070        switch (event.getAction()) {071            case MotionEvent.ACTION_DOWN:  072                fHandler.post(rewind);         073                mp.pause(); //点击快退按钮时,音乐暂暂停播放074                break;075 076            case MotionEvent.ACTION_UP:077                fHandler.removeCallbacks(rewind);078                mp.start(); //松开快退按钮时,音乐暂恢复播放079                playBtn.setBackgroundResource(080                    R.drawable.pause_selecor);081                break;082        }083        return false;084    }085});086 087seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {         088    @Override089    public void onStopTrackingTouch(SeekBar seekBar) {090        mp.start();     //停止拖动进度条时,音乐开始播放091    }092    @Override093    public void onStartTrackingTouch(SeekBar seekBar) {094        mp.pause();     //开始拖动进度条时,音乐暂停播放095    }096             097    @Override098    public void onProgressChanged(SeekBar seekBar, int progress,099        boolean fromUser) {100        if(fromUser){101            mp.seekTo(progress);    //当进度条的值改变时,音乐播放器从新的位置开始播放102        }103    }104});

热点排行