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 );

...しかし、常にエラー情報が表示されます:「/リソースの読み込み中にエラーが発生しました...」

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 と一緒に) ?

ところで。R.id.View のようなリソースを取得する Android の特別な方法があることは知っていますが、私の場合は扱いが簡単ではありません。

ありがとう!

4

2 に答える 2

5

個人的には、WAV ファイルを「リソース」として認識していないので、「assets」フォルダに入れて、ご指摘のように AssetManager を使用することをお勧めします。

これは私のために働く...

プロジェクトにフォルダー構造を作成します...

    /assets/instruments/data/bongo/audio

...次に、bong1.wav ファイルをそこにコピーします。

以下を使用してロードします。注: soundPool.load() へのパスを指定するときは、「instruments」の前に「/」を置かないでください...

    // 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);
于 2010-12-12T03:42:12.770 に答える
1

クラスパス パスではなく、ファイル システム パスを期待しているようです。

を使用URL#getPath()して取得します。

soundPool.load(url.getPath());
于 2010-12-12T03:42:18.217 に答える