背景音乐功能

来源:互联网 发布:最终兵器 弓 知乎 编辑:程序博客网 时间:2024/05/22 12:29
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类,内容为:
<span style="font-family: Arial, Helvetica, sans-serif;"></span><pre name="code" class="html"><pre name="code" class="html">import java.io.IOException;import com.servicedemo.R;import android.app.Service;import android.content.Intent;import android.media.MediaPlayer;import android.os.Bundle;import android.os.IBinder;import android.util.Log;import android.widget.Toast;public class MusicService extends Service {      private static final String TAG = "MyService";            private MediaPlayer mediaPlayer;        @Override      public IBinder onBind(Intent arg0) {          return null;      }        @Override      public void onCreate() {          Log.v(TAG, "onCreate");          Toast.makeText(this, "show media player", Toast.LENGTH_SHORT).show();            if (mediaPlayer == null) {              mediaPlayer = MediaPlayer.create(this, R.raw.ywgsbd);              mediaPlayer.setLooping(false);          }      }        @Override      public void onDestroy() {          Log.v(TAG, "onDestroy");          Toast.makeText(this, "stop media player", Toast.LENGTH_SHORT);          if (mediaPlayer != null) {              mediaPlayer.stop();              mediaPlayer.release();          }      }        @Override      public void onStart(Intent intent, int startId) {          Log.v(TAG, "onStart");          if (intent != null) {              Bundle bundle = intent.getExtras();              if (bundle != null) {                  int op = bundle.getInt("op");                  switch (op) {                  case 1:                      play();                      break;                  case 2:                      stop();                      break;                  case 3:                      pause();                      break;                  }              }          }      }        public void play() {          if (!mediaPlayer.isPlaying()) {              mediaPlayer.start();          }      }        public void pause() {          if (mediaPlayer != null && mediaPlayer.isPlaying()) {              mediaPlayer.pause();          }      }        public void stop() {          if (mediaPlayer != null) {              mediaPlayer.stop();              try {                  mediaPlayer.prepare();  // 在调用stop后如果需要再次通过start进行播放,需要之前调用prepare函数              } catch (IOException ex) {                  ex.printStackTrace();              }          }      }  }public class PlayMusicService extends Activity implements OnClickListener {private Intent intent;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.music_service);playBtn.setOnClickListener(this);stopBtn.setOnClickListener(this);pauseBtn.setOnClickListener(this);exitBtn.setOnClickListener(this);closeBtn.setOnClickListener(this);}@Overridepublic void onClick(View v) {int op = -1;intent = new Intent("com.homer.service.musicService");switch (v.getId()) {case R.id.play: // play musicop = 1;break;case R.id.stop: // stop musicop = 2;break;case R.id.pause: // pause musicop = 3;break;case R.id.close: // close activitythis.finish();break;case R.id.exit: // stopServiceop = 4;stopService(intent);this.finish();break;}Bundle bundle = new Bundle();bundle.putInt("op", op);intent.putExtras(bundle);startService(intent); // startService}}


3.将歌曲放入raw文件夹下,名称为mmp4.在Activity中加入代码private Intent intent = new Intent("com.angel.Android.MUSIC");onCreate方法中加入startService(intent);就可以实现了

0 0
原创粉丝点击