文章目录

  • 一丶前端布局
  • 二丶Activity
  • 三丶Service
  • 实验演示



一丶前端布局

Layout的框架

musicplayerdocker音源 music player apps_播放音乐


一个自定义的音乐播放器,页面布局大致如下

musicplayerdocker音源 music player apps_播放音乐_02


二丶Activity

首先定义控件

// 获取界面中显示歌曲标题、作者文本框
    TextView title, author;
    // 喜欢,播放,暂停,停止按钮
    ImageButton like, play, stop, previous, next;
    //进度条
    TextView currentTv;  //当前时间
    TextView totalTv;    //歌曲总时间
    int totalTime;       //歌曲总时长,用于获取歌曲时长
    SeekBar jindutiaoSb; //进度条
    int position;        //位置

定义音乐的状态,歌曲的名字和详情

public static final String CTL_ACTION =
            "org.pyp.action.CTL_ACTION";
    public static final String UPDATE_ACTION =
            "org.pyp.action.UPDATE_ACTION";
    // 定义音乐的播放状态,0x11代表没有播放;0x12代表正在播放;0x13代表暂停
    int status = 0x11;
    String[] titleStrs = new String[]{"晴天—周杰伦", "奇妙能力歌", "我很好"};
    String[] authorStrs = new String[]{
            "从前有个人爱你很久很久",
            "我拒绝更好更圆的月亮",
            "只是偶尔遗憾会从眼泪掉"};

绑定按钮

play = (ImageButton) this.findViewById(R.id.play);
        stop = (ImageButton) this.findViewById(R.id.stop);
        like = findViewById(R.id.like);
        previous = findViewById(R.id.previous);
        next = findViewById(R.id.next);

        //文本显示
        title = (TextView) findViewById(R.id.title);
        author = (TextView) findViewById(R.id.author);

添加监听

// 为按钮的单击事件添加监听器
        like.setOnClickListener(this);
        play.setOnClickListener(this);
        stop.setOnClickListener(this);
        previous.setOnClickListener(this);
        next.setOnClickListener(this);

自定义的BroadcastReceiver,负责监听从Service传回来的广播

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.pause);
                    // 设置当前状态
                    status = 0x12;
                    break;
                // 控制系统进入暂停状态
                case 0x13:
                    // 暂停状态下设置使用播放图标
                    play.setImageResource(R.drawable.play);
                    // 设置当前状态
                    status = 0x13;
                    break;
            }
        }

定义点击事件,点击时创建Intent,发送广播,将被Service组件中的BroadcastReceiver接收到

public void onClick(View source) {
        // 创建Intent
        Intent intent = new Intent("org.pyp.action.CTL_ACTION");
        switch (source.getId()) {
            // 按下喜欢
            case R.id.like:
                if (flag) {
                    like.setImageResource(R.drawable.unlike);
                    flag = false;
                } else {
                    like.setImageResource(R.drawable.like);
                    flag = true;
                }
                break;
            // 按下播放/暂停按钮
            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);
    }

三丶Service

创建MediaPlayer,绑定监听器

mPlayer = new MediaPlayer();
        mPlayer.setOnCompletionListener(new OnCompletionListener() // ①
        {
            @Override
            public void onCompletion(MediaPlayer mp)
            {
                current++;
                if (current >= 3)
                {
                    current = 0;
                }
                //发送广播通知Activity更改文本框
                Intent sendIntent = new Intent(MainActivity.UPDATE_ACTION);
                sendIntent.putExtra("current", current);
                // 发送广播,将被Activity组件中的BroadcastReceiver接收到
                sendBroadcast(sendIntent);
                // 准备并播放音乐
                prepareAndPlay(musics[current]);
            }
        });

定义MyReceiver,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;
                    }
                    //上一首
                case 3:
                    // 如果原来正在播放或暂停
                    if (status == 0x12 || status == 0x13) {
                        mPlayer.stop();
                        if (current - 1 < 0) {
                            current = musics.length - 1;
                        } else {
                            current--;
                        }
                        prepareAndPlay(musics[current]);
                        status = 0x12;
                    }
                    break;
                //下一首
                case 4:
                    if (status == 0x12 || status == 0x13) {
                        mPlayer.stop();
                        if (current + 1 >= musics.length) {
                            current = 0;
                        } else {
                            current++;
                        }
                        prepareAndPlay(musics[current]);
                        status = 0x12;
                    }
                    break;
            }
            // 广播通知Activity更改图标、文本框
            Intent sendIntent = new Intent(MainActivity.UPDATE_ACTION);
            sendIntent.putExtra("update", status);
            sendIntent.putExtra("current", current);
            // 发送广播,将被Activity组件中的BroadcastReceiver接收到
            sendBroadcast(sendIntent);
        }

播放音乐的方法

private void prepareAndPlay(String music)
    {
        try
        {
            // 打开指定音乐文件
            AssetFileDescriptor afd = am.openFd(music);
            mPlayer.reset();
            // 使用MediaPlayer加载指定的声音文件。
            mPlayer.setDataSource(afd.getFileDescriptor(),
                    afd.getStartOffset(), afd.getLength());
            // 准备声音
            mPlayer.prepare();
            // 播放
            mPlayer.start();

        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

实验演示

musicplayerdocker音源 music player apps_android_03


git仓库地址.