AVAudioPlayer via Speakers

后端 未结 8 623
甜味超标
甜味超标 2021-02-06 13:16

I got the following code:

- (id)init {
    if (self = [super init]) {
        UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
        AudioSessionS         


        
8条回答
  •  醉梦人生
    2021-02-06 13:55

    Very good example here http://ruckt.info/playing-sound-through-iphone-speaker/ Solved my problem.

    The method below - configureAVAudioSession - reroutes the audio to the main speakers in iOS 7.0. To do so, call it before playing the audio in this way: [self configureAVAudioSession].

    - (void) configureAVAudioSession //To play through main iPhone Speakers
    {
        //get your app's audioSession singleton object
        AVAudioSession* session = [AVAudioSession sharedInstance];
    
        //error handling
        BOOL success;
        NSError* error;
    
        //set the audioSession category.
        //Needs to be Record or PlayAndRecord to use audioRouteOverride:
    
        success = [session setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];
    
        if (!success)  
        {
            NSLog(@"AVAudioSession error setting category:%@",error);
        }
        //set the audioSession override
        success = [session overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&error];
        if (!success)  
        {
            NSLog(@"AVAudioSession error overrideOutputAudioPort:%@",error);
        }
        //activate the audio session
        success = [session setActive:YES error:&error];
        if (!success) 
        {
            NSLog(@"AVAudioSession error activating: %@",error);
        }
        else 
        {
            NSLog(@"audioSession active");
        }
    }
    

提交回复
热议问题