简体   繁体   中英

convert .mp3 from wav using Lame library in android

I got a hard problem when using Lame and I need your help. I am using LAME (version 3.9.9.5) my devices for debug is samsung galaxy s2 (OS android 4.0). I record and get 3ga file and rename to .wav file after that using LAME to config to mp3 file. I got mp3 file but when I play it not correct file (just zet zet) and shoter than original wav file. This is my mp3ConvertFIle: `public static final int NUM_CHANNELS = 2; // public static final int SAMPLE_RATE = 16000; public static final int SAMPLE_RATE = 48000; public static final int BITRATE = 128; public static final int MODE = 1; public static final int QUALITY = 2; 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);

private native int wavToMp3(String src, String dest, String quality,
        String bitrate);

/**
 * Convert a WAV file to MP3
 * 
 * @param src
 *            path to WAV file
 * @param dest
 *            path to MP3 file
 */
public void convert(String src, String dest) {
    initEncoder(NUM_CHANNELS, SAMPLE_RATE, BITRATE, MODE, QUALITY);
    encodeFile(src, dest);
}`

and this is my wrapper.c file:

#include "stdio.h"
#include "stdlib.h"
#include "jni.h"
#include "android/log.h"
#include "libmp3lame/lame.h"
#define BUFFER_SIZE 8192
#define be_short(s) ((short) ((unsigned short) (s) << 8) | ((unsigned short) (s) >> 8))
#define LOG_TAG "LAME ENCODER"
#define LOGD(format, args...)  __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, format, ##args);

lame_t lame;

int read_samples(FILE *input_file, short *input) {
    int nb_read;
    nb_read = fread(input, 1, sizeof(short), input_file) / sizeof(short);

    int i = 0;
    while (i < nb_read) {
        input[i] = be_short(input[i]);
        i++;
    }

    return nb_read;
}

void Java_com_dao_smsring_utility_MP3Converter_initEncoder(JNIEnv *env,
        jobject jobj, jint in_num_channels, jint in_samplerate, jint in_brate,
        jint in_mode, jint in_quality) {
    lame = lame_init();
    lame_set_num_channels(lame, in_num_channels);
    lame_set_in_samplerate(lame, in_samplerate);
    lame_set_out_samplerate(lame, 48000);
    lame_set_brate(lame, in_brate);
    lame_set_mode(lame, in_mode);
    lame_set_quality(lame, in_quality);
    int res = lame_init_params(lame);
    LOGD("version of %s is",get_lame_version());
}

void Java_com_dao_smsring_utility_MP3Converter_destroyEncoder(
        JNIEnv *env, jobject jobj) {
    int res = lame_close(lame);
}


void Java_com_dao_smsring_utility_MP3Converter_encodeFile(JNIEnv *env,
        jobject jobj, jstring in_source_path, jstring in_target_path) {
    const char *source_path, *target_path;
    source_path = (*env)->GetStringUTFChars(env, in_source_path, NULL);
    target_path = (*env)->GetStringUTFChars(env, in_target_path, NULL);

    FILE *input_file, *output_file;
    input_file = fopen(source_path, "rb");
    output_file = fopen(target_path, "wb");

    short input[BUFFER_SIZE];
    char output[BUFFER_SIZE];
    int nb_read = 0;
    int nb_write = 0;
    int nb_total = 0;

    while (nb_read = read_samples(input_file, input)) {
        nb_write = lame_encode_buffer(lame, input, input, nb_read, output,
                BUFFER_SIZE);
        fwrite(output, nb_write, 1, output_file);
        nb_total += nb_write;
    }
    LOGD(" total read %d is",nb_total);
    nb_write = lame_encode_flush(lame, output, BUFFER_SIZE);
//  fwrite(output, nb_write, 1, output_file);

    LOGD(" total write %d is",nb_write);
    lame_mp3_tags_fid(lame,output_file);

    lame_close(lame);
    fclose(input_file);
    fclose(output_file);
}

I don't know why? Help me please. Thanks you very much. My gmail: gunblackcatxiii@gmail.com Hoping to get your support

To record a wav file, capture with pcm audio codec maybe with 3ga you are using amr.

To use Lame in Android I would recommend you this link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM