安卓实训项目v0.6:基于存储卡音乐播放器V0.6

一、功能要求

在播放器V0.5的基础上添加:
1.添加启动画面(SplashScreenActivity)
2.添加音乐播放模式:随机,顺序,单曲循环
3.将进度条改成拖拽条:用户随意拉动播放进度

二 、运行效果

在这里插入图片描述

在这里插入图片描述

三、实现步骤

1.创建安卓应用【SDCardMusicPlayerV0.6】

2.将图片复制到drawable目录与mipmap目录

在这里插入图片描述
在这里插入图片描述

3.按钮背景图片选择

(1)播放:play_button_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/play_button_pressed" android:state_pressed="true"/>
    <item android:drawable="@drawable/play_button" android:state_pressed="false"/>
</selector>

(2)暂停:pause_button_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/pause_button_pressed" android:state_pressed="true"/>
    <item android:drawable="@drawable/pause_button" android:state_pressed="false"/>
</selector>

(3)上一首:previous_button_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/previous_button_pressed" android:state_pressed="true"/>
    <item android:drawable="@drawable/previous_button" android:state_pressed="false"/>
</selector>

(4)下一首:next_button_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/next_button_pressed" android:state_pressed="true"/>
    <item android:drawable="@drawable/next_button" android:state_pressed="false"/>
</selector>

4.在res下创建anim文件夹,在里面创建动画资源文件

在这里插入图片描述

5.创建自定义边框配置文件custom_border.xml(res下的drawable文件中创建)

在这里插入图片描述

6.创建ui子包,将MainActivity拖进ui子包,并创建启动界面SplashScreenActivity

7.创建entity子包,在里面创建Music实体类

在这里插入图片描述

8.创建app子包,里面创建接口AppConstants和音乐播放器应用程序类MusicPlayerApplication

(1)接口AppConstants:

在这里插入图片描述

(2)音乐播放器应用程序类MusicPlayerApplication

package net.lbj.sdcard_musicplayer_v06.app;

import android.app.Application;
import android.os.Environment;

import net.lbj.sdcard_musicplayer_v06.entity.Music;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * 功能:音乐播放器应用程序类
 * 日期:2021年01月10日
 */
public class MusicPlayerApplication extends Application {

    private SimpleDateFormat sdf; //简单日期格式
    //private List<Music> musicList; //音乐列表(播放数据源)*
    private int currentMusicIndex; //当前音乐索引
    private int currentPosition; //当前播放位置
    private int playMode; //播放模式*
    private int progressChangeByUser; //用户修改的播放进度

    @Override
    public void onCreate() {
        super.onCreate();
        //实例化简单日期格式
        sdf = new SimpleDateFormat("mm:ss");

    }

    public int getCurrentMusicIndex() {
        return currentMusicIndex;
    }

    public void setCurrentMusicIndex(int currentMusicIndex) {
        this.currentMusicIndex = currentMusicIndex;
    }

    public int getCurrentPosition() {
        return currentPosition;
    }

    public void setCurrentPosition(int currentPosition) {
        this.currentPosition = currentPosition;
    }

    public int getPlayMode() {
        return playMode;
    }

    public void setPlayMode(int playMode) {
        this.playMode = playMode;
    }

    public int getProgressChangeByUser() {
        return progressChangeByUser;
    }

    public void setProgressChangeByUser(int progressChangeByUser) {
        this.progressChangeByUser = progressChangeByUser;
    }

    /**
     * 获取格式化时间
     *
     * @param time 单位是毫秒
     * @return mm:ss格式的时间
     */
    /**
     * 获取格式化时间
     *
     * @param time 单位是毫秒
     * @return mm:ss格式的时间
     */
    public String getFormatTime(int time) {
        return sdf.format(time);
    }

    /**
     * 生成指定目录下某种类型的文件列表
     *
     * @param dir
     * @param suffix
     * @param typeFileList
     */
    public void makeTypeFileList(File dir, String suffix, List<String> typeFileList) {
        //获取指定目录下的file数组(File既可以指向目录,也可以指向文件)
        File[] files = dir.listFiles();
        //遍历file数组
        for (File file : files) {
            //判断file是否是文件
            if (file.isFile()) {//file是文件
                //按照后缀来过滤文件
                if (file.getName().endsWith(suffix)) {
                    //将满足条件的文件添加到文件列表
                    typeFileList.add(file.getAbsolutePath());
                }
            } else {//file是目录
                //目录非空递归调用
                if (file.list() != null) {
                    makeTypeFileList(file, suffix, typeFileList);
                }
            }
        }
    }

    /**
     * 获取音乐列表
     *
     * @return 音乐列表
     */
    public List<Music> getMusicList() {
        //声明音乐列表
        List<Music> musicList = null;

        //获取外置存储卡根目录
        File sdRootDir = Environment.getExternalStorageDirectory();
        //创建后缀字符串
        String suffix = ".mp3";
        //创建音乐文件列表
        List<String> musicFileList = new ArrayList<>();
        //调用方法,生成指定目录下某种类型文件列表
        makeTypeFileList(sdRootDir, suffix, musicFileList);
        //判断音乐文件列表是否有元素
        if (musicFileList.size() > 0) {
            //实例化音乐列表
            musicList = new ArrayList<>();
            //遍历音乐文件列表
            for (String musicFile : musicFileList) {
                //创建音乐实体
                Music music = new Music();
                //设置实体属性
                music.setMusicName(musicFile);
                //将音乐实体添加到音乐列表
                musicList.add(music);
            }
        }

        //返回音乐列表
        return musicList;
    }
}

9.创建adapter子包,在里面创建音乐适配器 - MusicAdapter

package net.lbj.sdcard_musicplayer_v06.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import net.lbj.sdcard_musicplayer_v06.R;
import net.lbj.sdcard_musicplayer_v06.entity.Music;

import java.util.List;

/**
 * 功能:音乐适配器
 * 日期:2021年01月10日
 */
public class MusicAdapter extends BaseAdapter {

    private Context context; //上下文
    private List<Music> musicList; //音乐列表

    /**
     * 构造方法
     *
     * @param context
     * @param musicList
     */
    public MusicAdapter(Context context, List<Music> musicList) {
        this.context = context;
        this.musicList = musicList;
    }

    @Override
    public int getCount() {
        return musicList.size();
    }

    @Override
    public Object getItem(int position) {
        return musicList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //声明视图容器
        ViewHolder holder = null;

        //判断转换视图是否为空
        if (convertView == null) {
            //将列表项模板映射成转换视图
            convertView = LayoutInflater.from(context).inflate(R.layout.music_name_list_item, null);
            //创建视图容器对象
            holder = new ViewHolder();
            //实例化转换视图里的控件
            holder.tvMusicName = convertView.findViewById(R.id.tvMusicName);
            //将视图容器附加到转换视图
            convertView.setTag(holder);
        } else {
            //从转换视图里取出视图容器
            holder = (ViewHolder) convertView.getTag();
        }

        // 获取列表项要显示的数据
        Music music = musicList.get(position);
        // 设置列表项控件的属性(去掉路径和扩展名)
        holder.tvMusicName.setText(music.getMusicName().substring(
                music.getMusicName().lastIndexOf("/") + 1, music.getMusicName().lastIndexOf(".")));

        // 返回转换视图
        return convertView;
    }

    /**
     * 视图容器
     */
    private static class ViewHolder {
        TextView tvMusicName;
    }
}

10.创建音乐名列表项模板music_name_list_item.xml

在这里插入图片描述

11.启动界面类SplashScreenActivity

(1)启动界面布局资源文件activity_splash_screen.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/splash_background"
    android:gravity="center"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/iv_music_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:src="@drawable/music" />

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:text="@string/title"
        android:textColor="#0000ff"
        android:textSize="25sp" />

    <TextView
        android:id="@+id/tv_version"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="@string/version"
        android:textColor="#ff0000"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/tv_author"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="@string/author"
        android:textColor="#000000"
        android:textSize="20sp" />
</LinearLayout>

(2)启动界面类SplashScreenActvity

package net.lbj.sdcard_musicplayer_v06.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

import net.lbj.sdcard_musicplayer_v06.R;

/**
 * 功能:启动界面类
 * 日期:2021年01月10日
 */
public class SplashScreenActivity extends Activity {
    /**
     * 动画对象
     */
    private Animation animation;
    /**
     * 音乐图标图像控件
     */
    private ImageView ivMusicIcon;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 利用布局资源文件设置用户界面
        setContentView(R.layout.activity_splash_screen);

        // 通过资源标识获得控件实例
        ivMusicIcon = findViewById(R.id.iv_music_icon);

        // 加载动画资源文件,创建动画对象
        animation = AnimationUtils.loadAnimation(this, R.anim.animator);
        // 让音乐图标图像控件启动动画
        ivMusicIcon.startAnimation(animation);
        // 给动画对象设置监听器
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                // 启动主界面
                startActivity(new Intent(SplashScreenActivity.this, MainActivity.class));
                // 关闭启动界面
                finish();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
    }

}

12.创建service子包,创建音乐服务类 - MusicPlayService

package net.lbj.sdcard_musicplayer_v06.service;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.MediaPlayer;
import android.os.IBinder;

import androidx.annotation.Nullable;

import net.lbj.sdcard_musicplayer_v06.R;
import net.lbj.sdcard_musicplayer_v06.app.AppConstants;
import net.lbj.sdcard_musicplayer_v06.app.MusicPlayerApplication;
import net.lbj.sdcard_musicplayer_v06.entity.Music;

import java.io.IOException;
import java.util.List;
import java.util.Random;

/**
 * 功能:音乐播放服务类
 * 日期:2021年01月10日
 */

public class MusicPlayService extends Service implements AppConstants {

    private MediaPlayer mp; // 媒体播放器
    private String musicName; // 音乐文件名
    private Thread thread; //更新音乐播放器进度的线程
    private boolean isRunning; // 线程循环控制变量
    private List<Music> musicList; // 音乐列表(数据源)
    private MusicPlayerApplication app; //音乐播放器应用程序对象
    private MusicReceiver receiver; //音乐广播接收器

    @Override
    public void onCreate() {
        super.onCreate();
        //获取音乐播放器应用程序对象
        app = (MusicPlayerApplication) getApplication();
        //获取音乐列表
        musicList = app.getMusicList();
        //创建媒体播放器
        mp = new MediaPlayer();
        //给媒体播放器注册完成监听器
        mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                //切换到下一首音乐
                nextMusic();
            }
        });

        // 设置线程循环控制变量为真
        isRunning = true;
        // 创建更新播放进度的线程
        thread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (isRunning) {
                    //判断音乐是否在播放
                    if (mp.isPlaying()) {
                        //创建意图
                        Intent intent = new Intent();
                        //设置广播频道:更新播放进度
                        intent.setAction(INTENT_ACTION_UPDATE_PROGRESS);
                        //设置当前播放位置
                        app.setCurrentPosition(mp.getCurrentPosition());
                        //让意图携带播放时长数据
                        intent.putExtra(DURATION, mp.getDuration());
                        //让意图携带控制图标数据(暂停图标)
                        intent.putExtra(CONTROL_ICON, R.drawable.pause_button_selector);
                        //按意图发送广播
                        sendBroadcast(intent);
                        try {
                            //让线程睡眠500毫秒
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        });
        // 启动更新播放进度的线程
        thread.start();

        //创建音乐广播接收器
        receiver = new MusicReceiver();
        //创建意图过滤器
        IntentFilter filter = new IntentFilter();
        //通过意图过滤器添加广播频道
        filter.addAction(INTENT_ACTION_PLAY_OR_PAUSE);
        filter.addAction(INTENT_ACTION_PLAY);
        filter.addAction(INTENT_ACTION_PREVIOUS);
        filter.addAction(INTENT_ACTION_NEXT);
        filter.addAction(INTENT_ACTION_USER_CHANGE_PROGRESS);//不加这句拖拽无效
        //注册广播接收器
        registerReceiver(receiver, filter);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        //获取当前播放音乐名
        musicName = musicList.get(app.getCurrentMusicIndex()).getMusicName();
        try {
            //设置播放源
            mp.setDataSource(musicName);
            //缓冲播放源
            mp.prepare();
            //音乐当前播放位置归零
            app.setCurrentPosition(0);
            //创建意图
            Intent intent1 = new Intent();
            //设置广播频道:更新播放进度
            intent1.setAction(INTENT_ACTION_UPDATE_PROGRESS);
            //让意图携带播放时长
            intent1.putExtra(DURATION, mp.getDuration());
            //按意图发送广播
            sendBroadcast(intent1);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //返回非粘性服务
        return Service.START_NOT_STICKY;
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    /**
     * 销毁回调方法
     */
    @Override
    public void onDestroy() {
        super.onDestroy();
        //释放媒体播放器
        if (mp != null) {
            mp.release();
            mp = null;
        }
        //注销广播接收器
        unregisterReceiver(receiver);
        //设置线程循环控制变量
        isRunning = false;
        //销毁子线程
        thread = null;
    }

    /**
     * 播放方法
     */
    private void play() {
        try {
            // 重置媒体播放器
            mp.reset();
            // 获取当前播放的音乐名
            musicName = musicList.get(app.getCurrentMusicIndex()).getMusicName();
            // 设置播放源
            mp.setDataSource(musicName);
            // 缓冲播放源(从存储卡加载到内存)
            mp.prepare();
            // 定位到暂停时的播放位置
            mp.seekTo(app.getCurrentPosition());
            // 启动音乐的播放
            mp.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 上一首音乐
     */
    private void previousMusic() {
        // 更新音乐索引
        if (app.getCurrentMusicIndex() > 0) {
            app.setCurrentMusicIndex(app.getCurrentMusicIndex()- 1);
        } else {
            app.setCurrentMusicIndex(musicList.size() - 1);
        }
        // 当前播放位置归零
        app.setCurrentPosition(0);
        // 调用播放方法
        play();
    }

    /**
     * 下一首音乐
     */
    private void nextMusic() {
        // 根据播放模式来更新音乐索引
        switch (app.getPlayMode()) {
            // 顺序播放模式
            case PLAY_MODE_ORDER:
                if (app.getCurrentMusicIndex() < musicList.size() - 1) {
                    app.setCurrentMusicIndex(app.getCurrentMusicIndex() + 1);
                } else {
                    app.setCurrentMusicIndex(0);
                }
                break;
            // 随机播放模式
            case PLAY_MODE_RANDOM:
                // 随机设置索引
                app.setCurrentMusicIndex(new Random().nextInt(app.getMusicList().size()));
                break;
            // 单曲循环模式
            case PLAY_MODE_LOOP:
                // 音乐索引保持不变
                break;
        }
        // 当前播放位置归零
        app.setCurrentPosition(0);
        // 调用播放方法
        play();

    }

    /**
     * 暂停方法
     */
    private void pause() {
        // 暂停播放
        mp.pause();
        // 保存音乐播放的当前位置
        app.setCurrentPosition(mp.getCurrentPosition());
        //任务:发送广播给主界面,更改图标,更改播放进度
        //创建意图
        Intent intent = new Intent();
        //设置广播频道:更新播放进度
        intent.setAction(INTENT_ACTION_UPDATE_PROGRESS);
        //让意图携带播放时长数据
        intent.putExtra(DURATION, mp.getDuration());
        //让意图携带控制图标数据(播放图标)
        intent.putExtra(CONTROL_ICON, R.drawable.play_button_selector);
        //按意图发送广播
        sendBroadcast(intent );
    }


    /**
     * 音乐广播接收器
     */
    private class MusicReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            //获取意图动作(广播频道)
            String action = intent.getAction();
            //对广播频道非空判断
            if (action != null) {
                //根据不同频道进行不同操作
                switch (action) {
                    case INTENT_ACTION_PLAY: //主界面单击列表项
                        //播放进度归零
                        app.setCurrentPosition(0);
                        //调用播放方法
                        play();
                        break;
                    case INTENT_ACTION_PLAY_OR_PAUSE: //主界面打击了播放|暂停按钮
                        //判断音乐是否在播放
                        if (mp.isPlaying()) {
                            pause();
                        } else {
                            play();
                        }
                        break;
                    case INTENT_ACTION_PREVIOUS: //主界面单击了上一首按钮
                        //切换到上一首音乐
                        previousMusic();
                        break;
                    case INTENT_ACTION_NEXT: //主界面单击了下一首按钮
                        //切换到下一首音乐
                        nextMusic();
                        break;
                    case INTENT_ACTION_USER_CHANGE_PROGRESS:
                        // 根据拖拽条的进度值计算当前播放位置
                        app.setCurrentPosition(app.getProgressChangeByUser() * mp.getDuration() / 100);
                        // 根据音乐当前播放位置开始播放音乐
                        play();
                        break;
                    }
                }
            }
        }
    }

13.修改项目清单文件

在这里插入图片描述

14.字符串资源文件string.xml

在这里插入图片描述

15.布局资源文件activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    android:orientation="vertical"
    android:padding="20dp"
    tools:context=".ui.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@drawable/custom_border"
        android:gravity="center"
        android:orientation="horizontal"
        android:padding="5dp">

        <TextView
            android:id="@+id/tv_play_mode"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/play_mode"
            android:textSize="13sp" />

        <RadioGroup
            android:id="@+id/rg_play_mode"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <RadioButton
                android:id="@+id/rb_order"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:text="@string/order"
                android:textSize="13sp" />

            <RadioButton
                android:id="@+id/rb_random"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/random"
                android:textSize="13sp" />

            <RadioButton
                android:id="@+id/rb_loop"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/loop"
                android:textSize="13sp" />
        </RadioGroup>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ProgressBar
            android:id="@+id/pbScanMusic"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:visibility="gone"/>

        <TextView
            android:id="@+id/tvScanMusic"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/scan_music"
            android:textColor="#ff00ff"
            android:textSize="25sp"
            android:visibility="gone"/>
    </LinearLayout>

    <ListView
        android:id="@+id/lvMusicName"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginBottom="16dp"
        android:layout_weight="8" />

    <TextView
        android:id="@+id/tvMusicName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:textColor="#0000ff"
        android:textSize="20sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:layout_weight="0.5"
        android:gravity="center"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tvCurrentPosition"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textColor="#ff0000" />

        <SeekBar
            android:id="@+id/pbMusicProgress"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="6" />

        <TextView
            android:id="@+id/tvDuration"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textColor="#ff00ff" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:gravity="center"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btnPrevious"
            android:layout_width="60dp"
            android:layout_height="50dp"
            android:background="@drawable/previous_button_selector"
            android:onClick="doPrevious" />

        <Button
            android:id="@+id/btnPlayOrPause"
            android:layout_width="60dp"
            android:layout_height="50dp"
            android:background="@drawable/play_button_selector"
            android:onClick="doPlayOrPause" />

        <Button
            android:id="@+id/btnNext"
            android:layout_width="60dp"
            android:layout_height="50dp"
            android:background="@drawable/next_button_selector"
            android:onClick="doNext" />
    </LinearLayout>
</LinearLayout>

16.主界面类 - MainActivity

package net.lbj.sdcard_musicplayer_v06.ui;

import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import net.lbj.sdcard_musicplayer_v06.R;
import net.lbj.sdcard_musicplayer_v06.adapter.MusicAdapter;
import net.lbj.sdcard_musicplayer_v06.app.AppConstants;
import net.lbj.sdcard_musicplayer_v06.app.MusicPlayerApplication;
import net.lbj.sdcard_musicplayer_v06.entity.Music;
import net.lbj.sdcard_musicplayer_v06.service.MusicPlayService;

import java.util.List;

/**
 * 功能:基于存储卡音乐播放器v0.6
 * 日期:2021/01/10
 */
public class MainActivity extends AppCompatActivity implements AppConstants {

    private String musicName; // 音乐文件名
    private TextView tvMusicName; // 音乐名标签
    private Button btnPlayOrPause; // 播放|暂停按钮
    private MusicReceiver receiver; // 音乐广播接收器
    private TextView tvCurrentPosition; // 显示当前播放位置的标签
    private TextView tvDuration; // 显示音乐播放时长的标签
    //private ProgressBar pbMusicProgress; // 音乐播放进度条
    private SeekBar sbMusicProgress; //音乐播放拖拽条
    private ListView lvMusicName; // 音乐名列表控件
    private List<Music> musicList; // 音乐列表(数据源)
    private MusicAdapter adapter; // 音乐适配器
    private MusicPlayerApplication app; // 音乐播放器应用程序对象
    private ProgressBar pbScanMusic; // 扫描存储卡音乐进度条
    private TextView tvScanMusic; // 扫描存储卡音乐标签(提示用户耐心等待)
    private RadioGroup rgPlayMode; //播放模式单选按钮组*

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //利用布局资源文件设置用户界面
        setContentView(R.layout.activity_main);

        // 通过资源标识符获取控件实例
        lvMusicName = findViewById(R.id.lvMusicName);
        tvMusicName = findViewById(R.id.tvMusicName);
        btnPlayOrPause = findViewById(R.id.btnPlayOrPause);
        tvCurrentPosition = findViewById(R.id.tvCurrentPosition);
        tvDuration = findViewById(R.id.tvDuration);
        sbMusicProgress = findViewById(R.id.pbMusicProgress);
        pbScanMusic = findViewById(R.id.pbScanMusic);
        tvScanMusic = findViewById(R.id.tvScanMusic);
        rgPlayMode = findViewById(R.id.rg_play_mode);//

        //获取音乐播放器应用程序对象
        app = (MusicPlayerApplication) getApplication();

        // 定义存储读写权限数组
        String[] PERMISSIONS_STORAGE = {
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE
        };
        // 检查是否有读权限
        final int permission = ActivityCompat.checkSelfPermission(this, PERMISSIONS_STORAGE[0]);
        // 如果没有授权,那么就请求读权限
        if (permission != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, PERMISSIONS_STORAGE, 0);
            return;
        }

        //执行填充音乐列表的异步任务
        new FillMusicListTask().execute();

        // 给音乐列表控件注册监听器
        lvMusicName.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                // 获取音乐索引
                app.setCurrentMusicIndex(position);
                // 当前播放位置归零
                app.setCurrentPosition(0);
                //获取音乐名
                musicName = app.getMusicList().get(position).getMusicName();
                // 设置音乐名标签内容,去掉路径和扩展名
                tvMusicName.setText("No." + (app.getCurrentMusicIndex() + 1) + " " + musicName.substring(
                        musicName.lastIndexOf("/") + 1, musicName.lastIndexOf(".")));
                //创建意图
                Intent intent = new Intent();
                //设置广播频道
                intent.setAction(INTENT_ACTION_PLAY);
                //按意图发送广播
                sendBroadcast(intent);
            }
        });

        // 给播放模式单选按钮组注册监听器
        rgPlayMode.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
                // 判断用户选择何种播放模式
                switch (checkedId) {
                    // 顺序播放模式
                    case R.id.rb_order:
                        app.setPlayMode(PLAY_MODE_ORDER);
                        break;
                    // 随机播放模式
                    case R.id.rb_random:
                        app.setPlayMode(PLAY_MODE_RANDOM);
                        break;
                    // 单曲循环模式
                    case R.id.rb_loop:
                        app.setPlayMode(PLAY_MODE_LOOP);
                        break;
                }
            }
        });

        //给音乐播放拖拽条注册监听器
        sbMusicProgress.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                // 判断进度是否为用户修改
                if (fromUser) {
                    // 设置用户修改的播放进度
                    app.setProgressChangeByUser(progress);
                    // 创建意图
                    Intent intent = new Intent();
                    // 设置广播频道:用户修改播放进度
                    intent.setAction(INTENT_ACTION_USER_CHANGE_PROGRESS);
                    // 按意图发送广播
                    sendBroadcast(intent);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        //创建音乐广播接收器
        receiver = new MusicReceiver();
        //创建意图过滤器
        IntentFilter filter = new IntentFilter();
        //通过意图过滤器添加广播频道
        filter.addAction(INTENT_ACTION_UPDATE_PROGRESS);
        //注册音乐广播接收器
        registerReceiver(receiver, filter);
    }

    /**
     * 填充音乐列表异步任务类
     */
    private class FillMusicListTask extends AsyncTask<Void, Integer, Void> {
        /**
         * 耗时工作执行前
         */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // 显示扫描音乐进度条
            pbScanMusic.setVisibility(View.VISIBLE);
            // 显示扫描音乐标签
            tvScanMusic.setVisibility(View.VISIBLE);
        }

        @Override
        protected Void doInBackground(Void... voids) {
            // 获取音乐列表
            musicList = app.getMusicList();
            // 故意耗时,要不然扫描太快结束
            for (long i = 0; i < 2000000000; i++) {
            }
            return null;
        }

        /**
         * 耗时工作执行后
         *
         * @param aVoid
         */
        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            // 隐藏扫描音乐进度条
            pbScanMusic.setVisibility(View.GONE);
            // 隐藏扫描音乐标签
            tvScanMusic.setVisibility(View.GONE);

            // 判断音乐列表是否有元素
            if (musicList.size() > 0) {
                // 创建音乐适配器
                adapter = new MusicAdapter(MainActivity.this, musicList);
                // 给音乐列表控件设置适配器
                lvMusicName.setAdapter(adapter);
                // 获取当前要播放的音乐名(默认是音乐播放列表的第一首)
                musicName = musicList.get(0).getMusicName();
                // 设置音乐名标签内容,去掉路径和扩展名,添加序号
                tvMusicName.setText("No." + (app.getCurrentMusicIndex() + 1) + " " + musicName.substring(
                        musicName.lastIndexOf("/") + 1, musicName.lastIndexOf(".")));
                //创建意图,用于启动音乐播放服务
                Intent intent = new Intent(MainActivity.this, MusicPlayService.class);
                //按意图启动服务
                startService(intent);
            } else {
                // 提示用户没有音乐文件
                Toast.makeText(MainActivity.this, "外置存储卡上没有音乐文件!", Toast.LENGTH_SHORT);
            }
        }
    }

    /**
     * 音乐广播接收器
     */
    private class MusicReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            //获取广播频道
            String action = intent.getAction();
            //判断广播频道是否为空
            if (action != null) {
                //根据不同广播频道执行不同操作
                if (INTENT_ACTION_UPDATE_PROGRESS.equals(action)) {
                    //获取播放时长
                    int duration = intent.getIntExtra(DURATION, 0);
                    //获取播放控制图标
                    int controlIcon = intent.getIntExtra(CONTROL_ICON,
                            R.drawable.play_button_selector);
                    //计算进度值
                    int progress = app.getCurrentPosition() * 100 / duration;
                    //获取音乐名
                    musicName = app.getMusicList().get(app.getCurrentMusicIndex()).getMusicName();
                    //设置正在播放的文建明(去掉扩展名)
                    tvMusicName.setText("No." + (app.getCurrentMusicIndex() + 1) + " "
                            + musicName.substring(musicName.lastIndexOf("/") + 1, musicName.lastIndexOf(".")));
                    //设置播放进度值标签
                    tvCurrentPosition.setText(app.getFormatTime(app.getCurrentPosition()));
                    //设置播放时长标签
                    tvDuration.setText(app.getFormatTime(duration));
                    //设置播放拖拽条的进度值
                    sbMusicProgress.setProgress(progress);
                    //设置【播放|暂停】按钮显示的图标
                    btnPlayOrPause.setBackgroundResource(controlIcon);
                }
            }
        }
    }

    /**
     * 上一首按钮单击事件处理方法
     *
     * @param view
     */
    public void doPrevious(View view){
        //创建意图
        Intent intent = new Intent();
        //设置广播频道
        intent.setAction(INTENT_ACTION_PREVIOUS);
        //按意图发送广播
        sendBroadcast(intent);
    }

    /**
     * 下一首按钮单击事件处理方法
     *
     * @param view
     */
    public void doNext(View view){
        //创建意图
        Intent intent = new Intent();
        //设置广播频道
        intent.setAction(INTENT_ACTION_NEXT);
        //按意图发送广播
        sendBroadcast(intent);
    }

    /**
     * 播放|暂停按钮单击事件处理方法
     *
     * @param view
     */
    public void doPlayOrPause(View view) {
        //创建意图
        Intent intent = new Intent();
        //设置广播频道
        intent.setAction(INTENT_ACTION_PLAY_OR_PAUSE);
        //按意图发送广播
        sendBroadcast(intent);
    }

    /**
     * 销毁回调方法
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        //停止音乐播放器
        stopService(new Intent(MainActivity.this, MusicPlayService.class));
        //注销广播接收器
        if (receiver != null) {
            unregisterReceiver(receiver);
        }
    }
}

四、 遇到的问题

1.因为v0.6是自己添加新功能,刚开始写的时候除了把新的界面布局出来,就不知道怎么写了,然后就先写了第二阶段的项目(显示不出来音乐)。最后跟着第二阶段的添加新内容
2.写完后页面显示不出来
原因:没有改项目清单文件
解决办法:在这里插入图片描述
3.进度条虽然设成了拖拽,但是无效
解决方法:
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zip安卓app开发项目-音乐播放器(源码).zi

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值