新文件(路径)总是说没有文件

1 java android file path soundpool

我的问题描述有点棘手.

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

String path = "/resources/instruments/data/bongo/audio/bong1.wav";  
Run Code Online (Sandbox Code Playgroud)

我已经可以用了

url = StreamHelper.class.getClassLoader().getResource( path );
url.openStream();
Run Code Online (Sandbox Code Playgroud)

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

SoundPool soundPool = new SoundPool(  5, AudioManager.STREAM_MUSIC, 0 );  
soundPool.load ( path, 1 );
Run Code Online (Sandbox Code Playgroud)

...但我总是得到一个错误信息:"错误加载/资源..."

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
Run Code Online (Sandbox Code Playgroud)

现在我的问题是:它的运作方式如何.或者我的问题还有其他任何想法(使用AssetManager)?

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

谢谢!

Squ*_*onk 5

就个人而言,我没有看到WAV文件是"资源",我建议你把它们放在'assets'文件夹中,并按照你的提到使用AssetManager.

这对我有用......

在项目中创建文件夹结构......

    /assets/instruments/data/bongo/audio
Run Code Online (Sandbox Code Playgroud)

...然后在那里复制你的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();
    }
Run Code Online (Sandbox Code Playgroud)

用它来播放它......

    soundPool.play(mySoundId, 1, 1, 0, 0, 1);
Run Code Online (Sandbox Code Playgroud)