[英]AVPlayer removes background music
I've been using giffycat to decode, store, and play gifs in my app. 我一直在使用giffycat在我的应用程序中解码,存储和播放GIF。 I am making it so that it can easily load a gif in a UICollectionView's cell, so I have decided for each gif model to have its own AVPlayer. 我正在制作它,以便它可以轻松地在UICollectionView的单元格中加载一个gif,所以我决定让每个gif模型都拥有自己的AVPlayer。 I have noticed that simply by creating an AVPlayer, shown bellow, audio from other apps is killed! 我注意到,只需创建一个AVPlayer,如下所示,来自其他应用程序的音频就会被杀死! Annoying for both the user and the creater! 对用户和创作者都很烦人!
// Create an AVURLAsset with an NSURL containing the path to the video
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:[NSURL URLWithString:_mp4] options:nil];
// Create an AVPlayerItem using the asset
AVPlayerItem *item = [AVPlayerItem playerItemWithAsset:asset];
_player = [AVPlayer playerWithPlayerItem:item]; //if this line is commented out, I hear audio, else audio from Spotify is quickly killed...
Since these videos are just gifs, I am wondering if there is some way to unassign the audio session. 由于这些视频只是GIF,我想知道是否有某种方法可以取消分配音频会话。 I do not know much about this. 我对此并不了解。 ples help! ples帮忙!
Turns out the answer is pretty easy, after a little googling and documentation reading... The solution is 事实证明,答案很简单,经过一番谷歌搜索和文档阅读...解决方案是
// audio session
let audioSession = AVAudioSession.sharedInstance()
try! audioSession.setCategory(AVAudioSessionCategoryAmbient,
withOptions: AVAudioSessionCategoryOptions.MixWithOthers)
oops, just realized I am posting my question in objC and answer in Swift. 哎呀,刚刚意识到我在objC中发布我的问题并在Swift中回答。 Well tough, because that's life sometimes. 很难,因为这有时候是生活。
AudioSession is a singleton for your entire app to rule how your application mingles with the other sounds of the system and other apps! AudioSession是整个应用程序的单例,可以统治您的应用程序如何与系统和其他应用程序的其他声音混合! The default audio session is 默认音频会话是
CategoryAmbient tells it not to do 4 CategoryAmbient告诉它不要做4
Nice documentation! 好文档! https://developer.apple.com/library/ios/documentation/Audio/Conceptual/AudioSessionProgrammingGuide/ConfiguringanAudioSession/ConfiguringanAudioSession.html#//apple_ref/doc/uid/TP40007875-CH2-SW1 https://developer.apple.com/library/ios/documentation/Audio/Conceptual/AudioSessionProgrammingGuide/ConfiguringanAudioSession/ConfiguringanAudioSession.html#//apple_ref/doc/uid/TP40007875-CH2-SW1
在从中创建AVPlayer之前,将AVPlayerItem的audioMix属性设置为nil,以从资产中删除音轨。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.