【问题标题】:convert .mp3 from wav using Lame library in android使用 android 中的 Lame 库从 wav 转换 .mp3
【发布时间】:2013-12-13 07:43:04
【问题描述】:

我在使用 Lame 时遇到了一个难题,我需要您的帮助。 我正在使用 LAME(版本 3.9.9.5)我的调试设备是三星 Galaxy s2(操作系统 android 4.0)。 我录制并获取 3ga 文件并重命名为 .wav 文件,然后使用 LAME 配置为 mp3 文件。 我得到了 mp3 文件,但是当我播放它时文件不正确(只是 zet zet)并且比原始 wav 文件更准确。 这是我的 mp3ConvertFIle: `public static final int NUM_CHANNELS = 2; // public static final int SAMPLE_RATE = 16000; 公共静态最终 int SAMPLE_RATE = 48000; 公共静态最终 int 比特率 = 128; 公共静态最终 int MODE = 1; 公共静态最终 int 质量 = 2; 静止的 { 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);
}`

这是我的 wrapper.c 文件:

#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);
}

我不知道为什么?请帮帮我。 非常感谢你。 我的邮箱:gunblackcatxiii@gmail.com 希望得到您的支持

【问题讨论】:

    标签: android lame


    【解决方案1】:

    要录制 wav 文件,请使用 pcm 音频编解码器进行捕获,也许使用 3ga 您正在使用 amr。

    要在 Android 中使用 Lame,我会向您推荐 link

    【讨论】:

    • 亲爱的,感谢您的评论,我认为我的问题是在捕获阶段。我要检查一下。非常感谢,
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-21
    相关资源
    最近更新 更多