开发者

Android resource diectory name to string for WavFile usage

开发者 https://www.devze.com 2023-04-08 11:07 出处:网络
I am using the Wave tools downloaded from here: http://www.labbookpages.co.uk/audio/javaWavFiles.html

I am using the Wave tools downloaded from here: http://www.labbookpages.co.uk/audio/javaWavFiles.html

To begin to read a wav file in my res/raw directory I have done the following in a sub class which extends my main activity.

WavFile wavFile = WavFile.openWavFile(new File(context.getResources().openRawResource(R.raw.xsp11_10db).toString()));

Where context is defined as:

Context context;

The first line returns a NullPointerException, so it seems the type of class WaveFile (wavfile) returns null. Which brings 开发者_JAVA技巧to question the way I define the File string:

new File(context.getResources().openRawResource(R.raw.xsp11_10db).toString());

I know it is a crude way to obtain the resource wavfile "xsp11_10db", but I am unaware of any other methods to access the file. I tried with URI's but the directory was not found.

Does anyone know of a better way to read the wave file from resource, or what is causing my null pointer exception.

Any help would be appreciated, thankyou in advanced.


It looks like Wav API you are using does not support InputStream, and only accepts File as input. This is understandable, because most likely they use RandomAccessFile for implementation.

You probably thought that .openRawResource(...).toString() would give you file name, but that is not true. It will just call .toString() on InputStream returned from openRawResource(...). Which is not what you want.

The solution is to open InputStream using openRawResource() and copy data to a new file on SD card yourself. And then use that file for Wav API you are using.

0

精彩评论

暂无评论...
验证码 换一张
取 消