播放本地资源assets音频

1.创建BeatBox

public class BeatBox {
    private static final String TAG="BeatBox";
    private static final String SOUNDS_FOLDER="light";
    private static final int MAX_SOUNDS=1;
    //访问assets需要用到AssetManager类
    private AssetManager mAssets;
    private String[] soundNames;
    private List<Sound> mSounds=new ArrayList<>();
    private final SoundPool mSoundPool;
    private String path;

    public BeatBox(Context context,String path){
        this.mAssets=context.getAssets();
        this.path = path;
        /**
         * SoundPool(int, int,int)
         * 1.第一个参数:指定同时播放多少个音频。
         * 此处指定了6个,在播放6个音频时,如果尝试播放第7个,则会停止播放原来的音频
         * 2.第二个参数:确定音频流类型。
         * Android有很多不同的音频流,都有各自独立的音量控制选项。如降低音乐音量,不会影响闹钟的音量
         * STREAM_MUSIC  使用的是同音乐和游戏一样的音量控制
         * 3.第三个参数:指定采样率转换品质。这个参数不起作用,一般传入0
         */
        mSoundPool = new SoundPool(MAX_SOUNDS, AudioManager.STREAM_MUSIC,0);

        //调出声音文件的清单
        LoadSounds();
    }
    /**
     * 调出声音文件的清单的方法
     */
    private void LoadSounds(){
        String[] soundNames = new String[0];
        try {
            //拿到声音文件的文件名
            //AssetManager.list(String) 能列出指定目录中所有文件名
            soundNames = mAssets.list(path);
            Log.e(TAG, "Found  "+ soundNames.length+"个sound");
        } catch (IOException e) {
            e.printStackTrace();
            Log.e(TAG, "Could not list assets",e );
            // TODO: 2016/11/22
        }
        for (String filename:soundNames) {
            String assetPath=path+"/"+filename;//拿到声音文件路径
            Sound sound=new Sound(assetPath);//拿到一个声音的对象
            try {
                load(sound);//将音频文件载入
            } catch (IOException e) {
                e.printStackTrace();
            }
            mSounds.add(sound);//将对象加入到集合中,初始化数据
        }
    }

    /**
     * 规范代码
     * @return
     */
    public List<Sound> getSounds(){
        return mSounds;
    }

    /**
     * 加载音频
     */
    private void load(Sound sound) throws IOException {
        AssetFileDescriptor assetFileDescriptor = mAssets.openFd(sound.getmAssetPath());
        //调用mSoundPool.load(AssetFileDescriptor,int)可以把文件载入SoundPool待播
        int soundId = mSoundPool.load(assetFileDescriptor, 1);
        sound.setSoundId(soundId);
    }
    /**
     * 播放音频
     */
    public void play(Sound sound){
        Integer soundId = sound.getSoundId();
        if (soundId==null){
            return;
        }
        //SoundPool.play(int soundID, float leftVolume, float rightVolume, int priority, int loop, float rate)
        //参数依次代表:音频Id、左音量、右音量、优先级(无效)、是否循环、播放速率
        mSoundPool.play(soundId,1.0f,1.0f,1,0,1.0f);
    }
    /**
     * 继续播放音频
     */
    public void resume(Sound sound){
        Integer soundId = sound.getSoundId();
        if (soundId==null){
            return;
        }
        //SoundPool.play(int soundID, float leftVolume, float rightVolume, int priority, int loop, float rate)
        //参数依次代表:音频Id、左音量、右音量、优先级(无效)、是否循环、播放速率
        //mSoundPool.play(soundId,1.0f,1.0f,1,0,1.0f);
        mSoundPool.resume(soundId);
    }
    /**
     * 暂停音频
     */
    public void pause(Sound sound){
        Integer soundId = sound.getSoundId();
        if (soundId==null){
            return;
        }
//        SoundPool.play(int soundID, float leftVolume, float rightVolume, int priority, int loop, float rate)
//        //参数依次代表:音频Id、左音量、右音量、优先级(无效)、是否循环、播放速率
//        mSoundPool.play(soundId,1.0f,1.0f,1,0,1.0f);
        mSoundPool.pause(soundId);
    }
    /**
     * 释放音频
     */
    public void release(){
        mSoundPool.release();
    }
}

2     

public class Sound {
    private String mAssetPath;//声音文件路径
    private String mName;

    public boolean isPlay() {
        return isPlay;
    }

    public void setPlay(boolean play) {
        isPlay = play;
    }

    private boolean isPlay = false;//声音是否在播放

    /**
     * SoundPool加载音频的优势:指令刚一发出,立即播放。
     * 前提:必须预先加载音频。SoundPool加载音频文件有自己的Integer类型ID
     */
    private Integer mSoundId;

    public Sound(String mAssetPath){
        this.mAssetPath=mAssetPath;
        /**
         * 拿到文件名
         * 1.用"/"分隔,得到数组
         * 2.文件名.文件类型是数组最后一位
         * 3.将“.文件类型”去掉就是文件名
         */
        String[] components = mAssetPath.split("/");
        String fileName = components[components.length - 1];
        mName=fileName.replace(".mp3","");
    }

    public String getmAssetPath() {
        return mAssetPath;
    }

    public String getmName() {
        return mName;
    }

    public Integer getSoundId() {
        return mSoundId;
    }

    public void setSoundId(Integer soundId) {
        mSoundId = soundId;
    }
}

 

 

 

3.    beatBox = new BeatBox(this, "voice2");  voice2资源文件的音频包

4.beatBox.play(beatBox.getSounds().get(0));

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值