Android studio给备忘录添加背景音乐
目录
一、音乐导入
在project/app/src/main/res/raw目录下添加音乐(命名要是英文的)
二、代码
(1)AndroidMainfest.xml
在AndroidMainfest.xml中的代码
<service
android:name=".MyIntentService"
android:exported="false" />
<activity android:name=".EditActivity" />
<activity android:name=".Alarm.EditAlarmActivity" />
<activity android:name=".Alarm.AlarmActivity" />
<receiver android:name=".Alarm.AlarmReceiver" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
在图示这个地方编辑以上代码
(2)MyIntentService类
package com.example.notebook;
import android.app.IntentService;
import android.content.Intent;
import android.media.MediaPlayer;
public class MyIntentService extends IntentService {
// TODO: Rename actions, choose action names that describe tasks that this
// IntentService can perform, e.g. ACTION_FETCH_NEW_ITEMS
public static final String ACTION_FOO = "com.example.notebook.action.FOO";
public static final String ACTION_BAZ = "com.example.notebook.action.BAZ";
// action声明 这里有五个背景音乐
public static final String ACTION_MUSIC_1 = "com.example.notebook.action.music1";
public static final String ACTION_MUSIC_2 = "com.example.notebook.action.music2";
public static final String ACTION_MUSIC_3 = "com.example.notebook.action.music3";
public static final String ACTION_MUSIC_4 = "com.example.notebook.action.music4";
public static final String ACTION_MUSIC_5 = "com.example.notebook.action.music5";
public static final String ACTION_MUSIC_6 = "com.example.notebook.action.music6";
// TODO: Rename parameters
public static final String EXTRA_PARAM1 = "com.example.notebook.extra.PARAM1";
public static final String EXTRA_PARAM2 = "com.example.notebook.extra.PARAM2";
// 声明MediaPlayer对象
private static MediaPlayer mediaPlayer;
public MyIntentService() {
super("MyIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
final String action = intent.getAction();
if (intent != null) {
if (ACTION_FOO.equals(action)) {
final String param1 = intent.getStringExtra(EXTRA_PARAM1);
final String param2 = intent.getStringExtra(EXTRA_PARAM2);
handleActionFoo(param1, param2);
} else if (ACTION_BAZ.equals(action)) {
final String param1 = intent.getStringExtra(EXTRA_PARAM1);
final String param2 = intent.getStringExtra(EXTRA_PARAM2);
handleActionBaz(param1, param2);
}
// 根据intent设置的action来执行对应服务的操作
if (ACTION_MUSIC_1.equals(action)){
handleActionMusic1();
}else if (ACTION_MUSIC_2.equals(action)){
handleActionMusic2();
}else if (ACTION_MUSIC_3.equals(action)){
handleActionMusic3();
}else if (ACTION_MUSIC_4.equals(action)){
handleActionMusic4();
}else if (ACTION_MUSIC_5.equals(action)){
handleActionMusic5();
}else if (ACTION_MUSIC_6.equals(action)){
handleActionMusic6();
}
}
}
/**
* 该服务执行的操作用来播放背景音乐
*/
private void handleActionMusic1() {
if(mediaPlayer != null) mediaPlayer.stop();
// 根据音乐资源文件创建MediaPlayer对象 设置循环播放属性 开始播放
mediaPlayer = MediaPlayer.create(this, R.raw.run);
mediaPlayer.setLooping(true);
mediaPlayer.start();
}
private void handleActionMusic2() {
if(mediaPlayer != null) mediaPlayer.stop();
// 根据音乐资源文件创建MediaPlayer对象 设置循环播放属性 开始播放
mediaPlayer = MediaPlayer.create(this, R.raw.dream_wedding);
mediaPlayer.setLooping(true);
mediaPlayer.start();
}
private void handleActionMusic3() {
if(mediaPlayer != null) mediaPlayer.stop();
// 根据音乐资源文件创建MediaPlayer对象 设置循环播放属性 开始播放
mediaPlayer = MediaPlayer.create(this, R.raw.be_quiet);
mediaPlayer.setLooping(true);
mediaPlayer.start();
}
private void handleActionMusic4() {
if(mediaPlayer != null) mediaPlayer.stop();
// 根据音乐资源文件创建MediaPlayer对象 设置循环播放属性 开始播放
mediaPlayer = MediaPlayer.create(this, R.raw.girly_heart);
mediaPlayer.setLooping(true);
mediaPlayer.start();
}
private void handleActionMusic5() {
if(mediaPlayer != null) mediaPlayer.stop();
// 根据音乐资源文件创建MediaPlayer对象 设置循环播放属性 开始播放
mediaPlayer = MediaPlayer.create(this, R.raw.sword_fairy);
mediaPlayer.setLooping(true);
mediaPlayer.start();
}
private void handleActionMusic6() {
if(mediaPlayer != null) mediaPlayer.stop();
// 根据音乐资源文件创建MediaPlayer对象 设置循环播放属性 开始播放
mediaPlayer = MediaPlayer.create(this, R.raw.perfect_encounter);
mediaPlayer.setLooping(true);
mediaPlayer.start();
}
/**
* Handle action Foo in the provided background thread with the provided
* parameters.
*/
private void handleActionFoo(String param1, String param2) {
// TODO: Handle action Foo
throw new UnsupportedOperationException("Not yet implemented");
}
/**
* Handle action Baz in the provided background thread with the provided
* parameters.
*/
private void handleActionBaz(String param1, String param2) {
// TODO: Handle action Baz
throw new UnsupportedOperationException("Not yet implemented");
}
}
(3)EditActivity
public boolean onOptionsItemSelected(MenuItem item) {
final String[] colors={"护眼色", "紫罗兰", "道奇蓝","碧绿色","热情粉","纯白色"};
final String[] musics={"奔跑吧","梦中的婚礼","安静","少女的心","剑仙","完美的邂逅"};
switch (item.getItemId()){
case R.id.delete:
new AlertDialog.Builder(EditActivity.this)
.setMessage("确定删除当前便签吗?")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if(openMode==4){
intent.putExtra("mode",-1); //新笔记,返回-1,什么也不做
}else {
intent.putExtra("mode",2); //已经存在的笔记,用于返回操作
intent.putExtra("id",id);
}
setResult(RESULT_OK,intent);
finish();
}
}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).create().show();
break;
case R.id.read:
Drawable drawable = item.getIcon(); //图标颜色变换
if(isRead){
drawable.setColorFilter(null); //消除上一级的改变
drawable.setColorFilter(getResources().getColor(R.color.greyC), PorterDuff.Mode.SRC_IN);
toast1.setText("您已进入编辑模式");
toast2.show();
editText.setFocusableInTouchMode(true);
editText.setFocusable(true);
isRead = false;
}else{
drawable.setColorFilter(null);
drawable.setColorFilter(getResources().getColor(R.color.p), PorterDuff.Mode.SRC_IN);
toast1.setText("您已进入阅读模式");
toast1.show();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
editText.setFocusableInTouchMode(false);
editText.setFocusable(false);
isRead = true;
}
break;
case R.id.change:
new AlertDialog.Builder(EditActivity.this)
.setTitle("选择一个背景色")
.setIcon(R.drawable.tomato)
.setItems(colors, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which){
case 0:
MainActivity.curId = 0;
break;
case 1:
MainActivity.curId = 1;
break;
case 2:
MainActivity.curId = 2;
break;
case 3:
MainActivity.curId = 3;
break;
case 4:
MainActivity.curId = 4;
break;
case 5:
MainActivity.curId = 5;
break;
default:
break;
}
getWindow().setBackgroundDrawableResource(curColor[MainActivity.curId]);
}
}).create().show();
break;
case R.id.music:
new AlertDialog.Builder(EditActivity.this)
.setTitle("选择一个背景音乐")
.setIcon(R.drawable.music_collection)
.setItems(musics, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which){
case 0:
// 启动服务播放背景音乐
intentMusic = new Intent(EditActivity.this, MyIntentService.class);
String action_1 = MyIntentService.ACTION_MUSIC_1;
// 设置action
intentMusic.setAction(action_1);
startService(intentMusic);
break;
case 1:
// 启动服务播放背景音乐
intentMusic = new Intent(EditActivity.this, MyIntentService.class);
String action_2 = MyIntentService.ACTION_MUSIC_2;
// 设置action
intentMusic.setAction(action_2);
startService(intentMusic);
break;
case 2:
// 启动服务播放背景音乐
intentMusic = new Intent(EditActivity.this, MyIntentService.class);
String action_3 = MyIntentService.ACTION_MUSIC_3;
// 设置action
intentMusic.setAction(action_3);
startService(intentMusic);
break;
case 3:
// 启动服务播放背景音乐
intentMusic = new Intent(EditActivity.this, MyIntentService.class);
String action_4 = MyIntentService.ACTION_MUSIC_4;
// 设置action
intentMusic.setAction(action_4);
startService(intentMusic);
break;
case 4:
// 启动服务播放背景音乐
intentMusic = new Intent(EditActivity.this, MyIntentService.class);
String action_5 = MyIntentService.ACTION_MUSIC_5;
// 设置action
intentMusic.setAction(action_5);
startService(intentMusic);
break;
case 5:
// 启动服务播放背景音乐
intentMusic = new Intent(EditActivity.this, MyIntentService.class);
String action_6 = MyIntentService.ACTION_MUSIC_6;
// 设置action
intentMusic.setAction(action_6);
startService(intentMusic);
break;
}
}
}).create().show();
break;
}
return super.onOptionsItemSelected(item);
}
三、演示
↑音乐播放按钮
↑音乐演示
关注公众号:Android老皮
解锁 《Android十大板块文档》 ,让学习更贴近未来实战。已形成PDF版
内容如下:
1.Android车载应用开发系统学习指南(附项目实战)
2.Android Framework学习指南,助力成为系统级开发高手
3.2023最新Android中高级面试题汇总+解析,告别零offer
4.企业级Android音视频开发学习路线+项目实战(附源码)
5.Android Jetpack从入门到精通,构建高质量UI界面
6.Flutter技术解析与实战,跨平台首要之选
7.Kotlin从入门到实战,全方面提升架构基础
8.高级Android插件化与组件化(含实战教程和源码)
9.Android 性能优化实战+360°全方面性能调优
10.Android零基础入门到精通,高手进阶之路
敲代码不易,关注一下吧。ღ( ´・ᴗ・` ) 🤔