2010-12-12 35 views
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); 

...但我总是得到一个错误信息:“错误加载/ resourc ......”

load(String path, int) 在这个环节我见过,我需要正确的路径文件

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

现在我的问题:它的路径是如何工作的。还是有任何其他的想法,我的问题(与AssetManager p..)?

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

谢谢!

回答

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

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

使用URL#getPath()来获得它。

soundPool.load(url.getPath()); 
+0

这在我的情况下不起作用。我不知道为什么,但资源仍然没有找到。这太遗憾了,因为它会容易得多:) – 2010-12-19 14:35:50