2015-08-05 14 views
10

Ho un AVPlayer con AVPlayerItem. Quello che voglio è disattivare la riproduzione audio da AVPlayer. Voglio giocare solo video.Disattiva la riproduzione audio di AVPlayer?

Qualcuno può aiutarmi? Grazie!

self.avPlayerItem = [AVPlayerItem playerItemWithURL:self.videoUrl]; 
    self.avPlayer = [AVPlayer playerWithPlayerItem:self.avPlayerItem]; 
    [self.avPlayer play]; 
    self.avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone; 


    self.avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:self.avPlayer]; 
    self.avPlayerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; 

    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(playerItemDidPlayToEndTime:) 
               name:AVPlayerItemDidPlayToEndTimeNotification 
               object:self.avPlayerItem]; 


    CGRect screenRect = [[UIScreen mainScreen] bounds]; 

    self.avPlayerLayer.frame = CGRectMake(0, 0, screenRect.size.width , screenRect.size.height); 
    [self.view.layer insertSublayer:self.avPlayerLayer atIndex:0]; 

risposta

11

AVPlayer hanno opzione

@property (nonatomic, getter=isMuted) BOOL muted NS_AVAILABLE(10_7, 7_0); 

È possibile scrivere

- (void) muteSound:(BOOL)mute 
{ 
    self.avPlayer.muted = mute; 
} 

E usarlo, come si desidera

- (void) startPlayingVideo 
{ 

    [self muteSound:YES]; 

    //other code 

} 
0

È possibile disattivare l'audio mediante l'attuazione di codice seguente in viewDidLoad() .

AVURLAsset *asset = [AVURLAsset URLAssetWithURL:[self myAssetURL] 
options:nil]; 
NSArray *audioTracks = [asset tracksWithMediaType:AVMediaTypeAudio]; 

// Mute all the audio 
tracksNSMutableArray *allAudioParams = [NSMutableArray array]; 

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

[audioInputParams setVolume:0.0 atTime:kCMTimeZero]; 

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

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

I seguenti collegamenti possono aiutarti.

  1. https://goo.gl/WYJNUF
  2. https://goo.gl/epHNGs
0

Nel caso in cui qualcuno è alla ricerca di Swift 4:

player.isMuted = true // To mute the sound 
player.isMuted = false // To un-mute the sound 

Nota a margine: muting il video non ripristina l'audio del video per iniziare. Funziona come una semplice funzione di silenziamento.

Problemi correlati