我可以以某种方式将UIWebview静音/取消静音吗?

时间:2014-07-17 13:32:20

标签: ios audio uiwebview

我有一个生成声音的UIWebView,但我想删除它。如何在UIWebView

中静音音频输出

2 个答案:

答案 0 :(得分:3)

我找到了将单个播放器UIWebView静音/取消静音的方法。任何时候UIWebView启动视频AVPlayerItemBecameCurrentNotification通知被触发。因此,处理它并跟踪当前的AVPlayerItem以控制播放。

-(void)viewDidLoad
{
    …

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerItemBecameCurrent:)
                                                 name:@"AVPlayerItemBecameCurrentNotification"
                                               object:nil];
}

-(void)playerItemBecameCurrent:(NSNotification*)notification
{
    if ([notification.object isKindOfClass:[AVPlayerItem class]]) {
        self.currentItem = (AVPlayerItem*)notification.object;
    }
}

-(void)muteSound:(BOOL)mute
{   
    AVAsset *asset = self.currentItem.asset;
    NSArray *audioTracks = [asset tracksWithMediaType:AVMediaTypeAudio];

    NSMutableArray *allAudioParams = [NSMutableArray array];

    for (AVAssetTrack *track in audioTracks)
    {
        AVMutableAudioMixInputParameters *audioInputParams = [AVMutableAudioMixInputParameters audioMixInputParameters];

        [audioInputParams setVolume:!mute atTime:kCMTimeZero];

        [audioInputParams setTrackID:[track trackID]];
        [allAudioParams addObject:audioInputParams];
    }

    AVMutableAudioMix *audioZeroMix = [AVMutableAudioMix audioMix];
    [audioZeroMix setInputParameters:allAudioParams];

    self.currentItem.audioMix = audioZeroMix;

}

答案 1 :(得分:1)

这将是最简单的方法。

-(void)stopAudio
{
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"" ]]];
}