繁体   English   中英

AVPlayer删除背景音乐

[英]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是整个应用程序的单例,可以统治您的应用程序如何与系统和其他应用程序的其他声音混合! 默认音频会话是

  1. 播放已启用,录制已禁用
  2. 当用户将静音开关移至“静音”时,您的音频会被静音
  3. 当用户按下睡眠/唤醒按钮锁定屏幕或自动锁定时间到期时,您的音频将被静音
  4. 当您的音频开始时,设备上的其他音频(音乐)将被静音。

CategoryAmbient告诉它不要做4

好文档! 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM