【转】iOS:AvPlayer设置播放速度不生效的解决办法

现象:

项目有一个需求是实现视频的慢速播放,使用的是封装的AvPlayer,但是设置时发现比如设置rate为0.5,0.1,0.01都是一样的速度,非常疑惑.后来经过查找资料,发现iOS
10对这个API进行了更新

iOS10之前官方API对AvPlayer rate属性的解释是
/*! 
@property  rate
@abstract  Changes the playback rate of the input signal
@discussion A value of 2.0 results in the output audio playing one octave higher.A value of 0.5, results in the output audio playing one octave lower. Range: 0.5 -> 2.0 Default: 1.0 Mixer: AVAudioEnvironmentNode */

由上,我们可以发现这个rate只是支持0.5-2倍的

iOS10更新后对AvPlayer rate属性的解释是
/*!
 @property  rate
 @abstract  Indicates the desired rate of playback; 0.0 means "paused", 1.0 indicates a desire to play at the natural rate of the current item. @discussion Setting the value of rate to 0.0 pauses playback, causing the value of timeControlStatus to change to AVPlayerTimeControlStatusPaused. Setting the rate to a non-zero value causes the value of timeControlStatus to become either AVPlayerTimeControlStatusWaitingToPlayAtSpecifiedRate or AVPlayerTimeControlStatusPlaying, depending on whether sufficient media data has been buffered for playback to occur and whether the player's default behavior of waiting in order to minimize stalling is permitted. See discussion of AVPlayerTimeControlStatus for more details. AVPlayer can reset the desired rate to 0.0 when a change in overall state requires playback to be halted, such as when an interruption occurs on iOS, as announced by AVAudioSession, or when the playback buffer becomes empty and playback stalls while automaticallyWaitsToMinimizeStalling is NO. The effective rate of playback may differ from the desired rate even while timeControlStatus is AVPlayerTimeControlStatusPlaying, if the processing algorithm in use for managing audio pitch requires quantization of playback rate. For information about quantization of rates for audio processing, see AVAudioProcessingSettings.h. You can always obtain the effective rate of playback from the currentItem's timebase; see the timebase property of AVPlayerItem. */

在代码中设置小于0.5的值一直不生效,查找资料,好像只是支持

I actually had a ticket with Apple DTS open for this issue and a bug filed. The only supported values are 0.50, 0.67, 0.80, 1.0, 1.25, 1.50, and 2.0. All other settings are rounded to nearest value.

详见 解决办法

这里面有人提供了这样一种办法:

I found that smaller values are indeed supported, but all tracks in the AVPlayerItem have to support the speed. However, Apple does not provide a property on individual tracks that would indicate what rates are supported, there is only the property canPlaySlowForward on AVPlayerItem.

What i found is, that AVPlayerItems with an audio track cannot play at rates slower than 0.5. However, if there is only a video track, the rate can have an arbitrary small value like 0.01. I will try to write a category that checks on-the-fly which values are supported and disable unsupported tracks if needed.

//I wrote a function which you can call whenever you want to set the rate for video below 0.5. It enables/disables all audio tracks.
- (void)enableAudioTracks:(BOOL)enable inPlayerItem:(AVPlayerItem*)playerItem
{
    for (AVPlayerItemTrack *track in playerItem.tracks) { if ([track.assetTrack.mediaType isEqual:AVMediaTypeAudio]) { track.enabled = enable; } } }

我使用此方法解决了我的问题,非常感谢!!!!!特此记录,备忘!

from:http://www.jianshu.com/p/555cc5b88f6b

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用AVAudioRecorder和AVPlayer来实现在iOS设备上边录音边播放。具体步骤如下: 1. 配置AVAudioSession,设置播放和录音的类别和模式: ```swift let session = AVAudioSession.sharedInstance() try? session.setCategory(.playAndRecord, mode: .default, options: [.defaultToSpeaker, .allowBluetoothA2DP]) try? session.setActive(true) ``` 2. 创建AVAudioRecorder实例,设置录音参数和文件路径: ```swift let recordSettings = [ AVFormatIDKey: Int(kAudioFormatMPEG4AAC), AVSampleRateKey: 44100, AVNumberOfChannelsKey: 1, AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue ] let audioURL = URL(fileURLWithPath: NSTemporaryDirectory() + "recording.m4a") let audioRecorder = try? AVAudioRecorder(url: audioURL, settings: recordSettings) audioRecorder?.prepareToRecord() audioRecorder?.record() ``` 3. 创建AVPlayer实例,设置播放URL和缓存: ```swift let audioURL = URL(string: "http://example.com/audio.mp3")! let playerItem = AVPlayerItem(url: audioURL) let player = AVPlayer(playerItem: playerItem) player.automaticallyWaitsToMinimizeStalling = false player.play() ``` 4. 在AVPlayer的代理方法中,检查缓存是否够用,如果不够用就暂停AVPlayer,等待缓存: ```swift func player(_ player: AVPlayer, didOutputCueMarkers markers: [AVMetadataItem], at time: CMTime) { let currentTime = player.currentTime() let bufferDuration = CMTimeGetSeconds(player.currentItem?.loadedTimeRanges.first?.timeRange.duration ?? CMTime.zero) let playbackDuration = CMTimeGetSeconds(currentTime) if bufferDuration - playbackDuration < 5 { player.pause() } else { player.play() } } ``` 这样就可以在iOS设备上边录音边播放了。注意,由于网络传输和设备性能的限制,可能会出现播放卡顿或录音失真的情况。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值