Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从资产加载和播放soundpool_Android_Soundpool - Fatal编程技术网

Android 从资产加载和播放soundpool

Android 从资产加载和播放soundpool,android,soundpool,Android,Soundpool,我有一堆声音,分配给一组按钮,我需要播放。我所有的声音都在资产文件夹中。但是,它不起作用目的是:从AssetFolder加载并播放声音。我将从我的项目中拿出代码示例: //set up audio player mSoundPool = new SoundPool(20, AudioManager.STREAM_MUSIC, 0); mAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);

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

//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();
    }
为了避免代码行过多,我创建了一个加载和播放声音的基本过程:

如您所见,我传递了文件(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);

我只是不想有这么多的代码行,我想让它看起来很漂亮

在使用

将您的
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)之后需要等待调用。您也可以看到示例不幸的是,当用户单击按钮时,我不能有任何延迟。它必须立即播放。有什么办法可以实现吗?实际上,我可以通过使用R.raw使其立即播放,但我想从资产中执行。然后在移动
mSoundPool.load(aMan.openFd(strSound),1)后尝试内部活动onCreate方法和
如果(已加载){mSoundPool.play(stream,streamVolume,streamVolume,1,LOOP_1_TIME,1f);}
活动恢复中的行我有大约20多个声音需要播放,它们都在一个活动中,所以看不到有其他线程的意义。让他们从资产文件夹加载将使我的工作更轻松,当我将创建一个声音列表。。。(谢谢)
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);
    }
}