package com.example.administrator.project; import android.app.Service; import android.content.Intent; import android.media.MediaPlayer; import android.os.IBinder; public class MusicService extends Service { public MediaPlayer mp; @Override public void onStart(Intent intent, int startId) { // TODO Auto-generated method stub // 开始播放音乐 mp.start(); // 音乐播放完毕的事件处理 mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { public void onCompletion(MediaPlayer mp) { // TODO Auto-generated method stub // 循环播放 try { mp.start(); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); // 播放音乐时发生错误的事件处理 mp.setOnErrorListener(new MediaPlayer.OnErrorListener() { public boolean onError(MediaPlayer mp, int what, int extra) { // TODO Auto-generated method stub // 释放资源 try { mp.release(); } catch (Exception e) { e.printStackTrace(); } return false; } }); super.onStart(intent, startId); } @Override public void onCreate() { // TODO Auto-generated method stub // 初始化音乐资源 try { // 创建MediaPlayer对象 mp = new MediaPlayer(); // 将音乐保存在res/raw/xingshu.mp3,R.java中自动生成{public static final int xingshu=0x7f040000;} mp = MediaPlayer.create(MusicService.this, R.raw.melt); // 在MediaPlayer取得播放资源与stop()之后要准备PlayBack的状态前一定要使用MediaPlayer.prepeare() mp.prepare(); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } super.onCreate(); } @Override public void onDestroy() { // TODO Auto-generated method stub // 服务停止时停止播放音乐并释放资源 mp.stop(); mp.release(); super.onDestroy(); } @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } }

_nrxsh LV12
6月16日
阿九11111 LV4
2024年5月21日
求学的熊猫 LV11
2024年5月20日
121514713 LV2
2023年5月26日
dglin20001030 LV1
2023年5月18日
watsonChen LV1
2023年2月9日
1532593037 LV8
2023年1月7日
yzhszz LV3
2022年12月27日
SQ2930501923 LV14
2022年12月12日
Highlighz LV4
2022年6月29日

_nrxsh LV12
6月16日
5460545
2024年12月24日
暂无贡献等级
chenranr LV10
2024年12月20日
2636804923 LV6
2024年6月20日
AIWU1234 LV2
2024年6月9日
lilitu LV6
2024年5月29日
今晚都别睡啦 LV9
2024年5月29日
happySuperman LV2
2024年5月29日
阿九11111 LV4
2024年5月21日
Aban-beautiful
2024年5月13日
暂无贡献等级