有人知道在播放Airplay时调整AVPlayer音轨音量的方法吗?我尝试过AVAudioMix和MPVolumeView但它们都不起作用。我尝试过iOS 5和iOS 6,并使用最新的xcode 4.5.1。这个不起作用的一个简单例子是Apple的AVPlayerTestApp,它使用setVolumeRampFromStartVolume进行简单的淡出。这在设备上工作正常,但如果通过Airplay连接则不行。
在ViewDidLoad中我加载一个曲目并开始播放(这全部来自AVPlayerTestApp)
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setActive:TRUE error:nil];
[session setCategory:AVAudioSessionCategoryPlayback error:nil];
self.mediaItems = [[[MPMediaQuery songsQuery] items] mutableCopy];
NSURL *anUrl = [[mediaItems objectAtIndex: 0] valueForProperty:MPMediaItemPropertyAssetURL];
AVAsset *asset = [AVURLAsset URLAssetWithURL:anUrl options:nil];
AVPlayerItem *myPlayerItem = [AVPlayerItem playerItemWithAsset:asset];
self.myPlayer1 = [[[AVPlayer alloc] initWithPlayerItem:myPlayerItem] retain];
[myPlayer1 play];
然后我有一个按钮,打开一个警报视图,允许用户设置音量并开启播放
MPVolumeView *volumeView = [[[MPVolumeView alloc] initWithFrame: CGRectMake(10, 37, 260, 20)] autorelease];
UIAlertView *volumeAlert = [[UIAlertView alloc] initWithTitle:@"Volume" message:@"" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[volumeView sizeToFit];
[volumeAlert addSubview:volumeView];
[volumeAlert show];
[volumeAlert release];
然后是另一个淡出当前播放曲目的按钮
AVAsset *asset = [myPlayer1.currentItem asset];
NSArray *keys = [NSArray arrayWithObject:@"tracks"];
[asset loadValuesAsynchronouslyForKeys:keys completionHandler:^(void) {
NSError *error = nil;
AVKeyValueStatus trackStatus = [asset statusOfValueForKey:@"tracks" error:&error];
CMTime currentTime;
switch (trackStatus) {
case AVKeyValueStatusLoaded:
if(myPlayer1)
{
NSArray *tracks = [asset tracksWithMediaType:AVMediaTypeAudio];
NSMutableArray * allAudioParams = [NSMutableArray array];
for (AVAssetTrack *t in tracks) {
AVMutableAudioMixInputParameters *params =[AVMutableAudioMixInputParameters audioMixInputParameters];
float fadeOutSeconds = 5;
currentTime = [myPlayer1 currentTime];
[params setVolumeRampFromStartVolume: 1.0 toEndVolume: 0.0 timeRange: CMTimeRangeMake(currentTime, CMTimeMakeWithSeconds(fadeOutSeconds, 1))];
[params setTrackID:[t trackID]];
[allAudioParams addObject:params];
}
AVMutableAudioMix * zeromix = [AVMutableAudioMix audioMix];
[zeromix setInputParameters:allAudioParams];
[myPlayer1.currentItem setAudioMix:zeromix];
}
break;
case AVKeyValueStatusFailed:
// error occured loading AVAsset
NSLog(@"error occured loading AVAsset");
break;
case AVKeyValueStatusCancelled:
// loading of the AVAsset was cancelled
NSLog(@"loading of the AVAsset was cancelled");
break;
default:
break;
}
}];
这在设备上按预期工作,并在点按按钮时淡出音量。但是,如果打开Airplay,则音量变化无法通过。使用MPMoviePlayController我可以自己淡化,这可以在Airplay上工作但是AVPlayer比Airplay的延迟更少,所以我宁愿使用它。
任何帮助都非常感激。
答案 0 :(得分:1)
由于Sam_899没有发表答案,我将不得不这样做。据我所知,除非在打开镜像的情况下进行连接,否则无法通过airplay控制avplayer的音量。作为启用镜像的唯一方法是从应用程序外部,唯一的选择是通知用户如何自己打开它并希望他们对它感到满意!对于那些不知道如何通过airplay开启镜像的人;双击主页按钮,向左滑动到音量控制,点击播放图标,选择要连接的airplay设备然后切换镜像。