Search code examples
iosobjective-cavplayeravplayerviewcontrolleravkit

Cannot play video audio via speakers - iOS


I have an iOS 9 application and I have implemented the AVPlayerViewController into one of my view controllers. I am trying to play a video. One problem I have noticed is that if I plug in the headphones, the audio plays in the headphones fine. But if I unplug the headphones, the audio will play from the little ear speaker at the top of the iPhone and NOT from the main speaker. How can I change this? Here is my code:

// Setup the video player view.
AVPlayerViewController *playerViewController = [[AVPlayerViewController alloc] init];
playerViewController.player = [AVPlayer playerWithURL:pass_URL];
self.player_view = playerViewController;
self.player_view.view.frame = self.view.frame;
[self.view addSubview:playerViewController.view];
self.view.autoresizesSubviews = TRUE;
[self.player_view.player play];

I have imported the following frameworks into my Xcode project:

#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>

Thanks for your time, Dan.


Solution

  • You need to add a routing change observer (AVAudioSessionRouteChangeNotification) to your AVAudioSession. You can then detect the removal of the headphones and dictate the routing yourself (e.g. with overrideOutputAudioPort) if necessary.