iOS播放无音频会话的视频 [英] iOS play video without audio session

查看:127
本文介绍了iOS播放无音频会话的视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用MPMoviePlayerControllerAVPlayer在我的应用中播放短视频.问题是(由于我的视频没有声音),我不想干扰后台其他应用程序播放的声音.我尝试玩AVAudioSession:

I am trying to play a short video in my app using either a MPMoviePlayerController or an AVPlayer. The problem is (since my video doesn't have any sound) that I do not want to interfere with the sounds being played by other apps in the background. I tried to play with AVAudioSession:

AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryAmbient  withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil];
[audioSession setActive:YES error:nil];

但是我没有运气.视频开始播放后,背景音乐就会停止.我什至尝试将音频会话设置为非活动状态:

but I had no luck. As soon as the video starts playing, the music in the background stops. I even tryied to set the audio session inactive:

   [[AVAudioSession sharedInstance] setActive:NO withOptions: AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil];

,但在这种情况下,声音会停止半秒钟,然后恢复,视频播放器将停止播放.有什么办法可以实现我想要做的事情?谢谢.

but in this case the sound stops for half a second and then resumes and the video player stops playing. Is there any way I can achieve what I am trying to do? Thanks.

推荐答案

我认为这与您不再相关,但可能与其他人相关.

I assume this is not relevant anymore for you, but can be relevant for somebody else.

没有什么可做的,但是这里有一些解决方法. 关键是,在初始化视频播放器时,请将音频会话类别设置为环境,在这种情况下,它不会中断其他应用程序中的音频会话.然后,如果您需要取消静音"视频,则可以将音频会话类别设置为默认(独奏环境).它将中断其他应用程序中的音频会话,并继续播放带有声音的视频.

There is nothing much to do, but here is some workaround. The point is, when you initialise the video player, set the audio session category to ambient, in this case it will not interrupt audio sessions in other apps. Then, if you will need to "unmute" the video, you can set audio session category to default (solo ambient). It will interrupt audio sessions in other apps and will resume playing the video with sound.

示例:

- (void)initPlayer {

    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient withOptions:0 error:nil];

    // some init logic
    // e.g:
    //
    // _playerItem = [AVPlayerItem playerItemWithAsset:[AVAsset assetWithURL:_URL]];
    // _player = [AVPlayer playerWithPlayerItem:_playerItem];
    // _playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];
    //
    // etc.

}

- (void)setMuted:(BOOL)muted {
    if (!muted) {
        [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategorySoloAmbient withOptions:0 error:nil];
    }

    self.player.muted = muted;
}

P.S.我认为,FB应用程序正在做类似的事情:当视频开始静音播放时,它不会中断其他应用程序的音频,但是当用户按下视频时,它将全屏显示声音,此时该视频将处于活动的音频会话,所有其他应用程序将停止播放音频.

P.S. I assume, FB app is making something similar: when video starts playing muted, it will not interrupt others apps audio, but when user presses on video, it will go fullscreen with sound, at this point there will be active audio session of that video, and all other apps will stop playing audio.

这篇关于iOS播放无音频会话的视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆