keegan3d
keegan3d

Reputation: 11275

MPMoviePlayerController want video only, no audio

I'm using video in my app for some tutorials screens. The video has no audio track. I have everything working except if the user is listening to audio already, like from the Music app, the audio is stopped when one of the tutorial videos starts.

Is there a way to only play the video and not affect the audio so the user can continue to enjoy the audio they had playing?

I tried setting [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil]; before [self.player play] but that didn't work. useApplicationAudioSession looked promising but it was deprecated in iOS 6. I've also tried AVPlayer and again setting AVAudioSessionCategoryAmbient before play but no luck.

Upvotes: 0

Views: 1440

Answers (3)

Fahri Azimov
Fahri Azimov

Reputation: 11770

Try to set [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil]; in application:didFinishLaunchingWithOptions:and try AVPlayer with AVPreviewLayer to play video. Hope this helps.

Upvotes: 4

Son Nguyen
Son Nguyen

Reputation: 3481

[videoPlayer moviePlayer].useApplicationAudioSession = YES;
[[MPMusicPlayerController applicationMusicPlayer] setVolume:0.0];

Upvotes: -3

Ashley Mills
Ashley Mills

Reputation: 53082

I'm not sure this is exactly what you're asking, but I'd written my answer before I re-read the question, and it might be useful for someone looking here…


You can't silence the audio on an MPMoviePlayerController. You can switch to using AVPlayer however, and remove the audio as follows:

// Load the video asset
AVURLAsset *asset = [AVURLAsset URLAssetWithURL: <your-video-URL> options:nil];
NSArray *audioTracks = [asset tracksWithMediaType:AVMediaTypeAudio];

// Mute all the audio tracks
NSMutableArray *allAudioParams = [NSMutableArray array];
for (AVAssetTrack *track in audioTracks) {
    AVMutableAudioMixInputParameters *audioInputParams =[AVMutableAudioMixInputParameters audioMixInputParameters];
    [audioInputParams setVolume:0.0 atTime:kCMTimeZero];
    [audioInputParams setTrackID:[track trackID]];
    [allAudioParams addObject:audioInputParams];
}
AVMutableAudioMix *audioZeroMix = [AVMutableAudioMix audioMix];
[audioZeroMix setInputParameters:allAudioParams];

// Create a player item
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:asset];
[playerItem setAudioMix:audioZeroMix]; // Mute the player item

 // Create an AVPlayer
 AVPlayer * moviePlayer = [AVPlayer playerWithPlayerItem: playerItem]

Then you'll need to add the AVPlayer's playerLayer to your view controller's view hierarchy.

The AVPlayer also gives you greater control over playback (finer grained seeking, for example).

Upvotes: 0

Related Questions