Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/98.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS在没有音频会话的情况下播放视频_Ios_Mpmovieplayercontroller_Avplayer_Avaudiosession - Fatal编程技术网

iOS在没有音频会话的情况下播放视频

iOS在没有音频会话的情况下播放视频,ios,mpmovieplayercontroller,avplayer,avaudiosession,Ios,Mpmovieplayercontroller,Avplayer,Avaudiosession,我正在尝试使用MPMoviePlayerController或AVPlayer在我的应用程序中播放短视频。问题是(因为我的视频没有任何声音),我不想干扰其他应用程序在后台播放的声音。我试着玩AVAudioSession: AVAudioSession *audioSession = [AVAudioSession sharedInstance]; [audioSession setCategory:AVAudioSessionCategoryAmbient withOptions:AVAudi

我正在尝试使用
MPMoviePlayerController
AVPlayer
在我的应用程序中播放短视频。问题是(因为我的视频没有任何声音),我不想干扰其他应用程序在后台播放的声音。我试着玩
AVAudioSession

AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryAmbient  withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil];
[audioSession setActive:YES error:nil];
但是我没有运气。视频一开始播放,背景中的音乐就停止了。我甚至尝试将音频会话设置为非活动状态:

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

但在这种情况下,声音会停止半秒钟,然后恢复,视频播放器停止播放。我有什么办法可以达到我想做的吗?谢谢。

您正在使用音乐bkg应用程序进行测试吗? 如果不是,那么答案可能是大多数音乐应用程序都包含:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(handleAudioSessionInterruption:)
                                             name:AVAudioSessionInterruptionNotification
                                           object:aSession];
和实施,如:

- (void) handleAudioSessionInterruption:(NSNotification*)notification
{
    NSNumber *interruptionType = [[notification userInfo] objectForKey:AVAudioSessionInterruptionTypeKey];
   .....code....

    switch (interruptionType.unsignedIntegerValue) {
        case AVAudioSessionInterruptionTypeBegan:{
            // stop playing
        } break;
        case AVAudioSessionInterruptionTypeEnded:{
            // continue playing
        } break;
        default:
            break;
    }
}

因此,他们停止播放,并在中断结束后开始播放。(对于来电等)

我认为这对你来说不再重要,但对其他人来说可能重要

没有什么要做的,但这里有一些解决方法。 关键是,当您初始化视频播放器时,将音频会话类别设置为ambient,在这种情况下,它不会中断其他应用程序中的音频会话。然后,如果您需要“取消静音”视频,您可以将音频会话类别设置为默认(solo ambient)。它将中断其他应用程序中的音频会话,并恢复播放带有声音的视频

例如:

- (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;
}

另外,我假设FB应用程序也在做类似的事情:当视频开始播放静音时,它不会中断其他应用程序的音频,但当用户按下视频时,它会全屏播放声音,此时该视频将有活动音频会话,所有其他应用程序将停止播放音频。

回答得好!对于感兴趣的人来说,这是一个Swift 5转换

try? AVAudioSession.sharedInstance().setCategory(.ambient)

您的视频中是否只有视频编解码器?检查视频文件的信息。(应该是唯一的编解码器:例如H.264,但不是H.264 AAC)唯一的编解码器是H.264请标记正确答案谢谢!正是我刚才在找的!