pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

我如何知道 Soundpool 已準備好使用低于 2.2 的 SD

How do I know that the Soundpool is ready using SDK target below 2.2?(我如何知道 Soundpool 已準備好使用低于 2.2 的 SDK 目標?)
本文介紹了我如何知道 Soundpool 已準備好使用低于 2.2 的 SDK 目標?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

這個問題與 這個問題有關.

我設法修改我的代碼以使用 SoundPool 而不是 AudioManager.知道它或多或少有效.

I managed to modify my code to use SoundPool instead of AudioManager. Know it works more or less.

public class Sound {

    private static boolean sound = true;

    private static final int EAT_SOUND = 1;
    private static final int SHORT_EAT_SOUND = 2;
    private static final int EAT_CHERRY_SOUND = 3;
    private static final int EAT_GHOST_SOUND = 4;
    private static final int EXTRA_LIVE_SOUND = 5;
    private static final int INTERMISSION_SOUND = 6;
    private static final int OPENING_SOUND = 7;
    private static final int PACMAN_DIES_SOUND = 8;
    private static final int SIREN_SOUND = 9;

    private static Context context;
    private static SoundPool soundPool;
    private static HashMap<Integer, Integer> soundPoolMap;

    public static void initializeOpenSound(Context con) {
        context = con;
        soundPool = new SoundPool(2, AudioManager.STREAM_RING, 100);
        soundPoolMap = new HashMap<Integer, Integer>();
        soundPoolMap.put(OPENING_SOUND, soundPool.load(context, R.raw.opening_song, 1));
    }

    public static void initializeSounds() {
        soundPoolMap.put(EAT_SOUND, soundPool.load(context, R.raw.eating, 1));
        soundPoolMap.put(SHORT_EAT_SOUND, soundPool.load(context, R.raw.eating_short, 1));
        soundPoolMap.put(EAT_CHERRY_SOUND, soundPool.load(context, R.raw.eating_cherry, 1));
        soundPoolMap.put(EAT_GHOST_SOUND, soundPool.load(context, R.raw.eating_ghoasts, 1));
        soundPoolMap.put(EXTRA_LIVE_SOUND, soundPool.load(context, R.raw.extra_lives, 1));
        soundPoolMap.put(INTERMISSION_SOUND, soundPool.load(context, R.raw.intermission, 1));
        soundPoolMap.put(PACMAN_DIES_SOUND, soundPool.load(context, R.raw.pac_man_dies, 1));
        soundPoolMap.put(SIREN_SOUND, soundPool.load(context, R.raw.siren, 1));
    }

    private static void playSound(int sound) {
        int streamID = soundPool.play(soundPoolMap.get(sound), 0.5f, 0.5f, 1, 0, 1f);
        if (sound == OPENING_SOUND) {
            soundPool.setLoop(streamID, 2);
        }
    }

    public static void playSirenSound() throws SoundInitializationError {
        if (isSoundOn()) {
            if (soundPoolMap.containsKey(SIREN_SOUND)) {
                System.out.println("Play Siren sound");
                playSound(SIREN_SOUND);
            } else {
                throw new SoundInitializationError("Siren Sound not initialized!");
            }
        }
    }

    public static void playPacmanDiesSound() throws SoundInitializationError {
        if (isSoundOn()) {
            if (soundPoolMap.containsKey(PACMAN_DIES_SOUND)) {
                System.out.println("Play Pacman Dies sound");
                playSound(PACMAN_DIES_SOUND);
            } else {
                throw new SoundInitializationError("Pacman Dies Sound not initialized!");
            }
        }
    }

    public static void playOpeningSound() throws SoundInitializationError {
        if (isSoundOn()) {
            if (soundPoolMap.containsKey(OPENING_SOUND)) {
                System.out.println("Play Opening sound");
                playSound(OPENING_SOUND);
            } else {
                throw new SoundInitializationError("Opening Sound not initialized!");
            }
        }
    }

    public static void playIntermissionSound() throws SoundInitializationError {
        if (isSoundOn()) {
            if (soundPoolMap.containsKey(INTERMISSION_SOUND)) {
                System.out.println("Play Intermission sound");
                playSound(INTERMISSION_SOUND);
            } else {
                throw new SoundInitializationError("Intermission Sound not initialized!");
            }
        }
    }

    public static void playExtraLiveSound() throws SoundInitializationError {
        if (isSoundOn()) {
            if (soundPoolMap.containsKey(EXTRA_LIVE_SOUND)) {
                System.out.println("Play Extra Live sound");
                playSound(EXTRA_LIVE_SOUND);
            } else {
                throw new SoundInitializationError("Extra Live Sound not initialized!");
            }
        }
    }

    public static void playEatSound() throws SoundInitializationError {
        if (isSoundOn()) {
            if (soundPoolMap.containsKey(EAT_SOUND)) {
                System.out.println("Play Eat Sound");
                playSound(EAT_SOUND);
            } else {
                throw new SoundInitializationError("Eat Sound not initialized!");
            }
        }
    }

    public static void playShortEatSound() throws SoundInitializationError {
        if (isSoundOn()) {
            if (soundPoolMap.containsKey(SHORT_EAT_SOUND)) {
                System.out.println("Play Short Eat sound");
                playSound(SHORT_EAT_SOUND);
            } else {
                throw new SoundInitializationError("Short Eat Sound not initialized!");
            }
        }
    }

    public static void playEatCherrySound() throws SoundInitializationError {
        if (isSoundOn()) {
            if (soundPoolMap.containsKey(EAT_CHERRY_SOUND)) {
                System.out.println("Play Eat Cherry sound");
                playSound(EAT_CHERRY_SOUND);
            }  else {
                throw new SoundInitializationError("Eat Cherry Sound not initialized!");
            }
        }
    }

    public static void playEatGhostSound() throws SoundInitializationError {
        if (isSoundOn()) {
            if (soundPoolMap.containsKey(EAT_GHOST_SOUND)) {
                System.out.println("Play Eat Ghost sound");
                playSound(EAT_GHOST_SOUND);
            }  else {
                throw new SoundInitializationError("Eat Ghost Sound not initialized!");
            }
        }
    }

    public static boolean isSoundOn() {
        return sound;
    }

    public static void setSoundOn(boolean b) {
        sound = b;
    }

}

但是,我有一個或多或少的大音頻文件,我想在啟動期間播放.但是,如果我調用 initializeOpenSound() 然后 playOpening Sound(),則不會播放聲音并且 logcat 輸出:

However, I have a more or less big audio file which I want to play during startup. However if i call initializeOpenSound() and then playOpening Sound(), then the sound isn't played and logcat outputs:

07-15 09:11:02.121: WARN/SoundPool(14273):   sample 1 not READY

在 SDK 2.2 中,我將使用 SoundPool.OnLoadCompleteListener但是如何使用 SDK 2.1 實現相同的目標?

In SDK 2.2 I would use SoundPool.OnLoadCompleteListener but how can I achieve the same using SDK 2.1?

我也很樂意提供一個不包含監聽器或類似東西的解決方案,但要么嘗試播放聲音文件直到它成功.

I would also be happy width a solution that doesn't include a listener or something like that, but either try to play the sound file until it was successful.

推薦答案

根據我的閱讀,如果您在初始化后立即嘗試播放聲音,它將無法準備好,您將收到此錯誤.您可以制作一個加載頁面,然后等待幾秒鐘,直到您確定聲音已加載.

From what I have read if you try to play the sound right after initializing it, it won't be ready and you will get this error. You could make a loading page and just wait for a few seconds until you are sure the sound has loaded.

還要確保您的文件大小不超過 1048576 字節,這就是 Soundpool 的限制.如果你想播放背景音樂或其他東西,我認為你必須使用媒體播放器.

Also make sure your filesize isn't over 1048576 bytes, thats the limitation for Soundpool. If you want to play background music or something i think you have to use media player.

這篇關于我如何知道 Soundpool 已準備好使用低于 2.2 的 SDK 目標?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Why would you choose Android API over Google APIs in the SDK on Eclipse?(為什么在 Eclipse 的 SDK 中選擇 Android API 而不是 Google API?)
Couchbase Bucket authentication error(Couchbase 存儲桶身份驗證錯誤)
admob 6.2.1 nullpointer exception(admob 6.2.1 空指針異常)
How to setup SDK in IntelliJ IDEA?(如何在 IntelliJ IDEA 中設置 SDK?)
My phone cannot be detected in eclipse to test run(eclipse 無法檢測到我的手機進行試運行)
platform-toolsaapt.exe directory missing in android SDK(android SDK 中缺少 platform-toolsaapt.exe 目錄)
主站蜘蛛池模板: 湖南自考_湖南自学考试网 | 膜结构停车棚-自行车棚-膜结构汽车棚加工安装厂家幸福膜结构 | 智慧物联网行业一站式解决方案提供商-北京东成基业 | 电杆荷载挠度测试仪-电杆荷载位移-管桩测试仪-北京绿野创能机电设备有限公司 | 上海瑶恒实业有限公司|消防泵泵|离心泵|官网 | 临时厕所租赁_玻璃钢厕所租赁_蹲式|坐式厕所出租-北京慧海通 | 锡膏喷印机-全自动涂覆机厂家-全自动点胶机-视觉点胶机-深圳市博明智控科技有限公司 | 首页|专注深圳注册公司,代理记账报税,注册商标代理,工商变更,企业400电话等企业一站式服务-慧用心 | 防爆大气采样器-防爆粉尘采样器-金属粉尘及其化合物采样器-首页|盐城银河科技有限公司 | 活性氧化铝球|氧化铝干燥剂|分子筛干燥剂|氢氧化铝粉-淄博同心材料有限公司 | 宁波普瑞思邻苯二甲酸盐检测仪,ROHS2.0检测设备,ROHS2.0测试仪厂家 | 医疗仪器模块 健康一体机 多参数监护仪 智慧医疗仪器方案定制 血氧监护 心电监护 -朗锐慧康 | 污水处理设备维修_污水处理工程改造_机械格栅_过滤设备_气浮设备_刮吸泥机_污泥浓缩罐_污水处理设备_污水处理工程-北京龙泉新禹科技有限公司 | 南京泽朗生物科技有限公司| 土壤有机碳消解器-石油|表层油类分析采水器-青岛溯源环保设备有限公司 | 合肥升降机-合肥升降货梯-安徽升降平台「厂家直销」-安徽鼎升自动化科技有限公司 | 临沂招聘网_人才市场_招聘信息_求职招聘找工作请认准【马头商标】 | pos机办理,智能/扫码/二维码/微信支付宝pos机-北京万汇通宝商贸有限公司 | FFU_空气初效|中效|高效过滤器_空调过滤网-广州梓净净化设备有限公司 | 贴片电容代理-三星电容-村田电容-风华电容-国巨电容-深圳市昂洋科技有限公司 | 【MBA备考网】-2024年工商管理硕士MBA院校/报考条件/培训/考试科目/提前面试/考试/学费-MBA备考网 | 杜甫仪器官网|实验室平行反应器|升降水浴锅|台式低温循环泵 | 手板-手板模型-手板厂-手板加工-生产厂家,[东莞创域模型] | 找果网 | 苹果手机找回方法,苹果iPhone手机丢了找回,认准找果网! | 低合金板|安阳低合金板|河南低合金板|高强度板|桥梁板_安阳润兴 北京租车牌|京牌指标租赁|小客车指标出租 | 山东彩钢板房,山东彩钢活动房,临沂彩钢房-临沂市贵通钢结构工程有限公司 | 企业微信scrm管理系统_客户关系管理平台_私域流量运营工具_CRM、ERP、OA软件-腾辉网络 | 精密模具制造,注塑加工,吹塑和吹瓶加工,EPS泡沫包装生产 - 济南兴田塑胶有限公司 | 硅胶布|电磁炉垫片|特氟龙胶带-江苏浩天复合材料有限公司 | 切铝机-数控切割机-型材切割机-铝型材切割机-【昆山邓氏精密机械有限公司】 | 管家婆-管家婆软件-管家婆辉煌-管家婆进销存-管家婆工贸ERP | SDG吸附剂,SDG酸气吸附剂,干式酸性气体吸收剂生产厂家,超过20年生产使用经验。 - 富莱尔环保设备公司(原名天津市武清县环保设备厂) | 炉门刀边腹板,焦化设备配件,焦化焦炉设备_沧州瑞创机械制造有限公司 | 拼装地板,悬浮地板厂家,悬浮式拼装运动地板-石家庄博超地板科技有限公司 | 球磨机,节能球磨机价格,水泥球磨机厂家,粉煤灰球磨机-吉宏机械制造有限公司 | 万濠影像仪(万濠投影仪)百科-苏州林泽仪器 | 液压油缸-液压缸厂家价格,液压站系统-山东国立液压制造有限公司 液压油缸生产厂家-山东液压站-济南捷兴液压机电设备有限公司 | 时代北利离心机,实验室离心机,医用离心机,低速离心机DT5-2,美国SKC采样泵-上海京工实业有限公司 工业电炉,台车式电炉_厂家-淄博申华工业电炉有限公司 | 动力配电箱-不锈钢配电箱-高压开关柜-重庆宇轩机电设备有限公司 聚天冬氨酸,亚氨基二琥珀酸四钠,PASP,IDS - 远联化工 | 档案密集柜_手动密集柜_智能密集柜_内蒙古档案密集柜-盛隆柜业内蒙古密集柜直销中心 | 有机肥设备生产制造厂家,BB掺混肥搅拌机、复合肥设备生产线,有机肥料全部加工设备多少钱,对辊挤压造粒机,有机肥造粒设备 -- 郑州程翔重工机械有限公司 |