4

我有 AVPlayer 问题。

1.如何控制它的音量?

2.如何知道AVPlayer是否因为连接不良而重新加载音乐,我是否有一些迹象?

4

1 回答 1

7

AVPlayer使用系统音量,因此如果您需要为此提供控件,您可以使用MPVolumeView它为您提供音量控制滑块。

对于音频衰减,您可以使用AVAudioMix. 这是一些代码:

//given an AVAsset called asset...
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:asset];
id audioMix = [[AVAudioMix alloc] init];
id volumeMixInput = [[AVMutableAudioMixInputParameters alloc] init];

//fade volume from muted to full over a period of 3 seconds
[volumeMixInput setVolumeRampFromStartVolume:0 toEndVolume:1 timeRange:
     CMTimeRangeMake(CMTimeMakeWithSeconds(0, 1), CMTimeMakeWithSeconds(3, 1))];
[volumeMixnput setTrackID:[[asset tracks:objectAtIndex:0] trackID]];

[audioMix setInputParameters:[NSArray arrayWithObject:volumeMixInput]];
[playerItem setAudioMix:audioMix];

您还可以在给定时间突然设置混音的音量:

[volumeMixInput setVolume:.5 atTime:CMTimeMakeWithSeconds(15, 1)];

希望这可以帮助。这个 API 肯定不明显。我强烈推荐观看名为Discovering AV Foundation的 WWDC 10 视频。太棒了。

于 2011-06-24T20:58:49.567 回答