Reputation: 131
I am trying to merge 4 audio files and play them. following is the code
- (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;
}
The problem is after merging i am able to play the sound in simulator. But when i am playing it on device then i am getting the sound only in headphone but not on speakers
Upvotes: 1
Views: 786
Reputation: 964
Set AVAudioSession before play
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategorySoloAmbient withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error:nil];
Upvotes: 1
Reputation: 5546
It is possible your device may be soft-muted. If your device is iPhone, please try toggling the side switch.
The iPad has an interesting mute quirk, which can result in no sound coming from certain apps over your iPad speaker, even though volume is turned up and you can hear the sound with headphones. In order to fix this, you need to follow the instructions below
(*) If you do not see the speaker icon, this feature is assigned to the side switch. In that case, just toggling the side switch should unmute your iPad speakers. The option to set the side switch function to Lock Rotation or Mute can be found under Settings.
Upvotes: 1