微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决在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 举报,一经查实,本站将立刻删除。