2 yue1liang1chuan yue1liang1chuan 于 2013.04.07 13:33 提问

AVPlayer控制音量播放

开发一个自定义播放,用到AVPlayer类,现在想加入UISlider控制音量。

实现代码:

NSMutableArray *allAudioParams = [NSMutableArray array];
AVMutableAudioMixInputParameters *audioInputParams =[AVMutableAudioMixInputParameters audioMixInputParameters];
[audioInputParams setVolume:volumeSlider.value atTime:kCMTimeZero];
[allAudioParams addObject:audioInputParams];
AVMutableAudioMix *audioZeroMix = [AVMutableAudioMix audioMix];
[audioZeroMix setInputParameters:allAudioParams];
[self.playerItem setAudioMix:audioZeroMix];

步骤地为什么不好使。请大家帮忙,谢谢。

2个回答

MrsSeven7
MrsSeven7   2013.04.07 14:01
已采纳
NSArray *audioTracks = [asset tracksWithMediaType:AVMediaTypeAudio];

NSMutableArray *allAudioParams = [NSMutableArray array];
for (AVAssetTrack *track in audioTracks) {
  AVMutableAudioMixInputParameters *audioInputParams = 
    [AVMutableAudioMixInputParameters audioMixInputParameters];
  [audioInputParams setVolume:volume atTime:kCMTimeZero];
  [audioInputParams setTrackID:[track trackID]];
  [allAudioParams addObject:audioInputParams];
}

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

[playerItem setAudioMix:audioMix];

用这段代码控制音量

loveiflower
loveiflower   2013.04.07 14:10
-(void)doVolumeFade
{  
    if (self.player.volume > 0.1) {
        self.player.volume = self.player.volume - 0.1;
        [self performSelector:@selector(doVolumeFade) withObject:nil afterDelay:0.1];       
     } else {
        // Stop and get the sound ready for playing again
        [self.player stop];
        self.player.currentTime = 0;
        [self.player prepareToPlay];
        self.player.volume = 1.0;
    }
}
Csdn user default icon
上传中...
上传图片
插入图片