【问题标题】:Loading and playing with soundpool from assets从资产加载和播放 soundpool
【发布时间】:2012-12-09 05:56:47
【问题描述】:

我有一堆声音,分配给一组按钮,我需要播放它们。我所有的声音都在资产文件夹中。但是,它不起作用。 目的是: 从assetFodler 加载并播放声音。我将从我的项目中拿出代码示例:

//set up  audio player
    mSoundPool = new SoundPool(20, AudioManager.STREAM_MUSIC, 0);
    mAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
    streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
    streamVolume = streamVolume / mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);

 //getting files lists from asset folder
    aMan = this.getAssets();
    try {
        filelist = aMan.list("");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

为了没有太多的代码行,我创建了一个加载播放声音的基本程序:

public void loadSound (String strSound, int stream) {

    try {
        stream= mSoundPool.load(aMan.openFd(strSound), 1);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
     mSoundPool.play(stream, streamVolume, streamVolume, 1, LOOP_1_TIME, 1f);
}

如你所见,我传递了 file(stringName) 和 streamID。

最后,这是我的使用方法:

     case R.id.button1:
        //if button was clicked two or more times, when play is still on im doing stop
    mSoundPool.stop(mStream1);
    loadSound(filelist[0],mStream1);
        break;

当我运行项目时,什么都没有发生,logcat 说:

12-09 10:38:34.851: W/SoundPool(17331):   sample 2 not READY

任何帮助将不胜感激。

UPD1: 当我这样做时,没有 loadSound 程序它可以正常工作 以下代码是onCreate:

//load fx
    try {
        mSoundPoolMap.put(RAW_1_1, mSoundPool.load(aMan.openFd(filelist[0]), 1));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

和 Onclick 按钮:

  //resourcePlayer.stop();
        mSoundPool.stop(mStream1);
        mStream1= mSoundPool.play(mSoundPoolMap.get(RAW_1_1), streamVolume, streamVolume, 1, LOOP_1_TIME, 1f);

我只是不想有那么多代码行,我想让它看起来不错

【问题讨论】:

    标签: android soundpool


    【解决方案1】:

    在使用SoundPool.setOnLoadCompleteListener播放之前,您需要检查文件是否加载成功

    将您的loadSound 方法代码更改为:

    public void loadSound (String strSound, int stream) {
         boolean loaded = false;
         mSoundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
                @Override
                public void onLoadComplete(SoundPool soundPool, int sampleId,
                        int status) {
                    loaded = true;
                }
            });
        try {
              stream= mSoundPool.load(aMan.openFd(strSound), 1);
            } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
       // Is the sound loaded already?
       if (loaded) {
         mSoundPool.play(stream, streamVolume, streamVolume, 1, LOOP_1_TIME, 1f);
        }
    }
    

    【讨论】:

    • 好吧,什么都没有发生。似乎没有加载...为什么?
    • @Daler :因为在你当前的代码中所有的事情都是按顺序执行的,所以在mSoundPool.load(aMan.openFd(strSound), 1); call 之后等待。你也可以看到vogella.com/blog/2011/06/27/… 例子
    • 不幸的是,当用户单击按钮时,我不能有任何延迟。它必须立即播放。关于如何实现它的任何想法?实际上我可以通过使用 R.raw 让它立即播放,但我想从资产中做到这一点..
    • 然后在活动的onCreate方法中移动mSoundPool.load(aMan.openFd(strSound), 1); 和Activity的onResume中的if (loaded) { mSoundPool.play(stream, streamVolume, streamVolume, 1, LOOP_1_TIME, 1f); }行后尝试
    • 我有大约 20 多个必须播放的声音,而且它们都在一个活动中,所以看不出有另一个线程的意义。让它们从资产文件夹中加载会让我的工作更轻松,当我将创建一个声音列表时......谢谢)
    猜你喜欢
    • 2022-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-06
    相关资源
    最近更新 更多