Android小项目--音乐播放器的实现

完成了音乐播放器的实现,效果如下:
在这里插入图片描述
在这里插入图片描述
点击播放暂停按钮可以实现音乐的播放和暂停,播放完毕自动切换到下一首。
主要是用广播和服务完成。

完整代码见:
https://download.csdn.net/download/ayangann915/10903942
有需要的大佬可以下载哦(小声说)
主要代码如下:
MainActivity:

public class MainActivity extends Activity implements OnClickListener {
    //获取界面中显示歌曲标题、作者文本框
    TextView title, author;
    // 播放/暂停、停止按钮
    ImageButton play, stop;
    ActivityReceiver activityReceiver;
    //广播接收器的过滤条件
    public static final String CONTROL = "iet.jxufe.cn.android.control";//控制播放、暂停
    public static final String UPDATE = "iet.jxufe.cn.android.update";//更新界面显示
    // 定义音乐的播放状态,0x11代表没有播放;0x12代表正在播放;0x13代表暂停
    int status = 0x11;
    String[] titleStrs = new String[] { "老男孩", "春天里", "在路上" };//歌曲名
    String[] authorStrs = new String[] { "筷子兄弟", "汪峰", "刘欢" };//演唱者

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 获取程序界面中的两个按钮以及两个文本显示框
        play = (ImageButton) this.findViewById(R.id.play);
        stop = (ImageButton) this.findViewById(R.id.stop);
        title = (TextView) findViewById(R.id.title);
        author = (TextView) findViewById(R.id.author);
        // 为两个按钮的单击事件添加监听器
        play.setOnClickListener(this);
        stop.setOnClickListener(this);
        activityReceiver = new ActivityReceiver();
        // 创建IntentFilter
        IntentFilter filter = new IntentFilter(UPDATE);
        // 指定BroadcastReceiver监听的Action
        // filter.addAction(UPDATE_ACTION);
        // 注册BroadcastReceiver
        registerReceiver(activityReceiver, filter);
        Intent intent = new Intent(this, MusicService.class);
        startService(intent);// 启动后台Service
    }
    // 自定义的BroadcastReceiver,负责监听从Service传回来的广播
    public class ActivityReceiver extends BroadcastReceiver {
        public void onReceive(Context context, Intent intent) {
            // 获取Intent中的update消息,update代表播放状态,默认为-1
            int update = intent.getIntExtra("update", -1);
            // 获取Intent中的current消息,current代表当前正在播放的歌曲,默认为-1
            int current = intent.getIntExtra("current", -1);
            if (current >= 0) {
                title.setText(titleStrs[current]);
                author.setText(authorStrs[current]);
            }

            switch (update) {
                //当前是未播放状态设置按钮未播放按钮
                case 0x11:
                    play.setImageResource(R.drawable.play);
                    status = 0x11;
                    break;
                // 控制系统进入播放状态
                case 0x12:
                    // 播放状态下设置使用暂停图标
                    play.setImageResource(R.drawable.pause);
                    // 设置当前状态
                    status = 0x12;
                    break;
                // 控制系统进入暂停状态
                case 0x13:
                    // 暂停状态下设置使用播放图标
                    play.setImageResource(R.drawable.play);
                    // 设置当前状态
                    status = 0x13;
                    break;
            }
        }
    }

    public void onClick(View source) {
        // 创建Intent
        Intent intent = new Intent(CONTROL);
        switch (source.getId()) {
            // 按下播放/暂停按钮
            case R.id.play:
                //播放传递值为1
                intent.putExtra("control", 1);
                break;
            // 按下停止按钮
            case R.id.stop:
                //暂停传递值为2
                intent.putExtra("control", 2);
                break;
        }
        // 发送广播 ,将被Service组件中的BroadcastReceiver接收到
        sendBroadcast(intent);
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(activityReceiver);
    }
}

Service

public class MusicService extends Service{
    ServiceReceiver serviceReceiver;
    AssetManager am;//资源管理器
    String[] musics = new String[]{
            "oldboy.mp3",
            "spring.mp3",
            "way.mp3"
    };//定义几首歌曲
    MediaPlayer mPlayer;
    //当前的状态,0x11 代表没有播放 ;0x12代表 正在播放;0x13代表暂停
    int status = 0x11;
    // 记录当前正在播放的音乐
    int current = 0;

    //音乐播放服务类必须实现的方法,以及初始化。
    //继承服务基类的抽象方法
    public IBinder onBind(Intent intent){
        return null;
    }
    public void onCreate(){
        am = getAssets();//调用Context里的方法
        // 创建BroadcastReceiver
        serviceReceiver = new ServiceReceiver();
        // 创建IntentFilter
        IntentFilter filter = new IntentFilter(MainActivity.CONTROL);
        registerReceiver(serviceReceiver, filter);
        // 创建MediaPlayer
        mPlayer = new MediaPlayer();
        // 为MediaPlayer播放完成事件绑定监听器
        mPlayer.setOnCompletionListener(new OnCompletionListener(){
            //当前音乐播放结束就切换至下一首
            public void onCompletion(MediaPlayer mp){
                current++;
                //一共三首音乐,播放完从第一首开始
                if (current >= 3){
                    current = 0;
                }
                /* 发送广播通知Activity更改文本框 */
                Intent sendIntent = new Intent(MainActivity.UPDATE);
                sendIntent.putExtra("current", current);
                // 发送广播 ,将被Activity组件中的BroadcastReceiver接收到
                sendBroadcast(sendIntent);
                // 准备、并播放音乐
                prepareAndPlay(musics[current]);
            }
        });
        super.onCreate();
    }
    public class ServiceReceiver extends BroadcastReceiver{
        public void onReceive(final Context context, Intent intent){
            int control = intent.getIntExtra("control", -1);
            switch (control){
                // 播放或暂停
                case 1:
                    // 原来处于没有播放状态
                    if (status == 0x11){
                        // 准备、并播放音乐
                        prepareAndPlay(musics[current]);
                        status = 0x12;
                    }
                    // 原来处于播放状态
                    else if (status == 0x12){
                        mPlayer.pause();// 暂停
                        status = 0x13;// 改变为暂停状态
                    }
                    // 原来处于暂停状态
                    else if (status == 0x13){
                        mPlayer.start();// 播放
                        status = 0x12;// 改变状态
                    }
                    break;
                // 停止声音
                case 2:
                    // 如果原来正在播放或暂停
                    if (status == 0x12 || status == 0x13){
                        mPlayer.stop();// 停止播放
                        status = 0x11;
                    }
            }
            /* 发送广播通知Activity更改图标、文本框 */
            Intent sendIntent = new Intent(MainActivity.UPDATE);
            sendIntent.putExtra("update", status);
            sendIntent.putExtra("current", current);
            // 发送广播 ,将被Activity组件中的BroadcastReceiver接收到
            sendBroadcast(sendIntent);
        }
    }
    private void prepareAndPlay(String music){
        try{
            //打开指定音乐文件
            AssetFileDescriptor afd = getAssets().openFd(music);
            mPlayer.reset();
            //使用MediaPlayer加载指定的声音文件。
            mPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
            mPlayer.prepare();// 准备声音
            mPlayer.start();// 播放
        }
        catch (IOException e){
            e.printStackTrace();
        }
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        unregisterReceiver(serviceReceiver);
    }
}

yeah!

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值