My app plays a video and occasionally has verbal queues. When there is nothing being said by the app, I want the audio (in the background say from Spotify or iTunes) to come back to full volume. When there is audio I want the audio to dim.
I have set the audio session to DuckOthers
.
The problem is, whenever I restore the background audio:
class func restoreBackgroundAudio()
{
do {
try AVAudioSession.sharedInstance().setActive(false)
} catch _ {
print("ERROR restoring background audio")
}
}
I get this error:
[0x1a03a9000] AVAudioSession.mm:697: -[AVAudioSession setActive:withOptions:error:]: Deactivating an audio session that has running I/O. All I/O should be stopped or paused prior to deactivating the audio session.
The problem is that the video file that is playing seems to have an audio that I cannot mute.
I have tried the following:
I removed the audio track from the video and tried playing it in an MPMoviePlayerController
_movie = MPMoviePlayerController() _movie.controlStyle = MPMovieControlStyle.None _movie.view.frame = _movieView.frame _movie.repeatMode = MPMovieRepeatMode.One _movie.backgroundView.backgroundColor = UIColor.whiteColor() _movie.contentURL = NSURL(fileURLWithPath: documentsDirectory.stringByAppendingString("/\(videoName)")) _movie.play()
Every time the movie plays its loop it forces the background audio to dim even though I am not setting AVAudioSession.sharedInstance().setActive(true)
Making my own AVPlayerItem and AVPlayer, and manually muting the audio tracks inside the AVPlayerItem as well as setting the AVPlayer to muted:
var asset = AVURLAsset(URL: _movieURL) var audioTracks = asset.tracksWithMediaType(AVMediaTypeAudio) // Set the volume of ANY potential audio track to 0 var allAudioParams:[AVMutableAudioMixInputParameters] = [] for track:AVAssetTrack in audioTracks { var audioInputParams = AVMutableAudioMixInputParameters() audioInputParams.setVolume(0.0, atTime: kCMTimeZero) audioInputParams.trackID = track.trackID allAudioParams.append(audioInputParams) } var muteTrack = AVMutableAudioMix() muteTrack.inputParameters = allAudioParams var item:AVPlayerItem = AVPlayerItem(URL: _movieURL) // Assign the muted audio track item.audioMix = muteTrack _avPlayer = AVPlayer(playerItem: item) // Also set the AVPlayer to be muted _avPlayer.muted = true let playerLayer = AVPlayerLayer(player: _avPlayer) playerLayer.frame = _movie.view.frame layer.addSublayer(playerLayer) _avPlayer.play() // ALWAYS DIMS BACKGROUND AUDIO!
Again, once the video loops it always dims the background audio. I tried hacking it and doing a restoreBackgroundAudio()
call right after I hit the play button, but I get a bad fade in and out and in and out effect like its trying to restore the background audio but the active video player is forcing the dim as an override.
If you have any suggestions on what else I can do to either actually mute a video thats playing or play a video that doesnt dim the background I would appreciate it.