您的位置:首页 > 移动开发 > Android开发

Android背景音乐开关示例

2015-07-03 15:45 465 查看
Android背景音乐示例,Android背景音乐需要用到server,服务(Service)是Android中实现程序后台运行的解决方案,它非常适合用于去执行那些不需要和用户交互而且还要求长期运行的任务。服务的运行不依赖于任何用户界面,即使当程序被切换到后台,或者用户打开了另外一个应用程序,服务仍然能够保持正常运行。

程序中我用了 Switch控件,Switch控件只能在Android4.0版本以上使用,因此程序的版本不能低于Android4.0


<Switch

        android:id="@+id/st_music"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignParentTop="true"

        android:layout_centerHorizontal="true"

        android:textOn="@string/play"

        android:textOff="@string/stop"

        android:layout_marginTop="184dp"

        android:text="@string/music_bg" />

背景音乐的server我是新建了一个AudioService类并继承了MediaPlayer.OnCompletionListener,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;

import android.widget.Toast;

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.lb);
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();
}
}
}

}

在MainActivity中首先为switch创建一个监听事件,在事件中监测Switch开关的状态,并根据状态启动服务和关闭服务。同时为了能够记录背景音乐的开关 使程序下次启动能够判断背景音乐的状态,使用了SharedPreferences记录背景音乐的状态。

package com.example.service;

import android.os.Bundle;

import android.app.Activity;

import android.content.Intent;

import android.content.SharedPreferences;

import android.content.SharedPreferences.Editor;

import android.view.Menu;

import android.widget.Button;

import android.widget.CompoundButton;

import android.widget.CompoundButton.OnCheckedChangeListener;

import android.widget.Switch;

import android.widget.Toast;

public class MainActivity extends Activity {
private Switch st_music;
private SharedPreferences sharedPreferences;
private boolean cbIscheck;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        

       st_music=(Switch) findViewById(R.id.st_music);

       

       sharedPreferences=getSharedPreferences("user", MODE_PRIVATE);

       getIscheck();

  

       st_music.setOnCheckedChangeListener(new OnCheckedChangeListener() {

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Editor edit=sharedPreferences.edit();
if(isChecked){
//背景音乐打开,启动服务
edit.putBoolean("ischecked", true);
startService(new Intent(MainActivity.this, AudioService.class));
}else{
//背景音乐关闭,关闭服务
edit.clear();
stopService(new Intent(MainActivity.this, AudioService.class));
}
edit.commit();
}
});

    }

    @Override

    protected void onResume() {

    super.onResume();

    }

    @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;

    }

    

    public void getIscheck(){

    cbIscheck=sharedPreferences.getBoolean("ischecked",false);

    st_music.setChecked(cbIscheck);

    if(st_music.isChecked()){
startService(new Intent(MainActivity.this, AudioService.class));
}

    }

    

    protected void onStop() { //程序结束,背景音乐停止播放

    super.onStop();

    stopService(new Intent(MainActivity.this, AudioService.class));

    }

}

服务还需要在 AndroidManifest中注册服务<service android:name="com.example.service.AudioService"></service>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Android背景音乐