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

android 声音播放 mediaplay soundpool

2014-08-06 11:22 246 查看
首先在res文件夹内新建raw文件夹,并在raw内存放声音文件。

mediaplay方法:

在onCreate(Bundle saveInstanceState)方法内添加

“MediaPlay mp=MediaPlayer.create(context,R.raw.backsound);”//用于初始化mediapaly

MediaPlayer.create(Context context, int resid)

MdeiaPlayer.create(Context context,Uri uri)

然后

mp.start();//用mediaplay播放声音

mp.pause();//停止播放

mp.isPlaying();//用于判断mediaplay是否正在播放

soundpool方法

常把声音放在hashmap内

SoundPool.play(int soundID, float leftVolume, float rightVolume, int priority, int loop, float rate);


rate为播放速率,最慢为0.5,最快为2

loop为播放次数

package com.example.sound;

import java.util.HashMap;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {
Button bt1,bt2,bt3,bt4;
TextView tv;
MediaPlayer mp;
SoundPool sp;
HashMap<Integer,Integer> spm;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initSounds();                               //初始化声音
init();                                     //控件引用与监听
}

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

void init()
{
tv=(TextView)findViewById(R.id.textView1);
bt1=(Button)findViewById(R.id.button1);
bt2=(Button)findViewById(R.id.button2);
bt3=(Button)findViewById(R.id.button3);
bt4=(Button)findViewById(R.id.button4);
bt1.setOnClickListener(this);
bt2.setOnClickListener(this);
bt3.setOnClickListener(this);
bt4.setOnClickListener(this);
}
void initSounds()                       //初始化声音
{
mp=MediaPlayer.create(this,R.raw.futa);  //初始化MediaPlayer
sp=new SoundPool(4,AudioManager.STREAM_MUSIC,100);
spm=new HashMap<Integer,Integer>();
spm.put(1,sp.load(this,R.raw.ding,1));
}
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;
sp.play(spm.get(sound),volume,volume,1,loop,1f);    //播放声音
}

@Override
public void onClick(View v) {            //实现各单击事件
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.button1:
{
tv.setText("使用MediaPlayer播放声音");
if(!mp.isPlaying())            //如果现在没有音乐正在播放
{mp.start();}                  //播放音乐
break;
}
case R.id.button2:
{
tv.setText("暂停MediaPlayer播放声音");
if(mp.isPlaying())             //如果现在没播放音乐
{mp.pause();}                 //停止音乐
break;
}
case R.id.button3:
{
tv.setText("使用soundPool播放声音");
this.playsound(1, 0);      //播放音乐
break;
}
case R.id.button4:
{
tv.setText("暂停soundPool播放声音");
sp.pause(1);      //暂停音乐
break;
}
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: