4

Androidからサウンドを読み込もうとしています。音は下res/raw/myownsound.wavです。私はすでに以下を使用してサウンドをロードできることを知っています:

 soundPool.load(context, R.raw.myownsound, 1)

カスタマイズの目的で、次を使用してロードしたいと思います。

 soundPool.load("res/raw/myownsound", 1)

...しかし、次のエラーが表示されますerror loading res/raw/myownsound。私も次のことを試しました:

 soundPool.loadSound("android.resource://upg.GraphismeBase/raw/myownsound", 1)

..しかし、エラーも発生します:error loading android.resource://upg.GraphismeBase/raw/myownsound

soundPool.load(path, priority) を使用する正しい方法は何ですか?

4

2 に答える 2

6

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

/assets/sounds/data/

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

// 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("sounds/data/myownsound"), 1);
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

これを試すことができます(動作するかどうかはわかりません):

SoundPool mySoundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);
    int myAudioFile = getResId("claps", R.raw.class);
    try{
        mySoundPool.load(context.getActivity(),myAudioFile,1);
    } catch (Exception e){
        message = String.valueOf(e);
    }

public static int getResId(String variableName, Class<?> c) {
    Field field = null;
    int resId = 0;
    try {
        field = c.getField(variableName);
        try {
            resId = field.getInt(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return resId;
}
于 2012-08-23T14:55:16.017 に答える
5

という名前のコンテキストを使用してこれを行う簡単な作業方法mContext。実行時に識別子 id を取得して、リソースを名前でロードします。

int sound_id = mContext.getResources().getIdentifier("myownsound", "raw",
                                                     mContext.getPackageName());
soundPool.load(mContext, sound_id, 1);

また、またはなどに置き換え"raw"て、ドローアブルまたは xml ファイルをロードすることもできます。"drawable""xml"

于 2012-09-03T21:52:28.783 に答える