当前位置:网站首页>Swift uses avplayer and avplayeritem for voice playback
Swift uses avplayer and avplayeritem for voice playback
2022-04-21 20:53:00 【It looks like fun】
Preface :
Recently, the work content uses voice playback , from AVPlayer To AVPlayerItem, I encountered a lot of problems , A lot bug, Also summarized some experience . Yes AVPlayer Learn more , Because there are many points involved , So I'm going to make a series and write this part in detail . I hope you can support me more , If you have any problems, please correct me .
AVPlayer Is a full-featured video player that can play any format
Support video format : WMV,AVI,MKV,RMVB,RM,XVID,MP4,3GP,MPG etc. .
Support audio format :MP3,WMA,RM,ACC,OGG,APE,FLAC,FLV etc. .
So I have to say that Apple's son is still very powerful , Is the first choice for video development .
How to use
AVPlayer Exist in AVFoundation frame , We need to import :
import AVFoundation
Several playback related parameters ( For the time being, I'll just talk about what I use )
Before creating a player, we need to know some player related classes
AVPlayer: Control the playback of the player , Pause , Broadcasting speed
AVPlayerItem: Manage resource objects , Provide playback data source
1. establish AVPlayer
Because I play , Need to play 、 Pause 、 Replay 、 Drag the progress bar to play , So I used AVPlayerItem
if let url = URL(string: "http://img.youluwx.com/qa/20200917/video/c94869f4-0ddc-4e45-be7e-b0620acc544d.mp3") {
let playerItem = AVPlayerItem(url: url)
let play = AVPlayer(playerItem: playerItem)
}
1.2 Video playback 、 Pause
/// Play
player.play()
/// Pause
player.pause()
1.3 Replay
Replay is , First adjust the progress to the beginning , And then play
let seekTime = CMTime(value: CMTimeValue(0), timescale: 1)
player.seek(to: seekTime, completionHandler: {
_ in
})
player.play()
1.3 Drag the progress bar to play
Use here UISlider Slide bar to drag and use Swift Use UISlider
let playTime = slider.value * duration
let seekTime = CMTime(value: CMTimeValue(playTime), timescale: 1)
player.seek(to: seekTime, completionHandler: {
_ in
})
1.4 Playback progress monitoring
let playerItem = AVPlayerItem(url: url)
let player = AVPlayer(playerItem: playerItem)
player.addPeriodicTimeObserver(forInterval: CMTime(value: 1, timescale: 1), queue: DispatchQueue.main) {
time in
if let duration = self.playerItem?.duration {
let nowDuration = CMTimeGetSeconds(duration)
let currentTime = Int(time.seconds)
let text = "\(currentTime)/\(nowDuration)\""
let progress = Float(currentTime) / Float(nowDuration)
print("text:\(text) progress:\(progress)")
}
}
2. Problems encountered
2.1 Get the total time of network audio
Use AVPlayer Play network streaming media , Found a pit :
Namely playerItem.duration It is possible not to return the total playing time of the network multimedia resources , Instead, it returns a strange data :nan,
Because I passed CMTimeGetSeconds (playerItem.duration) What you get has always been nan, Only monitor the playback progress , You can't get the data until you start playing
let playerItem = AVPlayerItem(url: url)
if let duration = playerItem?.duration {
print(CMTimeGetSeconds(duration))
}
After many twists and turns , Find out CMTimeGetSeconds (playerItem.asset.duration) What is returned is the correct total playing time of the network multimedia resource .
let playerItem = AVPlayerItem(url: url)
if let duration = playerItem?.asset.duration {
print(CMTimeGetSeconds(duration))
}
Last , The total time may be a little less than the playback time , This has never been understood , If you know something, please leave a message
The biggest problem
During the final test , Encountered more serious problems , I found that part of our voice can play , The other part cannot be played ,
Playable voice
http://img.youluwx.com/qa/20200917/video/c94869f4-0ddc-4e45-be7e-b0620acc544d.mp3
Do not play voice
http://img.youluwx.com/qa/20201110/video/47e36af4-8fc2-432f-b7c6-ccd982140694.mp3
I downloaded the audio files separately and compared them :

Can't play in Safari Open in the browser and display as follows , What can be played will not be displayed for the time being

The specific reason should be AVPlayer Don't support it , As for more specific questions : Maybe it's because I'm too delicious , Can't find the problem , No concrete solution has been found , Can only borrow other video players for temporary use
Um. , Last , If you want to use it AVPlayer, Better be careful
版权声明
本文为[It looks like fun]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204212048595101.html
边栏推荐
- String. Length () and string getBytes(). Length difference
- Matlab-句柄图形
- 工作流 流程设置 定制开发
- Construction of distributed second kill system
- TGIP-CN 038 报名|深度解析 Apache Pulsar 源码阅读正确姿势(一)
- 为何PostgreSQL即将超越SQL Server?
- Circular linked list of single and double linked lists (XV)
- 各种dns:百度DNS/阿里DNS/114DNS/腾讯DNS/谷歌DNS/OpenDNS 对比评测
- 88% of industrial people don't know that these 7 points of small programs can make the revenue surge, rough and effective! It is strongly recommended to collect and read repeatedly!
- [system analyst's road] real topic of system analyst's paper writing in the next 2020
猜你喜欢

Why is PostgreSQL about to surpass SQL Server?

SQL 错误: ORA-01428: 参数 ‘0‘ 超出范围 01428. 00000 - “argument ‘%s‘ is out of range“

3、MySQL Workbench 对表进行增删改查

TGIP-CN 038 报名|深度解析 Apache Pulsar 源码阅读正确姿势(一)

通达OA表单会签意见样式

MySQL数值函数

Circular linked list of single and double linked lists (XV)
![[daily leetcoding challenge14] a group of K flip linked lists](/img/d8/1479d52ac3ced3071db789cc2158ae.png)
[daily leetcoding challenge14] a group of K flip linked lists

2022-4-11至2022-4-17周报

Specific methods of configuring Profibus and PROFINET communication in two TIA botu projects
随机推荐
《动手学机器人学》7.3.1齐次坐标变换&&齐次变换矩阵
RTMP(4):User Control Message
4、MySQL Workbench创建访问用户
2022R2移动式压力容器充装考试练习题及在线模拟考试
Oracle management - tablespace permission control
Sketch
He was in '98. I can't play with him
UAV assembly and debugging tutorial
Trackup | six unique benefits of using project management
通达oa工作流升级 操作说明
Rhcsa (day 5)
PR视频添加字幕
In depth analysis of TCP three handshakes, the interviewer applauded
预处理问题
2022-4-11至2022-4-17周报
基于华为云图像识别标签
6、Qt使用MySQL例子
Swift 使用AVPlayer 和 AVPlayerItem 做语音播放
Go语言自学系列 | golang类型定义和类型别名
How to install the slip ring correctly and effectively