• Android音效SoundPool问题:soundpool 1 not retry


    Android音效SoundPool问题:soundpool 1 not retry


    今天开发中要用到SoundPool,遇到soundpool 1 not retry无法播放声音,MediaPlay可以


    后来经过一番研究,发现:

    出现soundpool 1 not retry问题的代码,无法播放声音

    mgr = (AudioManager) MainActivity.this.getSystemService(Context.AUDIO_SERVICE);
            //初始化soundPool 对象,第一个参数是允许有多少个声音流同时播放,第2个参数是声音类型,第三个参数是声音的品质
            soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
            soundPoolMap = new HashMap<Integer, Integer>();
            soundPoolMap.put(1, soundPool.load(MainActivity.this, R.raw.or, 1));
            soundPoolMap.put(2, soundPool.load(MainActivity.this, R.raw.sd, 1));
            volume = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);

    //loop:循环中的循环模式(0 =没有循环,-1 =无限循环)
                    soundPool.play(soundPoolMap.get(1), volume, volume, 1, 0, 1f);

    问题解决:这里的问题是soundPool.load(MainActivity.this, R.raw.or, 1),即 load() 声音文件后,立马 play() 播放,系统还没有准备好声音文件,所以才出了问题

    这里需要你:先在其他地方load()好了,比如在构造函数里先load()好了,在需要播放的地方再调用play(),也就是要过一段时间再调用play()

    这样写就没问题

    mgr = (AudioManager) MainActivity.this.getSystemService(Context.AUDIO_SERVICE);
            //初始化soundPool 对象,第一个参数是允许有多少个声音流同时播放,第2个参数是声音类型,第三个参数是声音的品质
            soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
            soundPoolMap = new HashMap<Integer, Integer>();
            soundPoolMap.put(1, soundPool.load(MainActivity.this, R.raw.or, 1));
            soundPoolMap.put(2, soundPool.load(MainActivity.this, R.raw.sd, 1));
            volume = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            soundPool.play(soundPoolMap.get(1), volume, volume, 1, 0, 1f);

  • 相关阅读:
    使用 kill 命令杀死 java进程,你用对了吗?
    脚本 启动/停止 jar包服务
    Zipkin和微服务链路跟踪
    nacos初探--作为配置中心
    第一次有人把“分布式事务”讲的这么简单明了
    SquishIt引起的HTTP Error 500.0
    imagesLoaded – 检测网页中的图片是否加载
    25个精美的创意机构和设计工作室网站案例
    使用 FocusPoint.js 实现图片的响应式裁剪
    设计师收藏的20款英文手写字体【免费下载】
  • 原文地址:https://www.cnblogs.com/snake-hand/p/3191926.html
Copyright © 2020-2023  润新知