您的位置:首页 > 其它

练习:多线程实现后台播放背景音乐的service

2015-06-30 11:19 369 查看
一、最终结果:



当点击左边按钮时,启动线程,播放音乐。当点击右边按钮时取消线程,音乐停止。

二、工程目录



三、代码

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 imgbtn1 = null;
	ImageButton imgbtn2 = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		getViews();
		// 开始音乐按钮
		imgbtn1.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(MainActivity.this,
						AudioService.class);
				startService(intent);
			}
		});
		// 停止音乐按钮
		imgbtn2.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() {
		imgbtn1 = (ImageButton) findViewById(R.id.imgBtn1);
		imgbtn2 = (ImageButton) findViewById(R.id.imgBtn2);
	}

	/*
	 * 自动播放
	 */
	// @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/imgBtn1"
        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/imgBtn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:src="@android:drawable/ic_media_pause" />

</LinearLayout>


四、遇到问题

1.停止线程方法不会

解决方法:stopService()和stopSelf()都可以停止通过startService()方式启动的service。

stopService需要传递startService(Intent service)时的intent对象作为参数,停止此intent对应的service。

stopSelf直接停止本service,不需要参数,在service中直接调用即可。

2.音乐存放在文件夹raw中,可以替换成其他mp3文件

五、相关下载

http://download.csdn.net/download/yyd_diablo/8853645
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: