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. 设置随机播放

没有设置模式时则默认为随机播放

playMode = (ImageView)findViewById(R.id.play_mode);
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
editor = sharedPreferences.edit();
int playmode = sharedPreferences.getInt("play_mode", -1);
if(playmode == -1){
   
editor.putInt("play_mode",0).commit();
}else{
    changeMode(playmode);
}

5. 设置摇一摇自动切歌

默认为摇一摇功能是开启的

playaccelerometer = (ImageView)findViewById(R.id.paly_accelerometer);
if(sharedPreferences.getInt("play_accelerometer",0) == 0){
   
clicktime = 0;
 playaccelerometer.setBackgroundResource(R.drawable.ic_alarm_on_black_24dp);
}else{
    clicktime = 1;
    playaccelerometer.setBackgroundResource(R.drawable.ic_alarm_off_black_24dp);
}

6. 设置分享功能

            设置分享出列表

       handler = new Handler();
imageView = (ImageView)findViewById(R.id.click_share);
imageView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.putExtra(Intent.EXTRA_TEXT,"我的博客地址:https://www.cnblogs.com/L0HEYNI/");
        shareIntent.setType("text/plain");
      
        startActivity(Intent.createChooser(shareIntent,"分享到"));
    }
});

7. 设置进度条

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

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();
                }
        });

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

9.   int id = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID));
获取歌曲标题
String tilte = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
获取歌曲的专辑名:MediaStore.Audio.Media.ALBUM
String album = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));
int albumId = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
获取歌曲的歌手名: MediaStore.Audio.Media.ARTIST
String artist = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
获取歌曲文件的路径 :MediaStore.Audio.Media.DATA
String url = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
获取歌曲的总播放时长 :MediaStore.Audio.Media.DURATION
int duration = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));
获取歌曲文件的大小 :MediaStore.Audio.Media.SIZE
Long size = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE));

三. 实现截图

 Android第三次作业

四.代码链接

  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

 

Android第三次作业

上一篇:antd移动端onClick事件点击无效


下一篇:[Python]ConfigParser解析配置文件