android添加背景音乐
2015-07-03 15:19
435 查看
1.在AndroidManifest.xml文件中的<application>标签内加入下边语句
<service android:name=".MusicServer">
<intent-filter>
<action android:name="com.angel.Android.MUSIC"/>
<category android:name="android.intent.category.default" />
</intent-filter>
</service>
2.新建MusicServer.java类,内容为
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
public class MusicServer extends Service {
private MediaPlayer mediaPlayer;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onStart(Intent intent,int startId){
super.onStart(intent, startId);
if(mediaPlayer==null){
// R.raw.mmp是资源文件,MP3格式的
mediaPlayer = MediaPlayer.create(this, R.raw.abc);
mediaPlayer.setLooping(true);
mediaPlayer.start();
}
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
mediaPlayer.stop();
}
}
3.将歌曲放入raw文件夹下,名称为abc
4.在Activity中加入代码
private Intent intent = new Intent("com.angel.Android.MUSIC");
onCreate方法中加入startService(intent);
就可以实现了
<service android:name=".MusicServer">
<intent-filter>
<action android:name="com.angel.Android.MUSIC"/>
<category android:name="android.intent.category.default" />
</intent-filter>
</service>
2.新建MusicServer.java类,内容为
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
public class MusicServer extends Service {
private MediaPlayer mediaPlayer;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onStart(Intent intent,int startId){
super.onStart(intent, startId);
if(mediaPlayer==null){
// R.raw.mmp是资源文件,MP3格式的
mediaPlayer = MediaPlayer.create(this, R.raw.abc);
mediaPlayer.setLooping(true);
mediaPlayer.start();
}
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
mediaPlayer.stop();
}
}
3.将歌曲放入raw文件夹下,名称为abc
4.在Activity中加入代码
private Intent intent = new Intent("com.angel.Android.MUSIC");
onCreate方法中加入startService(intent);
就可以实现了
相关文章推荐
- android截屏实现
- 解决Android Studio在MacbookPro 13下卡顿的问题
- Android文件命名规范
- Android开发:shape和selector和layer-list的(详细说明)
- 为android游戏开发-准备的地图编辑器-初步刷地图
- Android自带数据库-SQLite探究
- android中 MiniTwitter 实现记住密码
- Android广播机制
- android 利用Service实现背景音乐的播放
- Android Exception 17(database or disk is full)
- android屏幕framebuffer绘图
- Android 滑动界面实现---Scroller类别 从源代码和开发文档了解(让你的移动布局)
- android软键盘现实和隐藏的监听
- Android QQ登录验证的小例子
- android 5.2
- Android入门(5)——多状态按钮ToggleButton、多选按钮CheckBox、RadioGroup与RadioButtont按钮
- Android的消息机制
- Android Studio使用OpenCV的配置方法
- android工程里,配置文件的实现。
- Android RGB颜色对照表