背景音乐
2015-07-04 16:31
288 查看
mainActivity:
package com.example.service;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.ImageButton;
public class MainActivity extends Activity {
private ImageButton StartBtn=null;
private ImageButton StopBtn=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StartBtn =(ImageButton)findViewById(R.id.StartBtn);
StopBtn =(ImageButton)findViewById(R.id.StopBtn);
}
//开始播放音乐
public void start(View v){
Intent intent = new Intent(MainActivity.this,AudioService.class);
startService(intent);
}
//停止音乐
public void stop(View v){
Intent intent = new Intent(MainActivity.this,AudioService.class);
stopService(intent);
}
/* @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:
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:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:background="@drawable/bk"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ImageButton
android:id="@+id/StartBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="66dp"
android:onClick="start"
android:src="@drawable/start" />
<ImageButton
android:id="@+id/StopBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/StartBtn"
android:layout_alignTop="@+id/StartBtn"
android:layout_marginLeft="44dp"
android:layout_toRightOf="@+id/StartBtn"
android:onClick="stop"
android:src="@drawable/stop" />
</RelativeLayout>
package com.example.service;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.ImageButton;
public class MainActivity extends Activity {
private ImageButton StartBtn=null;
private ImageButton StopBtn=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StartBtn =(ImageButton)findViewById(R.id.StartBtn);
StopBtn =(ImageButton)findViewById(R.id.StopBtn);
}
//开始播放音乐
public void start(View v){
Intent intent = new Intent(MainActivity.this,AudioService.class);
startService(intent);
}
//停止音乐
public void stop(View v){
Intent intent = new Intent(MainActivity.this,AudioService.class);
stopService(intent);
}
/* @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:
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:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:background="@drawable/bk"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ImageButton
android:id="@+id/StartBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="66dp"
android:onClick="start"
android:src="@drawable/start" />
<ImageButton
android:id="@+id/StopBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/StartBtn"
android:layout_alignTop="@+id/StartBtn"
android:layout_marginLeft="44dp"
android:layout_toRightOf="@+id/StartBtn"
android:onClick="stop"
android:src="@drawable/stop" />
</RelativeLayout>
相关文章推荐
- hive sql 查询 Child Error 错误追究
- 土地划分(计算几何——线段相交)
- 两个线程交叉打印(最简单的)
- 真实故事:网站遭遇DOS攻击
- Windows上的文件合并命令
- hive sql 查询 Child Error 错误追究
- 1、uc/os:进程、线程、(void*)0 的简单解释
- PAT 1011 World Cup Betting
- JAVA中的继承和覆盖
- 谈一下最近期的学习计划
- opencv图像基本操作算法
- 黑马程序员——程序流程控制
- SNAT , DNAT区别
- android imageButton 透明图片
- 网页制作必须设置的一些属性
- Android开发(三十)——ScrollView中ListView的高度自动适应
- vim 命令
- PAT 1008 Elevator
- UVA - 116 Unidirectional TSP
- “用户体验不是把每个环节做到极致,而是在关键环节打动用户“这句话对吗?