1.把MP3文件放在src文件的raw目录下。
2.写音乐Server
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import com.example.administrator.ydxcfwpt.R;
/**
* Created by Administrator on 2018/1/17.
*/
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.music);
mediaPlayer.setLooping(true);
mediaPlayer.start();
}
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
mediaPlayer.stop();
}
}
3.在需要启动的地方用
private Intent intent10 = new Intent("com.angel.Android.MUSIC");
startService(intent10);
4.静态注册
<service
android:name=".Service.MyService"
android:enabled="true"
android:exported="true" />
2.写音乐Server
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import com.example.administrator.ydxcfwpt.R;
/**
* Created by Administrator on 2018/1/17.
*/
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.music);
mediaPlayer.setLooping(true);
mediaPlayer.start();
}
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
mediaPlayer.stop();
}
}
3.在需要启动的地方用
private Intent intent10 = new Intent("com.angel.Android.MUSIC");
startService(intent10);
4.静态注册
<service
android:name=".Service.MyService"
android:enabled="true"
android:exported="true" />