new File ( path ) 总是说没有文件

发布于 2024-10-07 04:48:44 字数 1025 浏览 3 评论 0原文

我的问题有点难以描述。

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

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

加载(字符串路径, 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)?

顺便提一句。我知道有特殊的 Android 方法来获取像 R.id.View 这样的资源...但就我而言,这并不容易处理。

谢谢!

my problem is a little bit tricky to describe.

I have a separated resources folder in my project (and in the apk-file).

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

I can already use it with

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

but actually I want to load the file into the SoundPool.
I tried it this way:

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

... but i always get an error information: "error loading /resourc..."

load(String path, int )
On this link I've seen that I need the correct path for File

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

Now my question: how has the path to be that it works. Or is there any other idea for my problem (p.e. with the AssetManager) ?

Btw. I know that there are special Android ways to get resources like R.id.View ... but in my case it would be not easy to handle.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

二手情话 2024-10-14 04:48:44

就我个人而言,我不将 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);

Personally, I don't see WAV files as 'resources' and I'd suggest you put them in the 'assets' folder and use AssetManager as you mentioned.

This works for me...

Create a folder structure in your project...

    /assets/instruments/data/bongo/audio

...then copy your bong1.wav file there.

Use the following to load it. NOTE: DO NOT put '/' in front of 'instruments' when supplying the path to 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();
    }

Use this to play it...

    soundPool.play(mySoundId, 1, 1, 0, 0, 1);
无悔心 2024-10-14 04:48:44

它显然需要文件系统路径而不是类路径路径。

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

soundPool.load(url.getPath());

It's apparently expecting a file system path rather than a classpath path.

Use URL#getPath() to obtain it.

soundPool.load(url.getPath());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文