Android Studio实现简单音乐播放功能

该博客介绍了如何使用BroadcastReceiver和音乐服务在Android Studio中实现简单的音乐播放功能,包括播放、暂停、切换歌曲、调整进度和音量。项目结构包含主页面和音乐服务端,通过广播进行通信。
摘要由CSDN通过智能技术生成

项目要求

基于Broadcast,BroadcastReceiver等与广播相关的知识实现简单的音乐播放功能,包括音乐的播放、暂停、切换、进度选择、音量调整。

设计效果

(进度条时间刷新功能还没有实现)
效果展示

实现思路

  1. 音乐服务端负责播放音乐和收发广播的功能。当音乐服务端作为接收器时,只能接收到主页面广播的控制消息;作为发送器时,向主页面发送歌曲信息更新的消息
  2. 主页面负责进度条以及音量按钮的监听,同时也有收发广播的功能。当主页面作为接收器时,只能接收到音乐服务端广播的歌曲信息更新的消息;作为发送器时,向音乐服务端发送对歌曲播放的控制消息。

项目结构图
项目结构

主要代码

1 主页面

package com.example.dell.musicbox3;

import  android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView;


public class MainActivity extends Activity implements View.OnClickListener
{
   
	// 获取界面中显示歌曲标题、作者文本框
	TextView title, author;
	// 播放/暂停、停止按钮
	ImageButton play, next, last;
	// 声明音量管理器
	public AudioManager mAudioManager = null;
	// 定义进度条
	public static SeekBar audioSeekBar = null;
	// 定义音量大小
	public SeekBar audioVolume = null;

	ActivityReceiver activityReceiver;

	public static final String CTL_ACTION = "org.crazyit.action.CTL_ACTION";

	public static final String UPDATE_ACTION = "org.crazyit.action.UPDATE_ACTION";
	// 定义音乐的播放状态,0x11代表没有播放;0x12代表正在播放;0x13代表暂停
	int status = 0x11;
	String[] titleStrs = new String[] {
    "龙卷风", "可爱女人", "星晴" };
	String[] authorStrs = new String[] {
    "JayChou", "JayChou", "JayChou" };

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

		// 获取程序界面界面中的两个按钮
		play = (ImageButton) this.findViewById(R.id.play);
		last = (ImageButton) this.findViewById(R.id.last);
		next = (ImageButton) this.findViewById(R.id.next);
		audioVolume = (SeekBar) findViewById(R.id.seekBar2);
		audioSeekBar = (SeekBar) findViewById(R.id.seekBar1);
		title = (TextView) findViewById(R.id.title);
		author = (TextView) findViewById(R.id.author);


		// 为两个按钮的单击事件添加监听器
		play.setOnClickListener(this);
		last.setOnClickListener
  • 14
    点赞
  • 96
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值