2012-12-06 19 views
1

我想合并4个音频文件并播放它们。以下是代码合并ios中的音频文件问题

- (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扬声器上的某些应用可能会导致没有声音。为了解决这个问题,就需要按照下面的说明

  • 双击home键从左至右
  • 刷卡点击扬声器图标到最左边(*)
  • 它应该显示“静音关闭”播放按钮下方
  • 听声音的应用程序再次

enter image description here

(*)如果您没有看到扬声器图标,则将此功能分配给侧面开关。在这种情况下,只需切换侧面开关即可取消iPad扬声器的静音。可以在设置下找到将侧面开关功能设置为锁定旋转或静音的选项。

1

集AVAudioSession前玩

AVAudioSession *audioSession = [AVAudioSession sharedInstance]; 
[audioSession setCategory:AVAudioSessionCategorySoloAmbient withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error:nil];