ios - AVPlayer 从 url 加载缓慢
问题描述
我已经创建了 AVQueuePlayer 来加载音频。来源来自音频文件。说到执行,我发现播放下一个视频需要 6-7 秒。你能告诉我优化下载时间和最小化加载下一个音频的等待时间的方法吗?
这是我的代码:
let url = URL(string: (audio?.audio_path)!)
let playerItem:AVPlayerItem = AVPlayerItem(url: url!)
解决方案
我已经在这个线程中回答了一个关于加载缓慢的问题。
AVPlayer
具有功能(来自 iOS 10+),您可以试用。我自己使用它,一切正常。
/*!
@method playImmediatelyAtRate:
@abstract Immediately plays the available media data at the specified rate.
@discussion
When the player's currentItem has a value of NO for playbackBufferEmpty, this method causes the value of rate to change to the specified rate, the value of timeControlStatus to change to AVPlayerTimeControlStatusPlaying, and the receiver to play the available media immediately, whether or not prior buffering of media data is sufficient to ensure smooth playback.
If insufficient media data is buffered for playback to start (e.g. if the current item has a value of YES for playbackBufferEmpty), the receiver will act as if the buffer became empty during playback, except that no AVPlayerItemPlaybackStalledNotification will be posted.
*/
- (void)playImmediatelyAtRate:(float)rate NS_AVAILABLE(10_12, 10_0);
此外,您可以查看此变量(您也可以使用 KVO):
/*!
@property reasonForWaitingToPlay
@abstract Indicates the reason for waiting when the value of timeControlStatus is AVPlayerTimeControlStatusWaitingToPlayAtSpecifiedRate
@discussion
When the value of timeControlStatus is AVPlayerTimeControlStatusWaitingToPlayAtSpecifiedRate, this property describes why the player is currently waiting. It is nil otherwise.
You can use the value of reasonForWaitingToPlay to show UI indicating the player's waiting state conditionally.
This property is key value observable.
Possible values are AVPlayerWaitingWithNoItemToPlayReason, AVPlayerWaitingWhileEvaluatingBufferingRateReason, and AVPlayerWaitingToMinimizeStallsReason.
*/
@property (nonatomic, readonly, nullable) NSString *reasonForWaitingToPlay NS_AVAILABLE(10_12, 10_0);
推荐阅读
- javascript - 如何在特定索引中嵌入 Instagram 轮播?
- python - Tkinter Checkbutton 在颜色更改后不会保留其复选标记
- php - 如何按字母顺序显示数组?
- django - Django - MongoDB - 引用错误的模型
- python - 为什么使用 pip install requirements.txt 而不是 pip install ./?
- android - 列表视图上的 onitemclicklistener 不起作用
- actions-on-google - 模拟器和设备上的不同应用行为
- javascript - 使用javascript过滤在firebase中列出的status ='pending'的数据
- blockchain - Quorum 区块链上的公共交易和私人交易有什么区别
- python - 如何使用 Python 将无值填充到元组列表中