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

时间:2010-12-12 03:10:29

标签: java android file path soundpool

我的问题描述起来有点棘手。

我的项目(以及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 );

...但我总是收到错误信息:“loading loading / resourc ...”

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这样的资源......但在我的情况下,它并不容易处理。

谢谢!

2 个答案:

答案 0 :(得分:5)

就个人而言,我没有将WAV文件视为“资源”,我建议您将它们放在'assets'文件夹中并使用您提到的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);

答案 1 :(得分:1)

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

使用URL#getPath()获取它。

soundPool.load(url.getPath());
相关问题