我正在使用 AVPlayer 播放广播流。 为了初始化播放器,我正在使用下一个代码:
self.asset = [AVAsset assetWithURL:self.url];
self.item = [AVPlayerItem playerItemWithAsset:self.asset];
self.player = [AVPlayer playerWithPlayerItem:self.item];
我读过这个问题 Adjusting the volume of a playing AVPlayer
我正在尝试使用 slider 值设置音量
- (void)setVolume:(float )volume
{
_volume = volume;
NSArray *audioTracks = self.asset.tracks;
NSMutableArray *allAudioParams = [NSMutableArray array];
for (AVAssetTrack *track in audioTracks) {
AVMutableAudioMixInputParameters *audioInputParams =
[AVMutableAudioMixInputParameters audioMixInputParameters];
[audioInputParams setVolume:volume atTime:kCMTimeZero];
[allAudioParams addObject:audioInputParams];
[audioInputParams setTrackID:[track trackID]];
}
AVMutableAudioMix *audioMix = [AVMutableAudioMix audioMix];
[audioMix setInputParameters:allAudioParams];
NSLog(@"%@", self.player.currentItem);
NSLog(@"%f", volume);
[self.player.currentItem setAudioMix:audioMix];
}
我也在尝试设置
[audioInputParams setVolume:volume atTime:self.player.currentTime];
但该数组 (self.asset.tracks) 中没有任何轨道, 并且它对音量没有任何影响。 (我也无法获得任何元数据), 我不能使用 AVAudioPlayer 通过 url 播放音频流
An instance of the AVAudioPlayer class, called an audio player, provides playback of audio data from a file or memory.
Apple recommends that you use this class for audio playback unless you are playing audio captured from a network stream or require very low I/O latency.
我正在考虑使用该代码 https://github.com/DigitalDJ/AudioStreamer/downloads 用于播放广播流,但我不知道如何使用 Audiotoolbox 设置音量。
请帮帮我!谢谢!
最佳答案
我已经尝试了很多解决方案,但最好的方法是实现一个实例 ov MPVolumeVIew:
self.myViewVolume = [[MPVolumeView alloc] initWithFrame:CGRectMake(20, 330, 280, 50)];
[self.myViewVolume sizeToFit];
[self.view addSubview:self.myViewVolume];
你可以将它添加到你想要的任何 UIView,只需将类更改为 MPVolumeView 不要忘记添加 MediaPlayer 框架。
如果您想从流中获取元数据,则必须执行以下操作:
[playerItem addObserver:self forKeyPath:@"timedMetadata" options:NSKeyValueObservingOptionNew context:NULL];
有了这个,你可以简单地将信息添加到任何 UILabel
关于ios - 播放广播流时如何设置AVPLayer的音量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13555731/