[英]AVPlayer removes background music
我一直在使用giffycat在我的应用程序中解码,存储和播放GIF。 我正在制作它,以便它可以轻松地在UICollectionView的单元格中加载一个gif,所以我决定让每个gif模型都拥有自己的AVPlayer。 我注意到,只需创建一个AVPlayer,如下所示,来自其他应用程序的音频就会被杀死! 对用户和创作者都很烦人!
// 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...
由于这些视频只是GIF,我想知道是否有某种方法可以取消分配音频会话。 我对此并不了解。 ples帮忙!
事实证明,答案很简单,经过一番谷歌搜索和文档阅读...解决方案是
// audio session
let audioSession = AVAudioSession.sharedInstance()
try! audioSession.setCategory(AVAudioSessionCategoryAmbient,
withOptions: AVAudioSessionCategoryOptions.MixWithOthers)
哎呀,刚刚意识到我在objC中发布我的问题并在Swift中回答。 很难,因为这有时候是生活。
AudioSession是整个应用程序的单例,可以统治您的应用程序如何与系统和其他应用程序的其他声音混合! 默认音频会话是
CategoryAmbient告诉它不要做4
在从中创建AVPlayer之前,将AVPlayerItem的audioMix属性设置为nil,以从资产中删除音轨。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.