Question

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 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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top