android 播放声音资源,android播放音效例子 (翻页音效、警报音效通用 只需传入声音源)...
音效播放:
資源文件: res/raw/filename
聲音池類:SoundPool sp = new
SoundPool(同時最大播放個數(shù),AudioManager.STREAM_MUSIC,0);
將加載里音頻文件的SoundPool添加到一個HashMap中,提供給以后的調(diào)用
HashMap spMap = new
HashMap
Integer>();
spMap.put(1,sp.load(this, R.raw.filename1,1));
spMap.put(2,sp.load(this, R.raw.filename2,1));
音效播放函數(shù):
public void playSounds(int sound, int number){
//實(shí)例化AudioManager對象,控制聲音
AudioManager
am =
(AudioManager)this.getSystemService(this.AUDIO_SERVICE);
//最大音量
float
audioMaxVolumn =
am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
//當(dāng)前音量
float
audioCurrentVolumn =
am.getStreamVolume(AudioManager.STREAM_MUSIC);
float
volumnRatio = audioCurrentVolumn/audioMaxVolumn;
//播放
sp.play(spMap.get(sound),
//聲音資源
volumnRatio, ?//左聲道
volumnRatio, ?//右聲道
1, ?//優(yōu)先級,0最低
number, ?//循環(huán)次數(shù),0是不循環(huán),-1是永遠(yuǎn)循環(huán)
1);?//回放速度,0.5-2.0之間。1為正常速度
}
音效暫停:
sp.pause(spMap.get(1));
代碼如下:
package
com.litsoft;
import java.util.HashMap;
import android.app.Activity;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class TestSounds extends Activity {
private
Button bPlay;
private
Button bPause;
private
SoundPool sp;
private
HashMap
spMap;
public void
onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bPlay = (Button)findViewById(R.id.button01);
bPause = (Button)findViewById(R.id.button02);
sp = new SoundPool(2,AudioManager.STREAM_MUSIC,0);
spMap = new
HashMap();
spMap.put(1, sp.load(this, R.raw.qiang, 1));
bPlay.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
// TODO Auto-generated method
stub
playSounds(1,1);
}
});
bPause.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method
stub
sp.pause(spMap.get(1));
}
});
}
public void
playSounds(int sound, int number){
AudioManager am =
(AudioManager)this.getSystemService(this.AUDIO_SERVICE);
float audioMaxVolumn =
am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float audioCurrentVolumn =
am.getStreamVolume(AudioManager.STREAM_MUSIC);
float volumnRatio =
audioCurrentVolumn/audioMaxVolumn;
sp.play(spMap.get(sound), volumnRatio,
volumnRatio, 1, number, 1);
}
}
version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
android:id="@+id/button01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Play">
android:id="@+id/button02"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Pause">
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的android 播放声音资源,android播放音效例子 (翻页音效、警报音效通用 只需传入声音源)...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++: int int int * i
- 下一篇: 谈谈热门的脑机芯片