音乐播放器


前言

在原音乐盒代码的基础上,加上上一曲和下一曲的功能,实现具有可以暂停和播放、切歌、停止播放功能的播放器


一、实现效果

在这里插入图片描述

二、实现代码

1.XML文件

界面分为两个部分,上部分两行使用 TextView 显示歌曲的名字和歌手名,下部分用 ImageButton 制作四个按钮,分别实现上一曲、暂停和播放、停止和下一曲功能,代码如下:
main.xml

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

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:ellipsize="marquee"
        android:gravity="center_horizontal"
        android:marqueeRepeatLimit="marquee_forever"
        android:textColor="#9C27B0"
        android:textSize="30sp" />

    <TextView
        android:id="@+id/author"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:gravity="center_horizontal"
        android:textSize="20sp" />

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

        <ImageButton
            android:id="@+id/before"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            app:srcCompat="@drawable/before" />

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

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

        <ImageButton
            android:id="@+id/next"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            app:srcCompat="@drawable/next" />
    </LinearLayout>

</LinearLayout>

2.class文件

创建 MusicService.class 文件,控制播放器在点击不同的按钮后的不同播放状态,代码如下:
MusicService.class

package com.example.xr.musicbox;

import java.io.IOException;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.IBinder;


public class MusicService extends Service
{
	MyReceiver serviceReceiver;
	AssetManager am;
	String[] musics = new String[] { "All the way north.mp3", "Because of love.mp3",
			"Light years away.mp3", "Practice love.mp3", "the longest film.mp3" };
	MediaPlayer mPlayer;
	// 当前的状态,0x11代表没有播放;0x12代表正在播放;0x13代表暂停
	int status = 0x11;
	// 记录当前正在播放的音乐
	int current = 0;
	@Override
	public IBinder onBind(Intent intent)
	{
		return null;
	}

	@Override
	public void onCreate()
	{
		super.onCreate();
		am = getAssets();

		// 创建BroadcastReceiver
		serviceReceiver = new MyReceiver();
		// 创建IntentFilter
		IntentFilter filter = new IntentFilter();
		filter.addAction(MainActivity.CTL_ACTION);
		registerReceiver(serviceReceiver, filter);


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

	public class MyReceiver extends BroadcastReceiver
{
	@Override
	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;
				}
				break;
			case 3:
				// 如果原来未播放、正在播放或暂停
				if (status == 0x11 || status == 0x12 || status == 0x13)
				{
					current--;
					if (current <0)
					{
						current = 4;
					}
					prepareAndPlay(musics[current]);
					status = 0x12;
				}
				break;
			case 4:
				// 如果原来未播放、正在播放或暂停
				if (status == 0x11 || status == 0x12 || status == 0x13)
				{
					current++;
					if (current >=5)
					{
						current = 0;
					}
					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();
		}
	}
}


主类 MainActivity 中,用status取不同的值,来代表不同的播放状态(0x11代表没有播放、0x12代表正在播放、0x13代表暂停), setImageResource 设置按钮的图标, setOnClickListener 监听屏幕的点击,通过 update 的值调整不同的图标和播放状态, source.getId() 通过屏幕点击不同的按钮给 control 传递不同的值,代码如下:
MainActivity.java

package com.example.xr.musicbox;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener
{
	// 获取界面中显示歌曲标题、作者文本框
	TextView title, author;
	// 播放/暂停、停止按钮
	ImageButton play, stop;
	ImageButton before,next;


	ActivityReceiver activityReceiver;

	public static final String CTL_ACTION =
			"org.wyy.action.CTL_ACTION";
	public static final String UPDATE_ACTION =
			"org.wyy.action.UPDATE_ACTION";
	// 定义音乐的播放状态,0x11代表没有播放;0x12代表正在播放;0x13代表暂停
	int status = 0x11;
	String[] titleStrs = new String[] { "一路向北", "因为爱情", "光年之外", "修炼爱情", "最长的电影"};
	String[] authorStrs = new String[] { "周杰伦", "陈奕迅", "邓紫棋", "林俊杰", "周杰伦" };

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		// 获取程序界面界面中的两个按钮
		play = (ImageButton) this.findViewById(R.id.play);
		stop = (ImageButton) this.findViewById(R.id.stop);
		before = (ImageButton) this.findViewById(R.id.before);
		next = (ImageButton) this.findViewById(R.id.next);

		title = (TextView) findViewById(R.id.title);
		author = (TextView) findViewById(R.id.author);

		before.setImageResource(R.drawable.before);
		next.setImageResource(R.drawable.next);

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

		activityReceiver = new ActivityReceiver();
		// 创建IntentFilter
		IntentFilter filter = new IntentFilter();
		// 指定BroadcastReceiver监听的Action
		filter.addAction(UPDATE_ACTION);
		// 注册BroadcastReceiver
		registerReceiver(activityReceiver, filter);

		Intent intent = new Intent(this, MusicService.class);
		// 启动后台Service
		startService(intent);
	}

	// 自定义的BroadcastReceiver,负责监听从Service传回来的广播
	public 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.pause);
				// 设置当前状态
				status = 0x12;
				break;
			// 控制系统进入暂停状态
			case 0x13:
				// 暂停状态下设置使用播放图标
				play.setImageResource(R.drawable.play);
				// 设置当前状态
				status = 0x13;
				break;
		}
	}
}

	@Override
	public void onClick(View source)
	{
		// 创建Intent
		Intent intent = new Intent("org.wyy.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.before:
				intent.putExtra("control", 3);
				break;
			case R.id.next:
				intent.putExtra("control", 4);
				break;
		}
		// 发送广播,将被Service组件中的BroadcastReceiver接收到
		sendBroadcast(intent);
	}
}





总结

通过这次设计让我了解了Service和广播的原理,以及如何控制音乐播放器的按钮、切歌和相关的操作

完整代码已上传代码仓库:
https://gitee.com/wyy-52/music-box/tree/master

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值