Android第三次作业

Android第三次作业

一. 功能介绍

此音乐播放器可实现播放音乐,通过进度条控制歌曲的功能,在听歌曲的同时,还可以得到歌曲的详细信息。

二. 实现代码

1. 设置播放功能

点击播放音乐,不过需要判断一下当前是否有音乐在播放,需要关闭正在播放的,position 可以获取到点击的是哪一个,去 musicList 里寻找播放

musicListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                currentposition = position;
        player(currentposition);
    }
});

2. 设置暂停功能

private void pause() {
    intent.putExtra("MSG","1");
    isplay = false;
    btn_play_pause.setBackgroundResource(R.drawable.play);
    startService(intent);
}

3. 设置切歌功能

切换上一曲

public  void previousMusic() {
               if(currentposition > 0){
                 currentposition -= 1;
                    player();
                   }else{
                   showInfo("已经是第一首音乐了");
                 }
               }

切换下一曲

private void nextMusic() {
                if(currentposition < musicList.size()-2){
                   currentposition += 1;
                   player();
               }else{
                   showInfo("已经是最后一首音乐了");
                 }
                }

4. 设置进度条

打开歌曲文件后,可拖动进度条来控制歌曲进度,如果没有打开歌曲,则会出现提示,当进度条的值改变时,音乐播放器从新的位置开始播放,拖动进度条时,音乐暂停播放,停止拖动进度条时,音乐开始播放

audioSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (currentposition == -1) {
                    Log.i("MusicPlayerService", "MusicActivity...showInfo(请选择要播放的音乐);.........");
                                      showInfo("请选择要播放的音乐");
                }
                   
if (fromUser) {
                     btn_play_pause.setBackgroundResource(R.drawable.pause);     MusicPlayerService.mediaPlayer.seekTo(progress);//                    }

                }
            }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                if (mediaPlayer != null) {
                    mediaPlayer.pause();
                }            }
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                if (mediaPlayer != null) {
                    mediaPlayer.start();
                }
        });

5. 点击查看歌曲详细信息

.   int id = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID));

String tilte = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
String album = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));
int albumId = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
String artist = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
String url = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
int duration = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));
Long size = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE));

三. 实现截图

 

视频演示:实现功能依次为打开音乐,暂停音乐,下一曲,暂停,上一曲。

视频链接:https://www.bilibili.com/video/av39893597?share_medium=android&share_source=qq&bbid=UWFYPg47AmBQMgdhHS8dLx0lHSUScUByQj4Ginfoc&ts=1546592457586

四.代码链接

  1. 项目链接:

https://git.coding.net/L_HEY/1600802085_music.git

  1. App链接:

https://coding.net/u/L_HEY/p/1600802085_music/git/blob/master/music-debug.apk

 

posted @ 2018-12-07 21:34  _长安思故里  阅读(265)  评论(0编辑  收藏  举报