Android单机版音乐盒

本文介绍了一个简单的安卓音乐播放器的实现过程,包括界面布局、后台音乐播放的Service组件使用及Activity与Service间的通信机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这个小软件主要用到了可以后台运行的Service组件、BroadcastReceiver组件、Intent、菜单对话框的使用及音乐的播放等。

布局很明了,通过三个线性布局的嵌套。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
	    android:layout_width="fill_parent"
	    android:layout_height="fill_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <ImageButton
    	android:id="@+id/start"
    	android:layout_width="wrap_content"
    	android:layout_height="wrap_content"
    	android:src="@drawable/png2"/>
    <ImageButton 
    	android:id="@+id/stop" 
    	android:layout_width="wrap_content"
    	android:layout_height="wrap_content"
    	android:src="@drawable/png1"/>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	    android:orientation="vertical"
	    android:layout_width="fill_parent"
	    android:layout_height="fill_parent">
	    <TextView
	    	android:id="@+id/textView1"
	    	android:layout_width="wrap_content" 
	    	android:layout_height="wrap_content"
	    	android:textSize="25px"
	    	android:textColor="#ffffff"
	    	android:ellipsize="marquee"//跑马灯效果
	    	android:layout_weight="1"
	    	android:marqueeRepeatLimit="marquee_forever"//跑马灯无限循环
	    	android:text="@string/myTextView1"/>
	    <TextView
	    	android:id="@+id/textView2"
	    	android:textSize="15px"
	    	android:gravity="center_vertical"
	    	android:layout_weight="1"
	    	android:layout_width="wrap_content"
	    	android:layout_height="wrap_content"
	    	android:text="@string/myTextView2"/>	    	
    </LinearLayout>
</LinearLayout>
<ImageView
	    	android:layout_width="wrap_content"
	    	android:layout_height="wrap_content"
	    	android:src="@drawable/jinsha"
	    	/>
</LinearLayout>
主要用到了ImageButton、ImageView还有TextView,还指定了文字颜色和大小。

喜欢研究TextView的可以看看:http://flysnow.iteye.com/blog/822358

接下来是后台播放音乐的Service:

package yxy.music;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.MediaPlayer;
import android.os.IBinder;
public class MyService extends Service{
	MediaPlayer mp;
	ServiceReceiver serviceReceiver;
	int status = 1;//当前的状态,1没有声音播放 ,2 正在播放声音,3暂停
	@Override
	public IBinder onBind(Intent intent) {//重写的onBind方法
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public void onCreate() {//重写的onCreate方法
		// TODO Auto-generated method stub		
		status = 1;
		serviceReceiver = new ServiceReceiver();//创建BroadcastReceiver
		IntentFilter filter = new IntentFilter();//创建过滤器
		filter.addAction("yxy.music.control");//添加Action
		registerReceiver(serviceReceiver, filter);//注册BroadcastReceiver
		super.onCreate();
	}
	@Override
	public void onDestroy() {//重写的onDestroy方法
		// TODO Auto-generated method stub
		unregisterReceiver(serviceReceiver);//取消注册
		super.onDestroy();
	}
	public class ServiceReceiver extends BroadcastReceiver{//自定义BroadcastReceiver
		@Override
		public void onReceive(Context context, Intent intent) {//重写的响应方法
			// TODO Auto-generated method stub
			int action = intent.getIntExtra("ACTION", -1);//得带需要的数据
			switch(action){
			case 1://播放或暂停声音
				if(status == 1){//当前没有声音播放
					mp = MediaPlayer.create(context, R.raw.nx);
					status = 2;
					Intent sendIntent = new Intent("yxy.music.update");
					sendIntent.putExtra("update", 2);
					sendBroadcast(sendIntent);
					mp.start();
				}
				else if(status == 2){//正在播放声音
					mp.pause();	//停止
					status = 3;//改变状态 
					Intent sendIntent = new Intent("yxy.music.update");
					sendIntent.putExtra("update", 3);//存放数据
					sendBroadcast(sendIntent);//发送广播
				}
				else if(status == 3){//暂停中
					mp.start();//播放声音
					status = 2;//改变状态
					Intent sendIntent = new Intent("yxy.music.update");
					sendIntent.putExtra("update", 2);//存放数据
					sendBroadcast(sendIntent);//发送广播
				}
				break;
			case 2://停止声音
				if(status == 2 || status == 3){//播放中或暂停中
					mp.stop();//停止播放
					status = 1;//改变状态
					Intent sendIntent = new Intent("yxy.music.update");
					sendIntent.putExtra("update", 1);//存放数据
					sendBroadcast(sendIntent);//发送广播
				}
			}
		}
	}
}

给大家一个还算不错的介绍Service的网站:http://zhangyan1158.blog.51cto.com/2487362/545422

关于绑定,我还是不懂。。。所以重写那几个方法的时候用的是onCreat()&onDestroy()。

关于Intent和IntentFilter的介绍可以参考:http://flysnow.iteye.com/blog/961576
还用到了一个广播的机制,里面的很多用法我还不是很熟,越来越发现Intent的强大。。。下面看看Activity的代码:

package yxy.music;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
public class MusicPodActivity extends Activity implements OnClickListener{
	ImageButton start;//播放、暂停按钮
	ImageButton stop;//停止按钮
	ActivityReceiver activityReceiver;
	int status = 1;//当前的状态,1没有声音播放 ,2 正在播放声音,3暂停
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {//重写的onCreate方法
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);//设置当前的用户界面
        start = (ImageButton) this.findViewById(R.id.start);//得到start的引用
        stop = (ImageButton) this.findViewById(R.id.stop);//得到stop按钮的引用
        start.setOnClickListener(this);//为按钮添加监听
        stop.setOnClickListener(this);//为按钮添加监听
        activityReceiver = new ActivityReceiver();//创建BroadcastReceiver
        IntentFilter filter = new IntentFilter();//创建IntentFilter过滤器
        filter.addAction("yxy.music.update");//添加Action
        registerReceiver(activityReceiver, filter);//注册监听
        Intent intent = new Intent(this, MyService.class);//创建Intent
        startService(intent);//启动后台Service
    }
    public class ActivityReceiver extends BroadcastReceiver{//自定义的BroadcastReceiver
		@Override
		public void onReceive(Context context, Intent intent) {//重写的onReceive方法
			// TODO Auto-generated method stub
			int update = intent.getIntExtra("update", -1);//得到intent中的数据
			switch(update){//分支判断
			case 1://没有声音播放
				status = 1; //设置当前状态
				break;
			case 2://正在播放声音
				start.setImageResource(R.drawable.png3);//更换图片
				status = 2; //设置当前状态
				break;
			case 3://暂停中
				start.setImageResource(R.drawable.png2);//更换图片
				status = 3; //设置当前状态
				break;
			}
		}
    }
	@Override
	public void onClick(View v) {//接口中的方法
		// TODO Auto-generated method stub
		Intent intent = new Intent("yxy.music.control");//创建Intent
		switch(v.getId()){//分支判断
		case R.id.start://按下播放、暂停按钮
			intent.putExtra("ACTION", 1);//存放数据
			sendBroadcast(intent);//发送广播
			break;
		case R.id.stop://按下停止按钮
			intent.putExtra("ACTION", 2);//存放数据
			sendBroadcast(intent);//发送广播
			break;
		}
	}
	@Override
	protected void onDestroy() {//释放时被调用
		// TODO Auto-generated method stub
		super.onDestroy();
        Intent intent = new Intent(this, MyService.class);//创建Intent
        stopService(intent);//停止后台的Service
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu){//弹出菜单 
		menu.add(0,Menu.FIRST,0,"退出")
			.setIcon(android.R.drawable.ic_menu_delete);//设置图标
		return true;
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item){//选择的菜单项
		switch(item.getItemId()){//分支判断
		case Menu.FIRST:
			showDialog(1);//显示对话框
			break;
		}
		//将来可在此进行扩展
		return false;
	}
	@Override
	protected Dialog onCreateDialog(int id){//创建对话框
		switch(id){//判断
		case 1:
			return new AlertDialog.Builder(this)
				.setTitle("您确定退出?")
				.setPositiveButton("确定", new android.content.DialogInterface.OnClickListener(){
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						System.exit(0);//直接退出
					}
				})
				.setNegativeButton("取消", null)//取消按钮
				.create();
		default:
			return null;
		}
	}
}

我们会发现Service和Activity在互相接收广播,Activity接收到Service的广播后作出界面的变化,Service接收到Activity的广播后作出后台音乐播放的变化。

歌曲是放在raw里面的只有一首歌,呵呵。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值