调整播放AVPlayer的音量 [英] Adjusting the volume of a playing AVPlayer

查看:731
本文介绍了调整播放AVPlayer的音量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在当前播放AVPlayer项目上获取音量变量的唯一方法是遵循此过程;

The only method for getting a volume change on currently playing AVPlayer items is to follow this process;


  • 卸载当前播放的AVPlayerItem的资产

  • 获取该AVPlayerItem的当前播放时间

  • 将资源加载到新的AVPlayerItem

  • 替换新的当前AVPlayerItem

  • 等待AVPlayer上的currentItem更改

  • 准备AudioMix并寻找之前的播放时间

  • Offload the currently playing AVPlayerItem's asset
  • Grab the current playback time for that AVPlayerItem
  • Load the asset into a new AVPlayerItem
  • Replace the current AVPlayerItem with the new one
  • Wait for the currentItem to change on the AVPlayer
  • Prepare your AudioMix and seek to previous playback time

我是否错过了某个地方的基本原则,或者只是为了简单地管理音量水平而感到错综复杂?

Have I missed a basic principle somewhere or is it meant to be this convoluted to simply manage volume levels?

我不能使用AVAudioPlayer,因为我需要将iTunes曲目加载到播放器中。

I cannot use an AVAudioPlayer because I need to load iTunes tracks into the player.

推荐答案

你可以改变使用此处描述的方法进行游戏时的音量:

You can change the volume while playing using the method described here:

http:// develo per.apple.com/library/ios/#qa/qa1716/_index.html

虽然文章的文字似乎表明它只能用于静音音频,您实际上可以将音量设置为您喜欢的任何音量,您可以在音频播放后进行设置。例如,假设您的AVAsset实例称为资产,您的AVPlayerItem实例称为playerItem,并且您要设置的卷称为volume,以下代码应该执行您想要的操作:

While the text of the article seems to suggest that it can only be used to mute the audio, you can actually set the volume to anything you like, and you can set it after the audio is playing. For example, assuming your instance of AVAsset is called "asset", your instance of AVPlayerItem is called "playerItem", and the volume you want to set is called "volume", the following code should do what you want:

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];

这篇关于调整播放AVPlayer的音量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆