Ignore ringtone mute switch during MPMoviePlayer video playback in iOS

24 May
24/05/2011

Recently I stumbled across an issue in one of the apps we are creating over at GrandCentrix: When playing back video using the MPMoviePlayer framework, the ringtone mute switch switches off all audio.

This default behavior is significantly different from the one iOS users learned from the built-in YouTube app.

Worse: If users start video playback with the mute switch turned on, they might believe they’ve got a faulty signal, as they won’t here any audio right from the beginning.

Turns out, it’s easy to fix:

[obj-c title=”Ignore the ringtone mute switch”]

– (void)viewDidLoad {
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:@"http://cdn.grandcentrix.net/video/ios/stream.m3u8"];
MPMoviePlayerViewController *moviePlayerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:url];

NSError *_error = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: &_error];

[self.view addSubview:moviePlayerViewController.view];
}

[/obj-c]

Note that you have to add the AVFoundation framework to your project. In Xcode 4 to add a framework select the topmost node in the Project Navigator, select your Target, click Build Phases and hit the little + icon.

Adding Framework in Xcode 4

This and more tips frequently get discussed over at the great iOS Developers Group on Facebook. If you’re on Twitter, follow me (@24z) for mostly iOS related tweets!


Tags: , , ,
0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *

© Copyright 2017 by Ralf Rottmann.