我正在尝试合并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;
}
问题是合并后我能够在模拟器中播放声音。但是当我在设备上播放时,我只能通过耳机而不是扬声器获得声音
答案 0 :(得分:1)
您的设备可能会被软静音。如果您的设备是iPhone,请尝试切换侧面开关。
iPad有一个有趣的静音怪癖,即使音量调高并且您可以通过耳机听到声音,也可能导致iPad扬声器上的某些应用程序无法发出声音。要解决此问题,您需要按照以下说明进行操作
(*)如果您没有看到扬声器图标,则此功能将分配给侧面开关。在这种情况下,只需切换侧面开关即可取消iPad扬声器的静音。可以在“设置”下找到将侧面开关功能设置为“锁定旋转”或“静音”的选项。
答案 1 :(得分:1)
播放前设置AVAudioSession
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategorySoloAmbient withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error:nil];