iOS将CAF文件转换为MP3----lame编译篇

本文详细介绍如何在iOS中使用lame将CAF格式录音转换为跨平台兼容的MP3格式,包括源码编译及framework生成步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

开篇语:

现在很多项目都加入了语音功能,可是iOS录音格式是caf,这导致音频文件很难在PC和Android端无缝播放,为了解决此问题,我们需要选择一种所有平台都能播放的格式:mp3 。iOS端要想完成转换,还得靠lame啊。好了,在使用它之前,先看看如何编译吧!

编译:

先找个地方下载源码:官网下载地址 如果下载慢的话,可以下载优快云资源:lame 3.100 ,压缩包不大 6M多点。然后就是我们的编译文件了:

build-lame-framework.sh  和lame源码放在同一个路径即可。直接生成lame.framework,是不是很方便~

#!/bin/sh

CONFIGURE_FLAGS="--disable-shared --disable-frontend"

ARCHS="arm64 armv7s armv7 x86_64"  #i386

# directories
SOURCE="lame-3.100"
FAT=".fat-lame"

SCRATCH=".scratch-lame"
# must be an absolute path
THIN=`pwd`/".thin-lame"

COMPILE="y"
LIPO="y"
FRAMEWORK="y"

if [ "$*" ]
then
	if [ "$*" = "lipo" ]
	then
		# skip compile
		COMPILE=
	else
		ARCHS="$*"
		if [ $# -eq 1 ]
		then
			# skip lipo
			LIPO=
		fi
	fi
fi

if [ "$COMPILE" ]
then
	CWD=`pwd`
	for ARCH in $ARCHS
	do
		echo "building $ARCH..."
		mkdir -p "$SCRATCH/$ARCH"
		cd "$SCRATCH/$ARCH"

		if [ "$ARCH" = "i386" -o "$ARCH" = "x86_64" ]
		then
		    PLATFORM="iPhoneSimulator"
		    if [ "$ARCH" = "x86_64" ]
		    then
		    	SIMULATOR="-mios-simulator-version-min=7.0"
                        HOST=x86_64-apple-darwin
		    else
		    	SIMULATOR="-mios-simulator-version-min=5.0"
                        HOST=i386-apple-darwin
		    fi
		else
		    PLATFORM="iPhoneOS"
		    SIMULATOR=
                    HOST=arm-apple-darwin
		fi

		XCRUN_SDK=`echo $PLATFORM | tr '[:upper:]' '[:lower:]'`
		CC="xcrun -sdk $XCRUN_SDK clang -arch $ARCH"
		#AS="$CWD/$SOURCE/extras/gas-preprocessor.pl $CC"
		CFLAGS="-arch $ARCH $SIMULATOR -fembed-bitcode"
		CXXFLAGS="$CFLAGS"
		LDFLAGS="$CFLAGS"

		CC=$CC $CWD/$SOURCE/configure \
		    $CONFIGURE_FLAGS \
                    --host=$HOST \
		    --prefix="$THIN/$ARCH" \
                    CC="$CC" CFLAGS="$CFLAGS" LDFLAGS="$LDFLAGS"

		make -j3 install
		cd $CWD
	done
fi

if [ "$LIPO" ]
then
	echo "building fat binaries..."
	mkdir -p $FAT/lib
	set - $ARCHS
	CWD=`pwd`
	cd $THIN/$1/lib
	for LIB in *.a
	do
		cd $CWD
		lipo -create `find $THIN -name $LIB` -output $FAT/lib/$LIB
	done

	cd $CWD
	cp -rf $THIN/$1/include $FAT
fi

if [ "$FRAMEWORK" ]
then
	rm -rf lame.framework
	echo "building lame.framework..."
	mkdir -p lame.framework/Headers/
	cp -rf $FAT/include/lame/* lame.framework/Headers/
	cp -f $FAT/lib/libmp3lame.a lame.framework/lame
fi

#   clean tmp directories
rm -rf $SOURCE $FAT $SCRATCH $THIN

执行方法:

./build-lame-framework.sh

附上已经编译好的文件:lame.framework  支持 arm64、armv7s、armv7、x86_64

到此为止,前期工作已经准备完毕,下一篇文章,让我将结束lame的简单使用方法。

 

 

ios录音的caf文件MP3文件,以兼容android 注意音频参数的设置,如果声音异常,请调整参数。 code: AVAudioSession *session = [AVAudioSession sharedInstance]; NSError *sessionError; [session setCategory:AVAudioSessionCategoryPlayAndRecord error:&sessionError]; _sampleRate = 11025;//8000;//44100; _quality = AVAudioQualityLow; if(session == nil) NSLog(@"Error creating session: %@", [sessionError description]); else [session setActive:YES error:nil]; NSString *cafFilePath = strCaf; // NSString *mp3FileName = @"Mp3File"; // mp3FileName = [mp3FileName stringByAppendingString:@".mp3"]; NSString *mp3FilePath = strMP3Path;//[[NSHomeDirectory() stringByAppendingFormat:@"/Documents/"] stringByAppendingPathComponent:mp3FileName]; @try { int read, write; FILE *pcm = fopen([cafFilePath cStringUsingEncoding:1], "rb"); //source fseek(pcm, 4*1024, SEEK_CUR); //skip file header FILE *mp3 = fopen([mp3FilePath cStringUsingEncoding:1], "wb"); //output const int PCM_SIZE = 8192; const int MP3_SIZE = 8192; short int pcm_buffer[PCM_SIZE*2]; unsigned char mp3_buffer[MP3_SIZE]; lame_t lame = lame_init(); lame_set_in_samplerate(lame, _sampleRate); lame_set_VBR(lame, vbr_default); lame_init_params(lame); do { read = fread(pcm_buffer, 2*sizeof(short int), PCM_SIZE, pcm); if (read == 0) write = lame_encode_flush(lame, mp3_buffer, MP3_SIZE); else write = lame_encode_buffer_interleaved(lame, pcm_buffer, read, mp3_buffer, MP3_SIZE); fwrite(mp3_buffer, write, 1, mp3); } while (read != 0); lame_close(lame); fclose(mp3); fclose(pcm); } @catch (NSException *exception) { NSLog(@"%@",[exception description]); } @finally { }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TheLittleBoy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值