我通过浏览samsung Porting and using LAME MP3 on Android with JNI
中的文档创建了一个现有的lame编码器然后我遇到了本机库的一些问题我用stackoverflow中的另一个问题解决了它this question
现在我得到了.so文件,但是当我运行它时抛出以下错误A / libc:致命信号11(SIGSEGV)在0x00000004(代码= 1),线程22791 A / libc:致命信号11(SIGSEGV)位于0x00000004(代码= 1),线程22791
我认为这是由于某些同步问题... 这是我的MainActivity。
package com.lameencoder;
public class MainActivity extends Activity {
static {
System.loadLibrary("mp3lame");
}
private native void initEncoder(int numChannels, int sampleRate, int bitRate, int mode, int quality);
private native void destroyEncoder();
private native int encodeFile(String sourcePath, String targetPath);
public static final int NUM_CHANNELS = 1;
public static final int SAMPLE_RATE = 16000;
public static final int BITRATE = 128;
public static final int MODE = 1;
public static final int QUALITY = 2;
private AudioRecord mRecorder;
private short[] mBuffer;
private final String startRecordingLabel = "Start recording";
private final String stopRecordingLabel = "Stop recording";
private boolean mIsRecording = false;
private File mRawFile;
private File mEncodedFile;
private Button startRecording,stopRecording;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initRecorder();
initEncoder(NUM_CHANNELS, SAMPLE_RATE, BITRATE, MODE, QUALITY);
startRecording = (Button) findViewById(R.id.button);
startRecording.setText(startRecordingLabel);
startRecording.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
if (!mIsRecording) {
mIsRecording = true;
try{
mRecorder.startRecording();
mRawFile = getFile("raw");
}catch(Exception e){
e.printStackTrace();
}
startBufferedWrite(mRawFile);
}
}
});
stopRecording =(Button) findViewById(R.id.button2);
stopRecording.setText(stopRecordingLabel);
stopRecording.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mIsRecording){
mIsRecording = false;
try {
mRecorder.stop();
mEncodedFile = getFile("mp3");
int result = encodeFile(mRawFile.getAbsolutePath(), mEncodedFile.getAbsolutePath());
if (result == 0) {
Toast.makeText(getApplicationContext(), "Encoded to " + mEncodedFile.getName(), Toast.LENGTH_SHORT)
.show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
}
@Override
public void onDestroy() {
try{
mRecorder.release();
}catch(Exception e){
Toast.makeText(getApplicationContext(),"recorder resources are not released",Toast.LENGTH_LONG).show();
}
destroyEncoder();
super.onDestroy();
}
private void initRecorder() {
int bufferSize = AudioRecord.getMinBufferSize(SAMPLE_RATE, AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT);
mBuffer = new short[bufferSize];
mRecorder = new AudioRecord(MediaRecorder.AudioSource.MIC, SAMPLE_RATE, AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT, bufferSize);
}
private void startBufferedWrite(final File file) {
new Thread(new Runnable() {
@Override
public void run() {
DataOutputStream output = null;
try {
output = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
while (mIsRecording) {
int readSize = mRecorder.read(mBuffer, 0, mBuffer.length);
for (int i = 0; i < readSize; i++) {
output.writeShort(mBuffer[i]);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (output != null) {
try {
output.flush();
} catch (IOException e) {
Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
} finally {
try {
output.close();
} catch (IOException e) {
Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
}
}
}
}).start();
}
private File getFile(final String suffix) {
File filePath;
Time time = new Time();
time.setToNow();
/*try{
filePath= new File(Environment.getExternalStorageDirectory(), time.format("%Y%m%d%H%M%S") + "." + suffix);
}catch(Exception e)
{
e.printStackTrace();
}*/
filePath= new File(Environment.getExternalStorageDirectory(), time.format("%Y%m%d%H%M%S") + "." + suffix);
return filePath;
}
`
应用程序在手机中运行,但是当我点击 stopRecording 时,按下 startRecording 时,它似乎记录了logcat说编码开始并提供错误消息
A / libc:致命信号11(SIGSEGV)位于0x00000004(代码= 1),线程22791
我看到很多这类问题,但似乎都没有答案..
这是我的logcat对应..
`03-08 22:57:10.226 25288-25288/com.lameencoder D/dalvikvm﹕ Late- enabling CheckJNI
03-08 22:57:10.416 25288-25288/com.lameencoder D/dalvikvm﹕ Trying to load lib /data/app-lib/com.lameencoder-1/libmp3lame.so 0x41662a18
03-08 22:57:10.416 25288-25288/com.lameencoder D/dalvikvm﹕ Added shared lib /data/app-lib/com.lameencoder-1/libmp3lame.so 0x41662a18
03-08 22:57:10.416 25288-25288/com.lameencoder D/dalvikvm﹕ No JNI_OnLoad found in /data/app-lib/com.lameencoder-1/libmp3lame.so 0x41662a18, skipping init
03-08 22:57:10.496 25288-25288/com.lameencoder D/LAME ENCODER﹕ Init parameters:
03-08 22:57:10.496 25288-25288/com.lameencoder D/LAME ENCODER﹕ Number of channels: 1
03-08 22:57:10.496 25288-25288/com.lameencoder D/LAME ENCODER﹕ Sample rate: 16000
03-08 22:57:10.496 25288-25288/com.lameencoder D/LAME ENCODER﹕ Bitrate: 128
03-08 22:57:10.496 25288-25288/com.lameencoder D/LAME ENCODER﹕ Mode: 1
03-08 22:57:10.496 25288-25288/com.lameencoder D/LAME ENCODER﹕ Quality: 2
03-08 22:57:10.516 25288-25288/com.lameencoder D/LAME ENCODER﹕ Init returned: 0
03-08 22:57:10.556 25288-25288/com.lameencoder D/libEGL﹕ loaded /system/lib/egl/libGLES_hawaii.so
03-08 22:57:10.556 25288-25288/com.lameencoder D/﹕ mem_init ++
03-08 22:57:10.556 25288-25288/com.lameencoder D/﹕ gHwMemAllocator client 3
03-08 22:57:10.556 25288-25288/com.lameencoder D/﹕ **** Using ION allocator ****
03-08 22:57:10.556 25288-25288/com.lameencoder D/﹕ registered SIGUSR1[10] for pid[25288]
03-08 22:57:10.556 25288-25288/com.lameencoder D/﹕ HwMemAllocatorImpl Static Counters 0 0
03-08 22:57:10.556 25288-25288/com.lameencoder D/﹕ HwMemAllocatorImpl[50059dcc] totalDeviceAllocSize[0] totalFree[0] maxFree[0] in numSlabs[0]
03-08 22:57:10.566 25288-25288/com.lameencoder D/﹕ mem_init 50059dcc--
03-08 22:57:10.566 25288-25288/com.lameencoder D/ION﹕ config: version(0x10000) secure(0xf000) 256M(0x22d) fast(0x608) hwwr(0x608)
03-08 22:57:10.566 25288-25288/com.lameencoder D/MM_DEVICE﹕ Waiting for mm thread to come up
03-08 22:57:10.566 25288-25303/com.lameencoder D/MM_DEVICE﹕ mm_device_thread starting
03-08 22:57:10.566 25288-25288/com.lameencoder D/HAWAII_EGL﹕ eglCreateContext() config: 18 context: 0x50092d10, VC context 1, Thread 25288
03-08 22:57:10.566 25288-25288/com.lameencoder D/HAWAII_EGL﹕ Set SWAP INTERVAL 0
03-08 22:57:10.566 25288-25288/com.lameencoder D/HAWAII_EGL﹕ eglCreateWindowSurface() surface: 0x40046ad0, VC surface: 1, Thread: 25288
03-08 22:57:10.566 25288-25288/com.lameencoder D/HAWAII_EGL﹕ eglMakeCurrent(0x50092d10, 0x40046ad0, 0x40046ad0) Thread: 25288
03-08 22:57:10.566 25288-25288/com.lameencoder D/OpenGLRenderer﹕ Enabling debug mode 0
03-08 22:57:11.667 25288-25310/com.lameencoder W/System.err﹕ java.io.FileNotFoundException: /storage/emulated/0/20150308225711.raw: open failed: EACCES (Permission denied)
03-08 22:57:11.667 25288-25310/com.lameencoder W/System.err﹕ at libcore.io.IoBridge.open(IoBridge.java:416)
03-08 22:57:11.667 25288-25310/com.lameencoder W/System.err﹕ at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
03-08 22:57:11.667 25288-25310/com.lameencoder W/System.err﹕ at java.io.FileOutputStream.<init>(FileOutputStream.java:73)
03-08 22:57:11.667 25288-25310/com.lameencoder W/System.err﹕ at com.lameencoder.MainActivity$3.run(MainActivity.java:127)
03-08 22:57:11.667 25288-25310/com.lameencoder W/System.err﹕ at java.lang.Thread.run(Thread.java:856)
03-08 22:57:11.667 25288-25310/com.lameencoder W/System.err﹕ Caused by: libcore.io.ErrnoException: open failed: EACCES (Permission denied)
03-08 22:57:11.667 25288-25310/com.lameencoder W/System.err﹕ at libcore.io.Posix.open(Native Method)
03-08 22:57:11.667 25288-25310/com.lameencoder W/System.err﹕ at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
03-08 22:57:11.667 25288-25310/com.lameencoder W/System.err﹕ at libcore.io.IoBridge.open(IoBridge.java:400)
03-08 22:57:11.677 25288-25310/com.lameencoder W/System.err﹕ ... 4 more
03-08 22:57:12.908 25288-25288/com.lameencoder D/LAME ENCODER﹕ Encoding started
03-08 22:57:12.908 25288-25288/com.lameencoder A/libc﹕ Fatal signal 11 (SIGSEGV) at 0x00000004 (code=1), thread 25288 (com.lameencoder)
`
提前感谢...
很高兴见到你们..