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);

Android音效SoundPool问题:soundpool 1 not retry的更多相关文章

  1. Android音频播放之SoundPool

    SoundPool 一.基本概念 在Android应用程序的开发过程中,经常需要播放多媒体文件,也许最先想到的会是MediaPlayer类了,该类提供了播放.暂停.停止及重复播放等功能性方法(该类位于 ...

  2. Android音频播放之SoundPool 详解

    SoundPool —— 适合短促且对反应速度比较高的情况(游戏音效或按键声等) 下面介绍SoundPool的创建过程: 1. 创建一个SoundPool (构造函数) public SoundPoo ...

  3. Android简单的利用SoundPool进行播放铃声的实例代码

    MainActivity.java package com.example.pengdonglin.soundpool_demo; import android.annotation.Suppress ...

  4. 修复android 5.0 Xutils的框架问题retry error, curr request is null

    Android 5.0手机对xUtils-2.6.13.jar请求时会出现retry error, curr request is null 情况, 修复解决方式: 找到library/src/com ...

  5. Android 使用SoundPool播放音效

    在Android开发中我们经常使用MediaPlayer来播放音频文件,但是MediaPlayer存在一些不足,例如:资源占用量较高.延迟时间较长.不支持多个音频同时播放等.这些缺点决定了MediaP ...

  6. android SoundPool播放音效

    MediaPlayer的缺点: 资源占用量高,延时时间较长 不支持多个音效同一时候播放 SoundPool主要用于播放一些较短的声音片段,CPU资源占用率低和反应延时小,还支持自行色设置声音的品质,音 ...

  7. Android MediaPlayer播放一般音频与SoundPool播放短促的音效

    [1]使用MediaPlayer实现一般的音频播放 MediaPlayer播放通常的音频文件 MediaPlayer mediaPlayer = new MediaPlayer(); if (medi ...

  8. Android SoundPool.play方法的音量与系统音量的关系

    Android中播放音频,除了使用MediaPlayer,还可以使用SoundPool.SoundPool使用音效池的概念来管理多个短促的音效,对于需要短促的提示音的场景特别合适. 通常调用Sound ...

  9. Android中的音频处理------SoundPool,MediaRecorder,MediaPlayer以及RingStone总结

    用Soundpool可以播一些短的反应速度要求高的声音, 比如游戏中的爆破声, 而Mediaplayer适合播放长点的. MediaRecorder主要用来录音. SoundPool载入音乐文件使用了 ...

随机推荐

  1. linux(vi)常用命令

    常用操作 系统命令 查看主机名 hostname 修改主机名(重启后无效) hostname yang 修改主机名(重启后永久生效) vi /ect/sysconfig/network 修改IP(重启 ...

  2. tf.metrics.accuracy ==>坑货

    tf.metrics.accuracy输出两个值,第一个值为上几步的平均精度,第二值是上几步与该步的精度的平均值. 正常的计算单个batch正确率的代码 self.correct_prediction ...

  3. Codeforces 981D Bookshelves(按位贪心+二维DP)

    题目链接:http://codeforces.com/contest/981/problem/D 题目大意:给你n本书以及每本书的价值,现在让你把n本书放到k个书架上(只有连续的几本书可以放到一个书架 ...

  4. 提高eclipse使用效率(二)—— 提高Android开发效率的小技巧

    XML文件的代码提示 adt中也有xml文件的代码提示,为了让提示来的更加猛烈,我们还要设置一下 打开eclipse - Window - Preferences,在右边的目录树中切换到XML - X ...

  5. 微信接口问题(The underlying connection was closed: An unexpected error occurred on a send)

    突然在调用微信接口是报:The underlying connection was closed: An unexpected error occurred on a send错误,跟踪了半天,是因为 ...

  6. day7 面向对象class()学习

        面向过程 VS 面向对象     编程范式 编程是程序员用特定的语法+数据结构+算法组成的代码来告诉计算机如何执行任务的过程,一个程序是程序员为了得到一个任务结果而编写的一组指令的集合,正所谓 ...

  7. umount /dev/shm

    [root@test ~]# umount /dev/shm umount: /dev/shm: device is busy.        (In some cases useful info a ...

  8. 005 Hadoop的三种模式区别

    1.本地模式 -默认模式. -不对配置文件进行修改. -使用本地文件系统,而不是分布式文件系统. -Hadoop不会启动NameNode.DataNode.ResourceManager.NodeMa ...

  9. JS图片滚动代码(无缝、平滑)

    非常平滑的JS图片滚动特效代码,无缝循环,速度可自定义,鼠标悬停时停止.它的特点是JS和图片地址分离,这样做你就经易的从数据库动态调用每张图片的地址,方便控制,因此它非常的应用. <!DOCTY ...

  10. Ionic实战三:Ionic 图片预览可放大缩小左右滑动demo-iClub图片预览

    这个demo的主要功能有两个,一个是首页的导航向上拉动会浮动在最上面的效果,另一个就是我们平时非常实用的功能,就是图片预览功能 点击可以放大图片,并且可以左右滑动,还可以双击放大缩小图片以及双手指控制 ...