在Android Studio项目中获取FileInputStream以读取WAV文件

问题描述

我在Android Studio项目中有一个单独的类,用于分析WAV文件。我最初在Eclipse中调试了文件,现在将其加载到Studio中。毫不奇怪,我在读取项目中的WAV文件时遇到了麻烦。首先,这是我项目中WAV文件的位置:

enter image description here

在MainActivity.class中,我调用函数WAVRead:

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

在实例化过程中调用以下FileInputStream:

      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,12);

此操作失败,NullPointerException引用了assetManager.openFd方法行。

   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

我的问题的一部分是我不确定我是否向项目提供了WAV文件的正确路径。但是,在我在这里看到的许多示例中,仅使用“ test.wav”就应该为assetManager提供正确的文件。有人可以建议一种使其识别文件的方法吗?

编辑:为了提供更多的上下文,我添加了以下代码来构造WAVFile类:

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

NPE中当前引用的第113行的错误:

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

最后,我如何在MainActivity.java中调用该方法(在NPE中也有引用:

WavFile j = new WavFile("test.wav",MainActivity.this);

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...