使用avaudio播放非常低

时间:2014-08-04 17:37:53

标签: ios xcode avaudioplayer

我有录制和播放例程。见下面的代码。唯一的问题是我的设备上的播放非常低。在模拟器上音量合适,我的设备音量一直在增加。我需要设置其他东西吗? 这是记录代码:

NSArray *pathComponents = [NSArray arrayWithObjects:


                            [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],
                               @"MyAudioMemo.m4a",
                               nil];
    NSURL *outputFileURL = [NSURL fileURLWithPathComponents:pathComponents];

    // Setup audio session
    AVAudioSession *session = [AVAudioSession sharedInstance];
    [session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];

    // Define the recorder setting
    NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init];

    [recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
    [recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
    [recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];

    // Initiate and prepare the recorder
    recorder = [[AVAudioRecorder alloc] initWithURL:outputFileURL settings:recordSetting error:NULL];
    recorder.delegate = self;
    recorder.meteringEnabled = YES;
    [recorder prepareToRecord];
[recorder record];

播放:

 player = [[AVAudioPlayer alloc] initWithContentsOfURL:recorder.url error:nil];
        [player setDelegate:self];
        player.meteringEnabled = YES;

        player.volume = 1.0;
        [player play];

1 个答案:

答案 0 :(得分:0)

将扬声器设为默认值,因为有时间问题音频通过其他路线

[session setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error:&sessionError];

托盘AVAudioSessionCategoryAmbient模式播放声音时音量很高

[session setCategory:AVAudioSessionCategoryAmbient error:&setCategoryError];