Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I mute for youtube-ios-player-helper?

I'm using youtube-ios-player-helper to play youtube video in my application. I'd like to be able to mute. Here is what I did:

  1. Add setVolume() and mute() function in YTPlayerView

    -(void)setVolume:(int)volume {
    
        if ( volume < 0 )
            volume = 0;
        else if ( volume > 100 )
            volume = 100;
        NSString *command = [NSString stringWithFormat:@"player.setVolume(%d);", volume];
        [self stringFromEvaluatingJavaScript:command];
    }
    
    -(void)mute {        
        NSString *command = [NSString stringWithFormat:@"player.mute();"];
        [self stringFromEvaluatingJavaScript:command];
    }
    

Then I call setVolume:0 or mute functions in my app. But the sound can't be turned off. The volume is still 100.

Did anybody turn off the sound successfully?

like image 342
Bagusflyer Avatar asked Mar 20 '15 06:03

Bagusflyer


1 Answers

Following the above I got it working in Swift4:

func playerView(_ playerView: YTPlayerView, didChangeTo state: YTPlayerState) {
    if case .playing = state {
        playerView.webView?.stringByEvaluatingJavaScript(from: "player.mute();")
    }
}
like image 62
Aleksander Niedziolko Avatar answered Oct 13 '22 04:10

Aleksander Niedziolko