Getting a FileInputStream to work reading in a WAV file in Android Studio project

0

I have a separate class in an Android Studio project that analyzes WAV files. I debugged the file originally in Eclipse and now have loaded it into Studio. Unsurprisingly, I'm having trouble reading in a WAV file in my project. First, this is the location of the WAV file in my project:

enter image description here

In my MainActivity.class, I call the function WAVRead:

 WavFile j = new WavFile("test.wav");

Which invokes the following FileInputStream as part of the instantiation:

      try {
        // Create a new file input stream for reading file data

        AssetFileDescriptor fileDescriptor = assetManager.openFd(filename);
        FileInputStream iStream = fileDescriptor.createInputStream();

        // Read the first 12 bytes of the file
        int bytesRead = iStream.read(innerBuffer, 0, 12);

This fails, with a NullPointerException refering to the assetManager.openFd method line.

   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.AssetFileDescriptor android.content.res.AssetManager.openFd(java.lang.String)' on a null object reference

Part of my problem is I'm not so sure if I'm feeding the project the right path to the WAV file. Yet, in many of the examples I've seen here, just utilizing "test.wav" should feed the assetManager the correct file. Can someone suggest a way to get it to recognize the file?

EDIT: To provide more context, I'm adding the following code that constructs the WAVFile class:

public class WavFile {

private enum IOState {READING, WRITING, CLOSED}
private final static int BUFFER_SIZE = 4096;

private final static int FMT_CHUNK_ID = 0x20746D66;
private final static int DATA_CHUNK_ID = 0x61746164;
private final static int RIFF_CHUNK_ID = 0x46464952;
private final static int RIFF_TYPE_ID = 0x45564157;

private File file;                 // File that will be read from or written to
private IOState ioState;           // Specifies the IO State of the Wav File (used for sanity
                                   // checking)
private int bytesPerSample;        // Number of bytes required to store a single sample
private long numFrames;            // Number of frames within the data section
private FileOutputStream oStream;  // Output stream used for writing data
private FileInputStream iStream;   // Input stream used for reading data
private double floatScale;         // Scaling factor used for int <-> float conversion
private double floatOffset;        // Offset factor used for int <-> float conversion
private boolean wordAlignAdjust;   // Specify if an extra byte at the end of the data chunk is
                                   // required for word alignment

// Wav Header
private int numChannels;           // 2 bytes unsigned, 0x0001 (1) to 0xFFFF (65,535)
private long sampleRate;           // 4 bytes unsigned, 0x00000001 (1) to 0xFFFFFFFF
                                   // (4,294,967,295)
                                   // Although a java int is 4 bytes, it is signed, so need to
                                   // use a long
private int blockAlign;            // 2 bytes unsigned, 0x0001 (1) to 0xFFFF (65,535)
private int validBits;             // 2 bytes unsigned, 0x0002 (2) to 0xFFFF (65,535)

// Buffering
private byte[] innerBuffer;        // Local innerBuffer used for IO
private int bufferPointer;         // Points to the current position in innerBuffer
private int bytesRead;             // Bytes read after last read into innerBuffer
private long frameCounter;         // Current number of frames read or written
private Context context;


/**
 * This constructor should not be called directly.
 */
private WavFile() {
    innerBuffer = new byte[BUFFER_SIZE];
}

/**
 * Constructor for creating a WavFile object to be read from a file.
 * @param filename The filename of the file to be read into this object
 */
public WavFile(String filename, Context context) {
    this();
    this.context=context;
    AssetManager assetManager = this.context.getAssets();

The error on Line 113 that is currently cited in an NPE:

    if(this.file.length()!=chunkSize+8) {
        throw new RuntimeException("Header chunk size (" + chunkSize +
                ") does not match file size (" + this.file.length() + ")");
    }

And finally, how I am calling the method in MainActivity.java (which is also cited in the NPE:

WavFile j = new WavFile("test.wav", MainActivity.this);
java
android
android-studio
fileinputstream
asked on Stack Overflow Sep 18, 2020 by wnjl • edited Sep 18, 2020 by wnjl

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0