2

wavを使用して、rawフォルダーにあるファイルのバイトからバッファーを読み取ろうとしていますInputStream

私が理解していないのは、ファイルの正しい場所を指定する方法だと思います。Rリソースのwavファイルはintであるため、次のことはできません。

InputStream is = new FileInputStream(R.raw.music);

intは承認されていないためですFileInputStream

基本的に私のコードは私が見つけたものの修正版です:

public void read(short[] musicin) {
    try {
        // Create a DataInputStream to read the audio data back from the saved file.
        InputStream is = new FileInputStream("R.raw.music");

        BufferedInputStream bis = new BufferedInputStream(is);

        DataInputStream dis = new DataInputStream(bis);

        int buffsize=512;

        // Read the file into the music array.
        int i = 0;

        while (i<buffsize&&dis.available() > 0) {
            musicin[i] = dis.readByte(); 
            i++;
        }

        // Close the input streams.
        dis.close();
    } catch (Throwable t) {
        Log.e("AudioTrack","Playback Failed");
    }
}

では、rawフォルダーからどのように読み取ることができますか?

4

3 に答える 3

1

あなたの活動クラスで

Context c;
c=this;
new yourClass(c);

あなたのクラスで

 public yourclass(Context context)
 {

InputStream in = context.getResources().openRawResource(R.raw.yourfilename); 
}
于 2012-10-23T13:07:49.720 に答える
1

さて、私がしたことは:

1-スレッドが開始する前に、これをアクティビティに追加します。

final Context con;
    con=this;

2-スレッドでクラスを呼び出す

new Wav_File_Reader();

. . .

Wav_File_Reader.read(musicin,con);

2-クラスを静的に変更します:

public static void read(short[] musicin, Context ctx ) {
    try {

    //  InputStream is = getBaseContext().getResources().openRawResource(R.raw.music);

    InputStream is = ctx.getResources().openRawResource(R.raw.music);
    BufferedInputStream bis = new BufferedInputStream(is);
    DataInputStream dis = new DataInputStream(bis);


    int buffsize=512;
    // Read the file into the music array.
    int i = 0;

    short in[]=new short[200000];

    //while (dis.available() > 0) {
    //while (i<buffsize&&dis.available() > 0) {
    while (i<200000&&dis.available() > 0) {
        //musicin[i] = dis.readByte(); 
        //in[i]=dis.readByte(); 
        in[i]=dis.readShort();
    i++;
    }

    // Close the input streams.
    dis.close(); 

    } catch (Throwable t) 
    {
    Log.e("AudioTrack","Playback Failed");
    }
    }

R.rawフォルダーから直接読み取ることができました。

迅速で便利なヘルプをありがとう!!!

于 2012-10-23T14:09:12.153 に答える
0

さて、InputStreamの実装は最初から目が離せないので、次のように変更します。

InputStream is = this.getResources().openRawResource(R.raw.music);
于 2012-10-23T13:08:15.930 に答える