Reputation: 4929
My app plays video file without sound. And it interrupts any playing music in backgroud (iPod app for example). How do not interrupt audio session of other apps if it's possible.
My video file is without sound. To play video i use MPMoviePlayerController
.
EDIT: Here is my video player code:
_player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:path]];
[self installMovieNotificationObservers:nil];
[_player setShouldAutoplay:YES];
[_player setUseApplicationAudioSession:NO];
[_player.view setFrame:self.navController.view.frame];
[_player setMovieSourceType:MPMovieSourceTypeFile];
[_player setRepeatMode:MPMovieRepeatModeNone];
[_player setFullscreen:YES animated:YES];
[_player setControlStyle:MPMovieControlStyleNone];
[_navController.view addSubview:_player.view];
[_player play];
Upvotes: 2
Views: 1917
Reputation: 2835
Swift 5 answer
do {
if #available(iOS 10.0, *) {
try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback, mode: AVAudioSession.Mode.default, options: .mixWithOthers)
} else {
try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback, options: .mixWithOthers)
}
try AVAudioSession.sharedInstance().setActive(true)
} catch {
print(error)
}
Upvotes: 1
Reputation: 1116
Directly from Apple's docs: http://developer.apple.com/library/ios/documentation/mediaplayer/reference/MPMoviePlayerController_Class/Reference/Reference.html#//apple_ref/occ/instp/MPMoviePlayerController/useApplicationAudioSession
useApplicationAudioSession A Boolean value that indicates whether the movie player should use the app’s audio session.
@property (nonatomic) BOOL useApplicationAudioSession Discussion The default value of this property is YES. Setting this property to NO causes the movie player to use a system-supplied audio session with a nonmixable playback category.
Important In iOS 3.1 and earlier, a movie player always uses a system-supplied audio session. To obtain that same behavior in iOS 3.2 and newer, you must set this property’s value to NO. When this property is YES, the movie player shares the app’s audio session. This give you control over how the movie player content interacts with your audio and with audio from other apps, such as the iPod. For important guidance on using this feature, see “Working with Movies and iPod Music” in Audio Session Programming Guide.
Changing the value of this property does not affect the currently playing movie. For the new setting to take effect, you must stop playback and then start it again.
Availability Available in iOS 3.2 and later. Declared In MPMoviePlayerController.h
Upvotes: 1