Sn0wfreeze
Sn0wfreeze

Reputation: 2032

iOS play audio through iPhone receiver (phone speaker)

I am currently working on an application which should play Audio files through the iPhone receiver. I know it was easily possible before iOS 6/7, but those methods are deprecated now.

So does anybody know how it works on iOS 7?

This is my code, which does not work:

    _audioSession = [AVAudioSession sharedInstance];
    [_audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
    [_audioSession overrideOutputAudioPort:AVAudioSessionPortBuiltInReceiver error:nil];

    NSString *ringtone =  [[NSUserDefaults standardUserDefaults] stringForKey:@"ringtone"];
    ringtone = [ringtone stringByReplacingOccurrencesOfString:@".m4r" withString:@""];
    NSString *path;
    path = [[NSBundle mainBundle] pathForResource:@"abto_ringbacktone" ofType:@"wav"];


    NSError *error;
    _player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:path] error:&error];
    if (error) {
        NSLog(@"Error: %@",error.localizedDescription);
    }

}

[_player setNumberOfLoops:10];
[_player prepareToPlay];
[_player play];
[_player setVolume:0.1];

Upvotes: 1

Views: 1316

Answers (1)

Piotr Tobolski
Piotr Tobolski

Reputation: 1406

You cannot pass AVAudioSessionPortBuiltInReceiver to the overrideOutputAudioPort:error: because it takes enum as an argument, not a string.

Correct way to do this is to configure session as follows for Receiver:

[_audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
[_audioSession overrideOutputAudioPort:AVAudioSessionPortOverrideNone error:nil];

and for Speaker:

[_audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
[_audioSession overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:nil];

Upvotes: 2

Related Questions