3

那么我现在面临的问题是尺寸问题。我允许用户从他们的音乐库中选择一首歌曲,然后将其切成碎片,然后能够在启用文件共享的计算机上使用 .wav 或 .mp3 文件。基本上我正在使用以下 AVAssetWritter 选项,并且我不断得到一个巨大的音频文件。EG 5 分钟的音乐大约是 50MB。我希望尝试将其减少到给定标准尺寸的行业。我有一种感觉,这与我使用 LinearPCM 作为我的选项之一有关,但是当我更改该选项或任何音频选项时,作者无法编写。我还在更改 AVAssetWritter 上的 FileType 参数。这是我的代码,它工作得很好,我只需要找到压缩或缩小文件。

AVAssetWriter *assetWriter = [[AVAssetWriter assetWriterWithURL:exportURL
                                                       fileType:AVFileTypeWAVE
                                                          error:&assetError]



NSDictionary *outputSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                [NSNumber numberWithInt:kAudioFormatLinearPCM], AVFormatIDKey, 
                                [NSNumber numberWithFloat:41000.0], AVSampleRateKey,
                                [NSNumber numberWithInt:2], AVNumberOfChannelsKey,
                                [NSData dataWithBytes:&channelLayout length:sizeof(AudioChannelLayout)], AVChannelLayoutKey,

                                [NSNumber numberWithInt:16], AVLinearPCMBitDepthKey,
                                [NSNumber numberWithBool:NO], AVLinearPCMIsNonInterleaved,
                                [NSNumber numberWithBool:NO],AVLinearPCMIsFloatKey,
                                [NSNumber numberWithBool:NO], AVLinearPCMIsBigEndianKey,
                                nil];
AVAssetWriterInput *assetWriterInput = [[AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeAudio
                                                                           outputSettings:outputSettings]
                                        retain];

任何有关压缩或使用我的 AVAssetWritter 写入不同编解码器而不是 WAVE 或 CoreAudioFormat 的帮助将不胜感激。

编辑:如果有办法直接从音乐库转到 .m4a 而不是 .wav 这将是我想采用的理想方法!

谢谢,尼克

回答

这是 .m4a 的正确音频选项,如果您希望用户将其视为 .mp3 或 .wav 文件,只需将它们附加到文件名的末尾!

AudioChannelLayout channelLayout;
memset(&channelLayout, 0, sizeof(AudioChannelLayout));
channelLayout.mChannelLayoutTag = kAudioChannelLayoutTag_Stereo;
NSDictionary *outputSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                [NSNumber numberWithInt: kAudioFormatMPEG4AAC], AVFormatIDKey,
                                [NSNumber numberWithFloat:44100.0], AVSampleRateKey,
                                [NSNumber numberWithInt:2], AVNumberOfChannelsKey,
                                [NSNumber numberWithInt:128000], AVEncoderBitRateKey,
                                [NSData dataWithBytes:&channelLayout length:sizeof(AudioChannelLayout)], AVChannelLayoutKey,                  

                                nil];    
4

2 回答 2

2

回答

这是 .m4a 的正确音频选项,如果您希望用户将其视为 .mp3 或 .wav 文件,只需将它们附加到文件名的末尾!

AudioChannelLayout channelLayout;
memset(&channelLayout, 0, sizeof(AudioChannelLayout));
channelLayout.mChannelLayoutTag = kAudioChannelLayoutTag_Stereo;
NSDictionary *outputSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                            [NSNumber numberWithInt: kAudioFormatMPEG4AAC], AVFormatIDKey,
                            [NSNumber numberWithFloat:44100.0], AVSampleRateKey,
                            [NSNumber numberWithInt:2], AVNumberOfChannelsKey,
                            [NSNumber numberWithInt:128000], AVEncoderBitRateKey,
                            [NSData dataWithBytes:&channelLayout    length:sizeof(AudioChannelLayout)], AVChannelLayoutKey,                  

                            nil];    
于 2012-02-22T02:33:59.643 回答
0

这个怎么样(很抱歉自己没有尝试,但它应该让你更接近):

AVAssetWriter *assetWriter = [AVAssetWriter assetWriterWithURL:exportURL
                                                       fileType:AVFileTypeAppleM4A
                                                          error:&assetError];

NSDictionary *outputSettings = [NSDictionary dictionaryWithObjectsAndKeys:
       [NSNumber numberWithInt: kAudioFormatMPEG4AAC], AVFormatIDKey,
       [NSNumber numberWithFloat:44100.0], AVSampleRateKey,
       [NSNumber numberWithInt:2], AVNumberOfChannelsKey,
       [NSNumber numberWithInt:128000], AVEncoderBitRateKey,
       [NSNumber numberWithInt:16], AVLinearPCMBitDepthKey,
       [NSNumber numberWithInt: AVAudioQualityHigh],  AVEncoderAudioQualityKey,
       nil];
于 2012-02-05T19:59:21.423 回答