Adjusting the volume of a playing AVPlayer(调整播放 AVPlayer 的音量)
问题描述
在当前播放的 AVPlayer 项目上获得音量变化的唯一方法是遵循这个过程;
The only method for getting a volume change on currently playing AVPlayer items is to follow this process;
- 卸载当前播放的 AVPlayerItem 的资源
- 获取该 AVPlayerItem 的当前播放时间
- 将资源加载到新的 AVPlayerItem
- 用新的 AVPlayerItem 替换当前的 AVPlayerItem
- 等待 AVPlayer 上的 currentItem 改变
- 准备您的 AudioMix 并寻找之前的播放时间
我是否在某个地方遗漏了一个基本原则,或者它是否意味着为了简单地管理音量级别而如此复杂?
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://developer.apple.com/library/ios/#qa/qa1716/_index.html
虽然文章的文字似乎暗示它只能用于静音,但您实际上可以将音量设置为您喜欢的任何值,并且可以在播放音频后进行设置.例如,假设您的 AVAsset 实例称为asset",您的 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 的音量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:调整播放 AVPlayer 的音量


基础教程推荐
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01