Android第三次作业

页面截图:


(播放暂停功能为一个按键)

代码:

player为MediaPlayer对象

播放功能:

public void play(){
    player.reset();
    try{
        player.setDataSource(musics.get(binder.getCurMusicIndex()));
        player.prepare();
        player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            public void onPrepared(MediaPlayer mediaPlayer) {
                player.start();
            }
        });
        player.seekTo(binder.getPausePosition());
        binder.setTotalMusicTime(player.getDuration());
    }catch (IOException e){
        e.printStackTrace();
    }
}

暂停:

public void pause(){
    if (player.isPlaying()){
        player.pause();
        binder.setPause(true);
        binder.setPausePosition(player.getCurrentPosition());
    }
}

停止:

public void stop(){
    if (player!=null){
        player.pause();
        player.stop();
        binder.setPause(false);
    }
    binder.setPausePosition(0);
    binder.setCurMusicIndex(0);
    binder.setPlayPosition(0);
}

上一首:

public void pre(){
    int curMusicIndex = binder.getCurMusicIndex();
    curMusicIndex-=1;
    if (curMusicIndex<0) {
        binder.setCurMusicIndex(musics.size() - 1);
        binder.setPausePosition(0);
        play();
    }else{
        binder.setCurMusicIndex(curMusicIndex);
        binder.setPausePosition(0);
        play();
    }
}

下一首:

public void next(){
    int curMusicIndex = binder.getCurMusicIndex();
    curMusicIndex+=1;
    if (curMusicIndex >=musics.size()){
        binder.setCurMusicIndex(0);
        binder.setPausePosition(0);
        play();
    }
    else{
        binder.setCurMusicIndex(curMusicIndex);
        binder.setPausePosition(0);
        play();
    }
}

播放列表的获得:

private void initListView() {
    musics = binder.getMusics();
    simpleMusics = binder.getSimpleMusics();
    adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, simpleMusics);
    musicLv.setAdapter(adapter);
}

Coding:

项目:
https://git.coding.net/midnightclad/Android3.git

APK:
https://coding.net/u/midnightclad/p/Android3/git/blob/master/1600802115JXS.apk

录制:

posted on 2018-12-11 13:21  ashenqh  阅读(157)  评论(0编辑  收藏  举报