Android添加背景音乐

介绍:

在android应用程序里,有一种没有UI的类(android.app.Service)——Service。简单来说,Service是一个 background process(背景程序),通过背景程序,可以实现一些不需要UI的功能,

一、在工程中添加一个新类AudioService  ,AudioService类继承 android.app.Service
1. Service 对象以 separated process 的方式执行,这表示 Service 与 UI(Activity)并不在同一个 process 里执行,而是各自在不同的 process 执行。

2. Android应用程序是在 Activity 启动与停止 Service。

3. 重载(override)onStart() 方法(method)在 Service 被启动,执行我们想要的背景功能。

4. 重载 onDestroy() 方法在 Service 被停止时,停止执行中的背景功能。

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.shijianzhuyu);
		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已经被挂起或是被销毁时,背景音乐还是在继续播放的,这也说明Service与Activity是两个不同的进程,让Activity在OnStop时把背景音乐也停止播放,重载Activity的OnStop:
package com.example.service;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
    }
    /**调用 startService() 即可启动一个 Service ,
     *只是,startService() 的参数是一个「Intent」型,并不是所要启动的类名。
     */
    protected void onResume() {
    	super.onResume();
    	startService(new Intent(this,AudioService.class));
    }
   
    //让Activity在OnStop时把背景音乐也停止播放,重载Activity的OnStop:
    @Override
	 protected void onStop() {
	  // TODO Auto-generated method stub
	  Intent intent = new Intent(MainActivity.this,AudioService.class);
	  stopService(intent);
	  super.onStop();
	 }
    @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;
    }
    
}
 
 <!-- 修改 Androidmanifest.xml 的目的是为了「 Android 应用程序加入一个 Service 类别」,这样才有办法驱动 Service。 -->
 <service android:name="com.example.service.AudioService" android:exported="true"></service>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值