【问题标题】:new File ( path ) always says that there is no filenew File ( path ) 总是说没有文件
【发布时间】:2011-05-24 03:27:12
【问题描述】:

我的问题有点难以描述。

我的项目(和 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 = 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 之类的资源……但就我而言,这并不容易处理。

谢谢!

【问题讨论】:

    标签: java android file path soundpool


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      使用URL#getPath()获取。

      soundPool.load(url.getPath());
      

      【讨论】:

      • 这在我的情况下不起作用。我不知道为什么,但仍然找不到资源。很遗憾,因为它会容易得多:)
      猜你喜欢
      • 1970-01-01
      • 2013-04-03
      • 2015-05-01
      • 2014-03-16
      • 2019-07-10
      • 2017-01-28
      • 1970-01-01
      • 1970-01-01
      • 2022-01-15
      相关资源
      最近更新 更多