2014-07-17 479 views

回答

1

這將是最簡單的方法。

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

不幸的是,我不想刪除UIWebview的內容。 – fcortes

+0

self.webView.allowsInlineMediaPlayback = YES; self.webView.mediaPlaybackRequiresUserAction = NO; – Bejibun

+0

您還可以添加AVAudioPlayer框架並將音量設置爲0. http://mobileorchard.com/easy-audio-playback-with-avaudioplayer/(教程) – Bejibun

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; 

} 
+0

此代碼的迅捷版本? – niks290192