在 ios 中合并音频文件的问题
Posted
技术标签:
【中文标题】在 ios 中合并音频文件的问题【英文标题】:Issue with merging audio file in ios 【发布时间】:2012-12-06 08:16:22 【问题描述】:我正在尝试合并 4 个音频文件并播放它们。以下是代码
- (BOOL) combineVoices
NSMutableArray * arr=[[NSMutableArray alloc] init];
[arr addObject:@"player_4_full"];
[arr addObject:@"event_1_1_1"];
[arr addObject:@"team_2_1"];
[arr addObject:@"event_1_1_2"];
NSError *error = nil;
BOOL ok = NO;
CMTime nextClipStartTime = kCMTimeZero;
//Create AVMutableComposition Object.This object will hold our multiple AVMutableCompositionTrack.
AVMutableComposition *composition = [[AVMutableComposition alloc] init];
AVMutableCompositionTrack *compositionAudioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
for(NSString * str in arr)
NSString *path = [[NSBundle mainBundle] pathForResource:str ofType:@"mp3"];
NSURL *url = [[NSURL alloc] initFileURLWithPath:path];
AVAsset *avAsset = [[AVURLAsset alloc] initWithURL:url options:nil];
NSArray *tracks = [avAsset tracksWithMediaType:AVMediaTypeAudio];
if ([tracks count] == 0)
return NO;
NSLog(@"%@",avAsset);
CMTimeRange timeRangeInAsset = CMTimeRangeMake(kCMTimeZero, [avAsset duration]);
AVAssetTrack *clipAudioTrack = [[avAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
ok = [compositionAudioTrack insertTimeRange:timeRangeInAsset ofTrack:clipAudioTrack atTime:nextClipStartTime error:&error];
if (!ok)
NSLog(@"Current Video Track Error: %@",error);
nextClipStartTime = CMTimeAdd(nextClipStartTime, timeRangeInAsset.duration);
NSLog(@"%@",composition);
self.item = [[AVPlayerItem alloc] initWithAsset:composition];
self.p =[AVPlayer playerWithPlayerItem:item];
[self.p play];
return YES;
问题是合并后我可以在模拟器中播放声音。但是当我在设备上播放时,我只能在耳机中听到声音,而不是在扬声器上
【问题讨论】:
【参考方案1】:您的设备可能已软静音。如果您的设备是 iPhone,请尝试切换侧面开关。
iPad 有一个有趣的静音怪癖,它可能导致某些应用程序无法通过 iPad 扬声器发出声音,即使音量已调高并且您可以通过耳机听到声音。为了解决这个问题,您需要按照以下说明进行操作
双击主页按钮 从左向右滑动 点按最左侧的扬声器图标 (*) 它应该在播放按钮下方显示“静音关闭” 再次聆听应用声音(*) 如果您没有看到扬声器图标,则此功能已分配给侧面开关。在这种情况下,只需切换侧面开关即可取消 iPad 扬声器的静音。可以在设置下找到将侧边开关功能设置为锁定旋转或静音的选项。
【讨论】:
【参考方案2】:在播放前设置 AVAudiosession
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategorySoloAmbient withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error:nil];
【讨论】:
以上是关于在 ios 中合并音频文件的问题的主要内容,如果未能解决你的问题,请参考以下文章