1

我的问题有点难以描述。

我的项目(和 apk 文件)中有一个单独的资源文件夹。

String path = "/resources/instruments/data/bongo/audio/bong1.wav";  

我已经可以使用它了

url = StreamHelper.class.getClassLoader().getResource( path );
url.openStream();

但实际上我想将文件加载到 SoundPool 中。我试过这样:

SoundPool soundPool = new SoundPool(  5, AudioManager.STREAM_MUSIC, 0 );  
soundPool.load ( path, 1 );

...但我总是收到错误信息:“错误加载/资源...”

load(String path, int) 在这个链接上,我看到我需要正确的 File 路径

File file = new File( path );
if( file.exists() ) 
     //always false but i thing if it would be true soundPool.load should work too

现在我的问题是:它的工作路径如何。还是对我的问题有任何其他想法(与 AssetManager 一起使用)?

顺便提一句。我知道有一些特殊的 Android 方法可以获取像 R.id.View 这样的资源......但在我的情况下,这并不容易处理。

谢谢!

4

2 回答 2

5

就个人而言,我不认为 WAV 文件是“资源”,我建议您将它们放在“资产”文件夹中并使用您提到的 AssetManager。

这对我有用...

在您的项目中创建一个文件夹结构...

    /assets/instruments/data/bongo/audio

...然后将您的 bong1.wav 文件复制到那里。

使用以下内容加载它。注意:在提供 soundPool.load() 的路径时,请勿将“/”放在“乐器”前面...

    // Declare globally if needed
    int mySoundId;
    SoundPool soundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 0 );
    AssetManager am = this.getAssets();

    //Use in whatever method is used to load the sounds
    try {
        mySoundId = soundPool.load(am.openFd("instruments/data/bongo/audio/bong1.wav"), 1);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

用这个来玩...

    soundPool.play(mySoundId, 1, 1, 0, 0, 1);
于 2010-12-12T03:42:12.770 回答
1

它显然期望文件系统路径而不是类路径路径。

用来URL#getPath()获取它。

soundPool.load(url.getPath());
于 2010-12-12T03:42:18.217 回答