通过多线程实现背景音乐的开始和结束
一、主要内容
主要部分包括MainActivity.java 、AudioService.java和activity_main.xml文件。
二、MainActivity.java代码
- package com.example.service;
- import android.media.MediaPlayer;
- import android.os.Bundle;
- import android.os.IBinder;
- import android.app.Activity;
- import android.content.Intent;
- import android.view.Menu;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.ImageButton;
- public class MainActivity extends Activity {
- ImageButton btn1= null;
- ImageButton btn2= null;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- getViews();
- // 开始音乐按钮
- btn1.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent intent = new Intent(MainActivity.this,
- AudioService.class);
- startService(intent);
- }
- });
- // 停止音乐按钮
- btn2.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- Intent intent = new Intent(MainActivity.this,
- AudioService.class);
- stopService(intent);
- }
- });
- }
- /*
- * 获取控件
- */
- private void getViews() {
- btn1= (ImageButton) findViewById(R.id.btn1);
- btn2= (ImageButton) findViewById(R.id.btn2);
- }
- /*
- * 自动播放
- */
- // @Override
- // protected void onResume() {
- // super.onResume();
- // startService(new Intent(this,AudioService.class));
- // }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- }
三、AudioService.java
- package com.example.service;
- /**
- * 多线程实现后台播放背景音乐的service
- */
- import android.app.Service;
- import android.content.Intent;
- import android.media.MediaPlayer;
- import android.os.Binder;
- import android.os.IBinder;
- public class AudioService extends Service implements
- MediaPlayer.OnCompletionListener {
- // 实例化MediaPlayer对象
- MediaPlayer player;
- private final IBinder binder = new AudioBinder();
- @Override
- public IBinder onBind(Intent intent) {
- return binder;
- }
- public void onCreate() {
- super.onCreate();
- // 从raw文件夹中获取一个应用自带的mp3文件
- player = MediaPlayer.create(this, R.raw.qq);
- player.setOnCompletionListener(this);
- player.setLooping(true);
- }
- @Override
- public int onStartCommand(Intent intent, int flags, int startId) {
- super.onStartCommand(intent, flags, startId);
- if (!player.isPlaying()) {
- new MusicPlayThread().start();
- } else
- player.isPlaying();
- return START_STICKY;
- }
- /**
- * 当Audio播放完的时候触发该动作
- */
- public void onCompletion(MediaPlayer mp) {
- stopSelf();// 结束了,则结束Service
- }
- public void onDestroy() {
- super.onDestroy();
- if (player.isPlaying()) {
- player.stop();
- }
- player.release();
- }
- // 为了和Activity交互,我们需要定义一个Binder对象
- public class AudioBinder extends Binder {
- // 返回Service对象
- public AudioService getService() {
- return AudioService.this;
- }
- }
- private class MusicPlayThread extends Thread {
- public void run() {
- if (!player.isPlaying()) {
- player.start();
- }
- }
- }
- }
四、activity_main.xml
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:id="@+id/LinearLayout2"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="horizontal"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- tools:context=".MainActivity" >
- <ImageButton
- android:id="@+id/btn1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:src="@android:drawable/ic_media_play" />
- <ImageButton
- android:id="@+id/btn2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:src="@android:drawable/ic_media_pause" />
- </LinearLayout>