2

我一直在尝试wav使用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");
    }
}

那么我怎样才能从原始文件夹中读取呢?

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 回答