-1

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.

Supertecnoboff
  • 6,406
  • 11
  • 57
  • 98
  • Did you change the category of the AVAudioSession like the answer found here? http://stackoverflow.com/questions/32476523/make-avaudioplayer-play-a-music-file-through-phone-call-ear-speaker – Stonz2 Oct 16 '15 at 16:51
  • @Stonz2 I am trying to play a video here, not an audio track. In any case, Xcode comes up with the error: ```Expected ;``` when adding that line in. – Supertecnoboff Oct 16 '15 at 16:56
  • Yeah, it's a bit malformed in the answer I noticed. He's missing the first part of the method name `setCategory:` before the enum. – Stonz2 Oct 16 '15 at 17:10

1 Answers1

0

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.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Why does everyone keep going on about ```AVAudioSession``` that applies for when you want to play an audio track alone. I am trying to play a video here. The problem I am having is that I can't get the audio track of the video to play through the speakers. – Supertecnoboff Oct 16 '15 at 17:01