有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

安卓 java。lang.UnsatifiedLinkError:未找到本机方法:创建

您好,我正在将wav数据编码到安卓中的ogg vorbis中。我正在为Xiph中的libogg vorbis库使用一个简单的JNI包装器。组织。当我创建VorbisFileOutputStream的对象时,程序抛出exeception java.lang.UnsatisfiedLinkError: create

VorbiFileOutputStream。爪哇

public class VorbisFileOutputStream extends AudioOutputStream {
    // The index into native memory where the ogg stream info is stored.
    private final int oggStreamIdx;
    private VorbisInfo info;
    private static final int VORBIS_BLOCK_SIZE = 1024;

    static {
        System.loadLibrary("ogg");
        System.loadLibrary("vorbis");
        System.loadLibrary("vorbis-stream");
    }

    public VorbisFileOutputStream (String fname, VorbisInfo s) throws IOException {
        info = s;
        oggStreamIdx = this.create(fname, s);
    }
    public VorbisFileOutputStream (String fname) throws IOException {
        oggStreamIdx = this.create(fname, new VorbisInfo());
    }

    @Override
    public void close() throws IOException {
        this.closeStreamIdx(this.oggStreamIdx);
    }

    /**
     * Write PCM data to ogg.  This assumes that you pass your streams in interleaved.
     * @param buffer
     * @param offset
     * @param length
     * @return
     * @throws java.io.IOException
     */
    @Override
    public void write(final short [] buffer, int offset, int length) throws IOException {
        this.writeStreamIdx(this.oggStreamIdx, buffer, offset, length);
    }

    private native int writeStreamIdx(int idx, short [] pcmdata, int offset, int size) throws IOException;
    private native void closeStreamIdx(int idx) throws IOException;
    private native int create(String path, VorbisInfo s) throws IOException;
    @Override
    public int getSampleRate() {
        return info.sampleRate;
    }
}

主要活动。爪哇

try {
    vorbisFileOutputStream = new VorbisFileOutputStream(
            "/sdcard/demo.ogg");
} catch (IOException e) {
    e.printStackTrace();
}
while (isRecording) {
    // gets the voice output from microphone to byte format
    recorder.read(sData, 0, bufferSize / 2);
    System.out.println("Short writing to file" + sData.toString());
    try {
        // writes the data to file from buffer stores the voice buffer
        dataOutputStream.write(short2byte(sData));
        vorbisFileOutputStream.write(short2byte(sData));
    } catch (IOException e) {
        e.printStackTrace();
    }
}

错误日志:

04-15 16:04:00.515: E/AndroidRuntime(2299): FATAL EXCEPTION: AudioRecorder Thread
04-15 16:04:00.515: E/AndroidRuntime(2299): java.lang.UnsatisfiedLinkError: create
04-15 16:04:00.515: E/AndroidRuntime(2299):     at com.example.app.VorbisFileOutputStream.create(Native Method)
04-15 16:04:00.515: E/AndroidRuntime(2299):     at com.example.app.VorbisFileOutputStream.<init>(VorbisFileOutputStream.java:33)
04-15 16:04:00.515: E/AndroidRuntime(2299):     at com.example.app.MainActivity.writeAudioDataToFile(MainActivity.java:111)
04-15 16:04:00.515: E/AndroidRuntime(2299):     at com.example.app.MainActivity.access$0(MainActivity.java:93)
04-15 16:04:00.515: E/AndroidRuntime(2299):     at com.example.app.MainActivity$1.run(MainActivity.java:48)
04-15 16:04:00.515: E/AndroidRuntime(2299):     at java.lang.Thread.run(Thread.java:1019)

enter image description here

有什么想法吗


共 (1) 个答案

  1. # 1 楼答案

    首先包括。所以,通过使用这些代码,可以在类中创建文件

    static
    {
           System.loadLibrary("library_name");  // Left the 'lib' prefix from .so file
    }