還是因?yàn)橐纛l合成的原因儡首,合成的音頻是m4a 格式片任,上傳到七牛后,形成的鏈接蔬胯,瀏覽器可以播放对供。但是手機(jī)不行,m4a 是偏視頻格式氛濒,用原有的avplayer 方法不能播放产场,所以需要進(jìn)行轉(zhuǎn)化后上傳播放,
??:更新 之前刪除臨時(shí)m4a轉(zhuǎn)化文件的位置不對導(dǎo)致音頻過段,還有采樣率一定要對上舞竿。
下面是一些采樣率的基礎(chǔ)值介紹:
- 8,000 Hz - 電話所用采樣率京景,對于人的說話已經(jīng)足夠
- 11,025 Hz
- 22,050 Hz - 無線電廣播所用采樣率
- 32,000 Hz - miniDV數(shù)字視頻camcorder、DAT(LP mode)所用采樣率
- 44,100 Hz - 音頻CD,也常用于MPEG-1音頻(VCD, SVCD, MP3)所用采樣率
- 47,250 Hz - Nippon Columbia(Denon)開發(fā)的世界上第一個(gè)商用PCM錄音機(jī)所用采樣率
- 48,000 Hz - miniDV骗奖、數(shù)字電視确徙、DVD、DAT执桌、電影和專業(yè)音頻所用的數(shù)字聲音所用采樣率
- 50,000 Hz - 二十世紀(jì)七十年代后期出現(xiàn)的3M和Soundstream開發(fā)的第一款商用數(shù)字錄音機(jī)所用采樣率
- 50,400 Hz - 三菱X-80數(shù)字錄音機(jī)所用所用采樣率
- 96,000或者192,000 Hz - DVD-Audio鄙皇、一些LPCM DVD音軌、Blu-ray Disc(藍(lán)光盤)音軌鼻吮、和HD-DVD(高清晰度DVD)音軌所用所用采樣率
- 2.8224 MHz - SACD育苟、索尼和飛利浦聯(lián)合開發(fā)的稱為Direct Stream Digital的1位sigma-delta modulation過程所用采樣率。
一定要采用合適的采樣率椎木,要不音頻是播不出來的违柏。
代碼如下:
-(void)convetM4aToWav:(NSURL *)originalUrl
destUrl:(NSString *)destUrlStr
completed:(void (^)(NSError *error)) completed {
NSLog(@"\n\n\nM4A-WAV\n\n\n");
if ([[NSFileManager defaultManager] fileExistsAtPath:destUrlStr]) {
[[NSFileManager defaultManager] removeItemAtPath:destUrlStr error:nil];
}
NSURL *destUrl = [NSURL fileURLWithPath:destUrlStr];
AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL:originalUrl options:nil];
//讀取原始文件信息
NSError *error = nil;
AVAssetReader *assetReader = [AVAssetReader assetReaderWithAsset:songAsset error:&error];
if (error) {
NSLog (@"error: %@", error);
completed(error);
return;
}
AVAssetReaderOutput *assetReaderOutput = [AVAssetReaderAudioMixOutput
assetReaderAudioMixOutputWithAudioTracks:songAsset.tracks
audioSettings: nil];
if (![assetReader canAddOutput:assetReaderOutput]) {
NSLog (@"can't add reader output... die!");
completed(error);
return;
}
[assetReader addOutput:assetReaderOutput];
AVAssetWriter *assetWriter = [AVAssetWriter assetWriterWithURL:destUrl
fileType:AVFileTypeCoreAudioFormat
error:&error];
if (error) {
NSLog (@"error: %@", error);
completed(error);
return;
}
AudioChannelLayout channelLayout;
memset(&channelLayout, 0, sizeof(AudioChannelLayout));
channelLayout.mChannelLayoutTag = kAudioChannelLayoutTag_Stereo;
NSDictionary *outputSettings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:kAudioFormatLinearPCM], AVFormatIDKey,
[NSNumber numberWithFloat:8000], AVSampleRateKey,
[NSNumber numberWithInt:2], AVNumberOfChannelsKey,
[NSNumber numberWithInt:16], AVLinearPCMBitDepthKey,
[NSData dataWithBytes :&channelLayout length:sizeof(AudioChannelLayout)],AVChannelLayoutKey,
[NSNumber numberWithBool:NO], AVLinearPCMIsNonInterleaved,
[NSNumber numberWithBool:NO],AVLinearPCMIsFloatKey,
[NSNumber numberWithBool:NO], AVLinearPCMIsBigEndianKey,
nil];
AVAssetWriterInput *assetWriterInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeAudio
outputSettings:outputSettings];
if ([assetWriter canAddInput:assetWriterInput]) {
[assetWriter addInput:assetWriterInput];
} else {
NSLog (@"can't add asset writer input... die!");
completed(error);
return;
}
assetWriterInput.expectsMediaDataInRealTime = NO;
[assetWriter startWriting];
[assetReader startReading];
AVAssetTrack *soundTrack = [songAsset.tracks objectAtIndex:0];
CMTime startTime = CMTimeMake (0, soundTrack.naturalTimeScale);
[assetWriter startSessionAtSourceTime:startTime];
__block UInt64 convertedByteCount = 0;
dispatch_queue_t mediaInputQueue = dispatch_queue_create("mediaInputQueue", NULL);
[assetWriterInput requestMediaDataWhenReadyOnQueue:mediaInputQueue
usingBlock: ^
{
while (assetWriterInput.readyForMoreMediaData) {
CMSampleBufferRef nextBuffer = [assetReaderOutput copyNextSampleBuffer];
if (nextBuffer) {
// append buffer
[assetWriterInput appendSampleBuffer: nextBuffer];
// NSLog (@"appended a buffer (%zu bytes)",
// CMSampleBufferGetTotalSampleSize (nextBuffer));
convertedByteCount += CMSampleBufferGetTotalSampleSize (nextBuffer);
} else {
[assetWriterInput markAsFinished];
[assetWriter finishWritingWithCompletionHandler:^{
}];
[assetReader cancelReading];
NSDictionary *outputFileAttributes = [[NSFileManager defaultManager]
attributesOfItemAtPath:[destUrl path]
error:nil];
NSLog (@"FlyElephant %lld",[outputFileAttributes fileSize]);
NSLog(@"轉(zhuǎn)換結(jié)束");
// 刪除臨時(shí)temprecordAudio.m4a文件
NSError *removeError = nil;
if ([[NSFileManager defaultManager] fileExistsAtPath:[originalUrl path]]) {
BOOL success = [[NSFileManager defaultManager] removeItemAtPath:[originalUrl path] error:&removeError];
if (!success) {
NSLog(@"刪除臨時(shí)temprecordAudio.m4a文件失敗:%@",removeError);
completed(removeError);
}else{
NSLog(@"刪除臨時(shí)temprecordAudio.m4a文件:%@成功",originalUrl);
completed(removeError);
}
}else {
NSLog(@"文件不存在");
}
break;
}
}
}];
}
文檔目錄相關(guān)代碼如下:
- (NSString *)documentPath
{
NSString *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
return documentPath;
}
- (NSString *)audioCacheFolder
{
NSString *audioFolder = [[self documentPath] stringByAppendingPathComponent:AUDIOCACHE];
if (![FILEMANAGER fileExistsAtPath:audioFolder]) {
NSError *error = nil;
[FILEMANAGER createDirectoryAtPath:audioFolder withIntermediateDirectories:YES attributes:nil error:&error];
if (error) {
NSLog(@"音頻文件夾創(chuàng)建失敗----%@", error);
}
}
return audioFolder;
}
//用url作為文件名
- (NSString *)audioFilePath:(NSString *)audioURL
{
NSString *fileName = [audioURL stringByReplacingOccurrencesOfString:@"/" withString:@"-"];
return [[self audioCacheFolder] stringByAppendingPathComponent:fileName];
}