当MPMoviePlayer播放时,AVAudioPlayer播放静音模式



问题是AVAudioPlayer本身的行为应该是这样的。当设备处于非静音模式(静音/铃声开关打开)时,它会播放背景音乐。当静音开关打开时,也尊重此状态,不播放背景音乐。

然而,AVAudioPlayer在静音模式下播放的一个特殊情况是当我在MPMoviePlayer中播放视频时。

在我的应用中,在播放电影之前会先播放BG的声音。

有人能帮忙吗?这只发生在我播放电影时。

其他详细信息:
使用的BG回路- m4a型
所用影片类型- m4v

我也发现这种情况在iOS 5中反复出现,但即使在最新版本中有时也会发生。

我在Google, Apple文档和SO中到处搜索,但我找不到任何关于这个特殊场景的内容。

我甚至尝试显式设置AVAudioSession的类别AVAudioSessionCategoryAmbient,然后调用setActive。(也尝试了setCategory:withOptions:error方法,但应用程序因某种原因崩溃-在设备上测试)

当我尝试使用AVAudioPlayerDelegate时,我甚至尝试设置委托和实现方法。

帮助任何人吗?(

NSURL *url = [NSURL URLWithString:self.videoURL];
            MPMoviePlayerViewController *mpvc = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
            NSError *_error = nil;
            [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: &_error];
            [self presentMoviePlayerViewControllerAnimated:mpvc];

添加这个框架

#import <AVFoundation/AVFoundation.h>

尝试用这种方式设置音频会话

AVAudioSession *audioSession = [AVAudioSession sharedInstance];
        NSError *err = nil;
        [audioSession setCategory :AVAudioSessionCategoryPlayAndRecord error:&err];
        if(err){
            NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
            return;
        }
        [audioSession setActive:YES error:&err];
        err = nil;
        if(err){
            NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
            return;
        }

最新更新