在R.Raw中打开文件音频

时间:2015-02-16 13:23:46

标签: android audio

我必须在/res/raw/文件夹中打开一个文件,但似乎android,并不能识别路径。

这是我的代码:

public static void openRec()
{
    //this is the wav file that I have to analyze
    File file = new File("/res/raw/chirp.wav");

    try {
       FileInputStream in = new FileInputStream(file);
       chirp = new byte[(int) file.length()];
       in.read(chirp);


        Log.d("xxx", "" + chirp.length);
        in.close();


    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

编辑: 我有另一个方法openRec,其中路径作为参数传递:

public static void openRec(String path) {

       File file = new File(path);
       try {
           FileInputStream in = new FileInputStream(file);
           recording = new byte[(int) file.length()];
           in.read(recording);
           Log.d("xxx", "" + recording.length);
           in.close();
       } catch (FileNotFoundException e) {
           e.printStackTrace();
       } catch (IOException e) {
           e.printStackTrace();
       }

}

我会使用/ res / raw中的ffile执行相同的操作。我怎么做?

1 个答案:

答案 0 :(得分:2)

以这种方式使用

public static void openRec()
{
    //this is the wav file that I have to analyze


    try {
       InputStream in = getResources().openRawResource(R.raw.chirp);
       chirp = new byte[(int) file.length()];
       in.read(chirp);


        Log.d("xxx", "" + chirp.length);
        in.close();


    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}