AVPlayer音量控制 [英] AVPlayer Volume Control

查看:146
本文介绍了AVPlayer音量控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个按钮来静音来自AVPlayer的音频。

I want to create a button that mutes the audio from an AVPlayer.

为什么我不能将.volume用于AVPlayer,只能使用AVAudioPlayer?
还有另一种方法可以改变音量吗?

Why can´t I use .volume with AVPlayer, only with AVAudioPlayer? Is there another way to change the volume?

例如 music.volume = 0.0;

感谢您的回答。

推荐答案

从iOS 7开始,只需致电:

Starting in iOS 7, simply call:

myAvPlayer.volume = 0;

否则,你将不得不使用烦人的 setAudioMix 解决方案。我在我的应用程序中检测到对此的支持,如下所示:

Otherwise, you'll have to use the annoying setAudioMix solution. I'm detecting support for this in my app as follows:

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
 }

这篇关于AVPlayer音量控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆