Android Studio之BroadcastReceiver组件实现音乐播放器

功能需求

 利用BroadcastReceiver组件实现基本音乐播放器功能:上一曲下一曲切换、
 播放、暂停,以及当前播放歌曲信息显示

实现

1.静态注册

(1)广播不会跟随Activity的生命周期的结束而结束,一直存在,即使应用程序关闭,也会被唤醒接受广播
(2)全局的广播

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.musicplay">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name=".MusicService">
        </service>
    </application>

</manifest>

2.发送和接收(主要代码)

  1. 创建一个Intent
  2. 调用sendBroadcast()函数,把Intent携带的消息广播出去

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageButton
        android:id="@+id/previous"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@android:drawable/ic_media_previous" />

    <ImageButton
        android:id="@+id/play"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@android:drawable/ic_media_play" />

    <ImageButton
        android:id="@+id/stop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@android:drawable/ic_media_pause" />

    <ImageButton
        android:id="@+id/next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@android:drawable/ic_media_next" />

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20dp"
            android:textColor="#000000"
            android:ellipsize="marquee"
            android:layout_weight="1"
            android:marqueeRepeatLimit="marquee_forever"/>
        <TextView
            android:id="@+id/author"
            android:textSize="12dp"
            android:gravity="center_vertical"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </LinearLayout>

MainActivity.java

    public void onClick(View source) {
        //创建Intent
        Intent intent=new Intent("org.crazyit.action.CTL_ACTION");
        switch (source.getId())
        {
            //按下播放/暂停按钮
            case R.id.play:
                intent.putExtra("control",1);
                break;
            //按下停止按钮
            case R.id.stop:
                intent.putExtra("control",2);
                break;
            //按下上一首按钮
            case R.id.previous:
                intent.putExtra("control",3);
                break;
            //按下下一首按钮
            case R.id.next:
                intent.putExtra("control",4);
                break;
        }
        //发送广播,将被Service组件中的BroadcastReceiver接收到
        sendBroadcast(intent);
    }

    //自定义的BroadcastReceiver,负责监听从Service传回的广播
    private class ActivityReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            //获取Intent中的update消息,update代表播放状态
            int update=intent.getIntExtra("update",-1);
            //获取Intent中的current消息,current代表当前正在播放的歌曲
            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.mic);
                    //设置当前状态
                    status=0x12;
                    break;
                //控制系统进入暂停状态
                case 0x13:
                    //暂停状态下设置使用播放图标
                    play.setImageResource(R.drawable.play);
                    //设置当前状态
                    status=0x13;
                    break;
            }
        }
    }

MusicService.java

    public class MyReceiver extends BroadcastReceiver
    {
        @Override
        public void onReceive(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;
                    }
                    break;
                case 3:
                    //原来处于没有播放或暂停状态
                    if (status==0x11||status==0x13)
                    {
                        if(current==0) {
                            current=2;
                            prepareAndPlay(musics[current]);
                        }
                        //准备并播放音乐
                        else {
                            current=current-1;
                            prepareAndPlay(musics[current]);
                        }
                        status=0x12;
                    }
                    //原来处于播放状态
                    else if (status==0x12)
                    {
                        //上一首//准备并播放音乐
                        if(current==0) {
                            current=2;
                            prepareAndPlay(musics[current]);
                        }
                        else {
                            current=current-1;
                            prepareAndPlay(musics[current]);
                        }
                    }
                    break;
                case 4:
                    //原来处于没有播放或暂停状态
                    if (status==0x11||status==0x13)
                    {
                        if(current==2) {
                            current=0;
                            prepareAndPlay(musics[current]);
                        }   //准备并播放音乐
                        else {
                            current=current+1;
                            prepareAndPlay(musics[current]);
                        }
                        status=0x12;
                    }
                    //原来处于播放状态
                    else if (status==0x12)
                    {
                        //下一首
                        if(current==2) {
                            current=0;
                            prepareAndPlay(musics[current]);
                        }
                        else {
                            current=current+1;
                            prepareAndPlay(musics[current]);
                        }
                    }
                    break;
            }
            //广播通知Activity更改图标、文本框
            Intent sendIntent=new Intent(MainActivity.UPDATE_ACTION);
            sendIntent.putExtra("update",status);
            sendIntent.putExtra("current",current);
            //发送广播,将被Activity组件中的BroadcastReceiver接收
            sendBroadcast(sendIntent);
        }
    }

实现效果

在这里插入图片描述

源码下载

https://gitee.com/moshangxveran/MusicPlay

发布了9 篇原创文章 · 获赞 0 · 访问量 253

猜你喜欢

转载自blog.csdn.net/bdwdwks/article/details/105589625