Android开发之初探音频的播放
/*
* Android开发之初探音频的播放
* 北京Android俱乐部群:167839253
* Created on: 2011-8-23
* Author: blueeagle
* Email: liujiaxiang@gmail.com
*/
音频的播放
Android平台中关于音频的播放有两种方式,一种是SoundPool,一种是MediaPlayer。SoundPool适合短促但是对反应速度要求较高的情况。但是MediaPlay则适合较长但是对时间要求不高的情况。
音频文件一般都放在res的raw目录下。
对于SoundPool的说明:SoundPool初始化的过程是异步的,也就是说,当对SoundPool初始化时,系统会自动启动一个后台线程来完成初始化工作。因此并不会影响前台其他程序的运行。但也带来一个问题,调用初始化操作后不能立即播放,需要等待一点时间,否则可能会出错。另外,SoundPool可以同时播放多个音频文件,但是MediaPlayer同意时间却只能播放一个。
源码如下所示:
/*
* Android开发之初探音频的播放
* MyMeidaTest01.java
* Created on: 2011-8-23
* Author: blueeagle
* Email: liujiaxiang@gmail.com
*/
package com.blueeagle;
import java.util.HashMap;
import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MyMeidaTest01 extends Activity {
Button button1;
Button button2;
Button button3;
Button button4;
TextView myTextView;
MediaPlayer myMediaplayer;
SoundPool mySoundpool;
HashMap<Integer,Integer> soundPoolMap;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initSounds();
initUI();
}
public void playSound(int sound , int loop){
//SoundPool的播放方法
AudioManager mgr = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
float streamVolumeCurrent = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
float streamVolumeMax = mgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float volume = streamVolumeCurrent/streamVolumeMax;
mySoundpool.play(soundPoolMap.get(sound), volume, volume, 1, loop, 1f);
}
private void initUI() {
// TODO Auto-generated method stub
myTextView = (TextView)findViewById(R.id.mytextview);
button1 = (Button)findViewById(R.id.button1);
button2 = (Button)findViewById(R.id.button2);
button3 = (Button)findViewById(R.id.button3);
button4 = (Button)findViewById(R.id.button4);
button1.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
myTextView.setText("使用MediaPlayer播放声音");
if(!myMediaplayer.isPlaying())
myMediaplayer.start();
}
});
button2.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
myTextView.setText("暂停MediaPlayer播放声音");
if(myMediaplayer.isPlaying())
myMediaplayer.pause();
}
});
button3.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
myTextView.setText("使用SoundPool播放声音");
playSound(1,0);
}
});
button4.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
myTextView.setText("暂停SoundPool播放声音");
mySoundpool.pause(1);
}
});
}
private void initSounds() {
// TODO Auto-generated method stub
myMediaplayer = MediaPlayer.create(this, R.raw.music);
mySoundpool = new SoundPool(4,AudioManager.STREAM_MUSIC,100);
soundPoolMap = new HashMap<Integer,Integer>();
soundPoolMap.put(1,mySoundpool.load(this,R.raw.kick,1));
//初始化声音操作,使用SoundPool时,一般将声音放进一个HashMap中,便于声音的管理和操作。
}
}
XML布局文件如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/mytextview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="没有播放任何声音"
/>
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="使用Media播放声音"
/>
<Button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="暂停Media播放声音"
/>
<Button
android:id="@+id/button3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="使用SoundPool播放声音"
/>
<Button
android:id="@+id/button4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="暂停SoundPool播放声音"
/>
</LinearLayout>