yue1liang1chuan
2013-04-07 05:33
采纳率: 69.8%
浏览 7.6k

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 2013-04-07 06: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 2013-04-07 06: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;
        }
    }
    
    打赏 评论

相关推荐 更多相似问题