如何在IOS中使用lame编码为MP3

问题描述 投票:0回答:2

我知道以前有一些关于这个话题的问题,但是那里的修女似乎有完整的答案。

我正在尝试将AFF编码为MP3,发现唯一的免费方式是使用“LAME”。不幸的是它只支持mac,所以当我尝试在IOS项目中使用它时,我会遇到链接器错误。

是否有人设法使用此库进行此用途?任何帮助将不胜感激,这对我来说非常重要..

ios objective-c core-audio lame
2个回答
0
投票

您可以使用autoconf编译库libmp3lame。我使用以下bash脚本来设置configure的参数:

#!/bin/sh

# The device's architecture
export ARCH="armv7-apple-darwin12"

# SDK setup
export SDKVER="7.1"
export SDKROOT="/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS7.1.sdk"

export PREFIX="/tmp/iphone-$SDKVER-armv7"

export PKG_CONFIG_PATH="$SDKROOT/usr/lib/pkgconfig":"$PREFIX/lib/pkgconfig"

# Flags
export CPPFLAGS="-arch armv7 -isysroot $SDKROOT -I$PREFIX/include"
export CFLAGS="-std=gnu99"
export CXXFLAGS="-std=gnu++11 -stdlib=libc++"
export LDFLAGS="-arch armv7 -isysroot $SDKROOT -L$PREFIX/lib"

# Run configure 
./configure \
        --prefix="$PREFIX" \
        --host="$ARCH" \
        --enable-static \
        --disable-shared $@

如果将上面的代码保存到LAME源目录中的ios-configure.sh,则可以使用以下命令配置和编译:

% ./ios-configure.sh
% make libmp3lame

0
投票
#import <Foundation/Foundation.h>
#import <lame/lame.h>

NS_ASSUME_NONNULL_BEGIN

const int PCM_SIZE = 4096;
const int MP3_SIZE = 4096;

@interface AudioFile : NSObject

-(BOOL) encode: (float*) recordedData
     datasize : (int) size
    sampleRate: (int) nSampleRate
       docName: (char*) fileName;

@end

NS_ASSUME_NONNULL_END




#import "AudioFile.h"
#import <AVFoundation/AVFoundation.h>

@implementation AudioFile

short int pcm_buffer_I_L[PCM_SIZE];
short int pcm_buffer_I_R[PCM_SIZE];
unsigned char mp3buf[MP3_SIZE];

-(BOOL) encode: (float*) recordedData // Float Array
     datasize : (int) size            // Size of Float Array
    sampleRate: (int) nSampleRate     // Sample rate
       docName: (char*) fileName      // File Name
{
    NSString *file = [NSString stringWithCString:fileName encoding:NSASCIIStringEncoding];

    NSString *tmpUrl = [NSString stringWithFormat:@"%@/%@", NSTemporaryDirectory(), file];

    NSLog(@"Mp3 File Path: %@", tmpUrl);

    float write;

    //Converting into mp3, so we need the mp3 file ready!
    FILE *mp3 = fopen((char*)[tmpUrl UTF8String], "wb");

    lame_t lame = lame_init();
    lame_set_in_samplerate(lame, nSampleRate);
    lame_set_out_samplerate(lame, nSampleRate);
    lame_set_VBR(lame, vbr_max_indicator);
    lame_set_mode(lame, MONO); //if this is enabled the channels will be set to 1 by default
    //    lame_set_num_channels(lame, 1);
    lame_set_quality(lame, 0); //0 is the best .. 9 is the worst
    lame_init_params(lame);

    NSLog(@"Sample Rate: %d " , lame_get_in_samplerate(lame));

    /*
     lame_encode_buffer_interleaved_ieee_float
     1st pram = lame config
     2nd pram = float
     3rd pram = frame
     4th pram = mp3buf //mp3buf is an array of unsigned char with size of 4096
     5th pram = size of Mp3 which is 4096
     */

    int i;
    for (i = 0; i < size; i++)
    {
        write = lame_encode_buffer_interleaved_ieee_float(lame, &recordedData[i], 1, mp3buf, MP3_SIZE);

        fwrite(mp3buf, write, 1, mp3);
    }

    lame_close(lame);

    fclose(mp3);

    return YES;
}

@end

你可以使用我的git链接。代码在Objective C中

https://gitlab.com/a.raza/lame-encoder-and-decoder-for-ios-unity-plugin

© www.soinside.com 2019 - 2024. All rights reserved.