2013-03-22 9 views
6

Chcę utworzyć przycisk, który wycisza dźwięk z odtwarzacza AVPlayer.Kontrola głośności AVPlayer

Dlaczego nie mogę używać .volume z AVPlayer, tylko z AVAudioPlayer? Czy jest inny sposób na zmianę głośności?

np music.volume = 0.0;

Dzięki za odpowiedzi.

Odpowiedz

11

Uruchamianie w iOS 7, po prostu c wszystkich:

myAvPlayer.volume = 0; 

W przeciwnym razie będziesz musiał użyć przykry setAudioMix rozwiązanie. Ja wykrywania poparcie dla tego w mojej aplikacji w następujący sposób:

if ([mPlayer respondsToSelector:@selector(setVolume:)]) { 
    mPlayer.volume = 0.0; 
} else { 
    NSArray *audioTracks = mPlayerItem.asset.tracks; 

    // Mute all the audio tracks 
    NSMutableArray *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]; 

    [mPlayerItem setAudioMix:audioZeroMix]; // Mute the player item 
} 
+2

Dzięki Doug! Szukałem tego przez kilka godzin. Na szczęście jest to ustawienie jednej własności na AVPLayer: D – Mapedd

+2

Doskonała, prosta, piękna odpowiedź! –

+0

@doug, dlaczego zmieniłeś to na "1.0" - tylko literówka? – CupawnTae

3

Użyłem poniższego kodu do wyciszenia AVPlayera.

float volSet = 0 ; 
AVAsset *avAsset = [[avPlayer currentItem] asset] ; 
NSArray *audioTracks = [avAsset tracksWithMediaType:AVMediaTypeAudio] ; 

NSMutableArray *allAudioParams = [NSMutableArray array] ; 
for(AVAssetTrack *track in audioTracks){ 
AVMutableAudioMixInputParameters *audioInputParams = [AVMutableAudioMixInputParameters audioMixInputParameters] ; 
[audioInputParams setVolume:volSet atTime:kCMTimeZero] ; 
[audioInputParams setTrackID:[track trackID]] ; 
[allAudioParams addObject:audioInputParams]; 
} 
AVMutableAudioMix *audioVolMix = [AVMutableAudioMix audioMix] ; 
[audioVolMix setInputParameters:allAudioParams]; 
[[avPlayer currentItem] setAudioMix:audioVolMix]; 
+0

to działa: [[MPMusicPlayerController applicationMusicPlayer] setVolume: 0.0 ]; –

+0

i to nie jest AVPlayer, chyba ... – BhushanVU

+0

nadal działa, ale ustawia głośność systemu – kezi

2

najlepszym sposobem jest następujący

AVAssetTrack* assetTrackAudio = [arrayTracks objectAtIndex:0]; 

    AVMutableAudioMixInputParameters* audioInputParams = [AVMutableAudioMixInputParameters audioMixInputParameters]; 
    [audioInputParams setVolume:currentVolume atTime:playerCursorStartPosition]; 
    [audioInputParams setTrackID:[assetTrackAudio trackID]]; 

    NSArray* audioParams = [NSArray arrayWithObject:audioInputParams]; 
    AVMutableAudioMix* audioMix = [AVMutableAudioMix audioMix]; 
    [audioMix setInputParameters:audioParams]; 

    AVPlayerItem* item = [player currentItem]; 
    [item setAudioMix:audioMix]; 
+0

Myślę, że działa tylko z AVAudioPlayer. Kiedy używam tego z AVPlayer nic się nie dzieje. –

+0

nie działa również z AVPlayer. Zrobiłem to przed –

+1

@ user2196602: To na pewno działa z AVPlayer. Sprawdź [dokumentacja Apple] (http://developer.apple.com/library/ios/#qa/qa1716/_index.html) – execv

0

Sprawdź ten link,

apple documentation for muting audio

Poniżej znajduje się kod

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

// Mute all the audio tracks 
NSMutableArray *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]; 

// Create a player item 
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:asset]; 
[playerItem setAudioMix:audioZeroMix]; // Mute the player item 

// Create a new Player, and set the player to use the player item 
// with the muted audio mix 
AVPlayer *player = [AVPlayer playerWithPlayerItem:playerItem]; 

// assign player object to an instance variable 
self.mPlayer = player; 

// play the muted audio 
[mPlayer play]; 
0

Jako iOS 7 jest też muted nieruchomość na AVPlayer:

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