yue1liang1chuan
yue1liang1chuan
采纳率69.8%
2013-04-07 05: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 8年前
    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];
    

    用这段代码控制音量

    点赞 1 评论 复制链接分享
  • loveiflower loveiflower 8年前
    -(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;
        }
    }
    
    点赞 评论 复制链接分享