1

我正在尝试合并 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;
}

问题是合并后我可以在模拟器中播放声音。但是当我在设备上播放时,我只能在耳机中听到声音,而不是在扬声器上

4

2 回答 2

1

您的设备可能已软静音。如果您的设备是 iPhone,请尝试切换侧面开关。

iPad 有一个有趣的静音怪癖,这可能会导致某些应用程序无法通过 iPad 扬声器发出声音,即使音量调高并且您可以用耳机听到声音。为了解决这个问题,您需要按照以下说明进行操作

  • 双击主页按钮
  • 从左向右滑动
  • 点击最左侧的扬声器图标 (*)
  • 它应该在播放按钮下方显示“静音关闭”
  • 再次聆听应用声音

在此处输入图像描述

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

于 2013-01-03T08:25:56.720 回答
1

播放前设置 AVAudioSession

AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategorySoloAmbient withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error:nil];
于 2014-01-30T10:57:18.547 回答