Android中的多媒体开发(一)——使用SoundPool来播放音效

   

SoundPool(int maxStreams, int streamType, int srcQuality) 参数依次是:

  • ①指定支持多少个声音,SoundPool对象中允许同时存在的最大流的数量。
  • ②指定声音类型,流类型可以分为STREAM_VOICE_CALL(通话), STREAM_SYSTEM(系统), STREAM_RING(铃声),STREAM_MUSIC(媒体音量) 和STREAM_ALARM(警报)四种类型。在AudioManager中定义。
  • ③指定声音品质(采样率变换质量),一般直接设置为0!、

 

以下是对它的常用方法的介绍:

1.加载声音资源

 

  • load(Context context,int resid,int priority)
  • load(String path,int priority)
  • load(FileDescriptor fd,long offset,long length,int priority)
  • load(AssetFileDescriptor afd,int priority)

 

参数介绍:

  • context:上下文
  • resId:资源id
  • priority:没什么用的一个参数,建议设置为1,保持和未来的兼容性
  • path:文件路径
  • FileDescriptor:貌似是流吧,这个我也不知道
  • AssetFileDescriptor:从asset目录读取某个资源文件,其用法: AssetFileDescriptor descriptor = assetManager.openFd("biaobiao.mp3");

 

 

2.播放控制

play(int soundID,float leftVolume,float rightVolume,int priority,int loop,float rate

参数依次是:

  • soundID:Load()返回的声音ID号
  • leftVolume:左声道音量设置
  • rightVolume:右声道音量设置
  • priority:指定播放声音的优先级,数值越高,优先级越大。
  • loop:指定是否循环:-1表示无限循环,0表示不循环,其他值表示要重复播放的次数
  • rate:指定播放速率:1.0的播放率可以使声音按照其原始频率,而2.0的播放速率,可以使声音按照其 原始频率的两倍播放。如果为0.5的播放率,则播放速率是原始频率的一半。播放速率的取值范围是0.5至2.0。

 

3,资源释放

方法:可以通过release()方法释放所有SoundPool对象所占据的内存和资源,也可以根据声音ID来释放。

 

下面是使用SoundPool实现的一个代码示例:

1.    运行效果图:

2.    MainActivity代码:

 

import android.content.res.AssetManager;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import java.util.HashMap;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private Button btnOne;
    private Button btnTwo;
    private Button btnThree;
    private Button btnFour;
    private Button btnFive;
    private Button btn_release;
    private AssetManager aManager;
    private SoundPool mSoundPool = null;
    private HashMap<Integer, Integer> soundID = new HashMap<Integer, Integer>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        aManager = getAssets();
        try {
            initSP();
        } catch (Exception e) {
            e.printStackTrace();
        }
        bindViews();
    }

    private void bindViews() {
        btnOne = (Button) findViewById(R.id.btn_play1);
        btnTwo = (Button) findViewById(R.id.btn_play2);
        btnThree = (Button) findViewById(R.id.btn_play3);
        btnFour = (Button) findViewById(R.id.btn_play4);
        btnFive = (Button) findViewById(R.id.btn_play5);
        btn_release = (Button) findViewById(R.id.btn_release);

        btnOne.setOnClickListener(this);
        btnTwo.setOnClickListener(this);
        btnThree.setOnClickListener(this);
        btnFour.setOnClickListener(this);
        btnFive.setOnClickListener(this);
        btn_release.setOnClickListener(this);

    }

    private void initSP() throws Exception{
        //设置最多可容纳5个音频流,音频的品质为5
        mSoundPool = new SoundPool(5, AudioManager.STREAM_SYSTEM, 5);
        soundID.put(1, mSoundPool.load(this, R.raw.duang, 1));
        soundID.put(2 , mSoundPool.load(getAssets().openFd("biaobiao.mp3") , 1));  //需要捕获IO异常
        soundID.put(3, mSoundPool.load(this, R.raw.duang, 1));
        soundID.put(4, mSoundPool.load(this, R.raw.duang, 1));
        soundID.put(5, mSoundPool.load(this, R.raw.duang, 1));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_play1:
                mSoundPool.play(soundID.get(1), 1, 1, 0, 0, 1);
                break;
            case R.id.btn_play2:
                mSoundPool.play(soundID.get(2), 1, 1, 0, 0, 1);
                break;
            case R.id.btn_play3:
                mSoundPool.play(soundID.get(3), 1, 1, 0, 0, 1);
                break;
            case R.id.btn_play4:
                mSoundPool.play(soundID.get(4), 1, 1, 0, 0, 1);
                break;
            case R.id.btn_play5:
                mSoundPool.play(soundID.get(5), 1, 1, 0, 0, 1);
                break;
            case R.id.btn_release:
                mSoundPool.release();   //回收SoundPool资源
                break;
        }
    }
}

3.    activity_main.xml

 

<LinearLayout 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:orientation="vertical"
    tools:context=".MainActivity">


    <Button
        android:id="@+id/btn_play1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="声音1" />


    <Button
        android:id="@+id/btn_play2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="声音2" />

    <Button
        android:id="@+id/btn_play3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="声音3" />

    <Button
        android:id="@+id/btn_play4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="声音4" />

    <Button
        android:id="@+id/btn_play5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="声音5" />

    <Button
        android:id="@+id/btn_release"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="释放SoundPool" />

</LinearLayout>

    点击声音1~5按钮会发出声音,但当点击最后一个release按钮将SoundPool释放后,再去按就没有任何效果了哦。

 

4.附录代码:

SoundPool

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值