在R.Raw中打开文件音频



我必须打开/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。我该怎么做?

以这种方式使用

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();
    }
}

最新更新