【问题标题】:Getting input data from resource wav file in "raw" folder从“原始”文件夹中的资源 wav 文件获取输入数据
【发布时间】:2012-10-23 13:02:56
【问题描述】:

我一直在尝试使用InputStream 从位于原始文件夹中的wav 文件的字节中读取缓冲区。

我想我不明白的是如何给出文件的正确位置。 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");
    }
}

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

【问题讨论】:

标签: java android input io


【解决方案1】:

在你的活动课上

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

在你的课堂上

 public yourclass(Context context)
 {

InputStream in = context.getResources().openRawResource(R.raw.yourfilename); 
}

【讨论】:

  • 我试过了,但现在我有一个错误...“方法 getResources() 未定义 Wav_File_Reader 类型”–
  • getResouces 是一个上下文的方法。所以你需要使用上下文。您是在活动类中还是在外部调用此方法??
  • 我正在从活动中调用一个类并且活动使用一个线程
  • 然后将活动上下文传递给该类。
  • 谢谢,但因为我在线程内调用函数,所以不能使用“this”
【解决方案2】:

好吧,我所做的是:

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 文件夹中读取。

感谢快速有用的帮助!!!

【讨论】:

    【解决方案3】:

    好吧,InputStream 的实现从一开始就引人注目,把它改成这样:

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

    【讨论】:

    • 我试过了,但现在我有一个错误...“方法 getResources() 未定义 Wav_File_Reader 类型”
    • 好吧,在这种情况下,this 是一个 Context 类型,所以尝试将其更改为:InputStream is = getBaseContext().getResources().openRawResource(R.raw.music); 如果你在某个 Activity 类中调用它
    • getBaseContext() 也未定义。
    • 好吧,那么你需要从活动中传递上下文
    • 已经做到了,我能够让这个东西工作,非常感谢你非常有帮助
    猜你喜欢
    • 1970-01-01
    • 2016-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多