早上好。
首先,我一直在学习Swift,不到3周才开始开发IOS。
我想实现截取当前用户的音乐文件(mp3)并显示波形并播放的功能。
我们使用名为 SCWaveformView 的开源工具来显示波形。
这是SCWaveformView ! 上述库的使用方法如下。
// Setting the asset AVAsset *asset = [AVURLAsset URLAssetWithURL:[NSURL URLWithString:@"blabla.mp3"]]; waveformView.asset = asset; // Show only the first second of your asset waveformView.timeRange = CMTimeRangeMake(kCMTimeZero, CMTimeMakeWithSeconds(1, 1)); // Use it inside a scrollView SCScrollableWaveformView *scrollableWaveformView = [SCScrollableWaveformView new]; scrollableWaveformView.waveformView; // Access the waveformView from there
For example, I have an MP3 file of 3 minutes 25 seconds.
You can display a 15-second waveform for this file to the user and scroll to see additional waveforms.
In other words, the maximum length of a user-visible waveform is 15 seconds.
You can scroll through the waveforms and view MP3 files in 15 second increments.
Through scrolling
If the start time is 2 minutes and 15 seconds, you can see the waveform up to 2 minutes and 30 seconds.
If the start time is 2 minutes and 20 seconds, you can see the waveform up to 2 minutes 45 seconds.
The library seems not to be a problem, but I am having trouble understanding CMTimeRange and CMTime.
Scrolling is slow and sometimes does not work. The following error message appears:
2016-12-12 15:29:03.568654 AppName[7765:2094292] CMTimeMakeWithSeconds(0.200 seconds, timescale 1): warning: error of -0.200 introduced due to very low timescale 2016-12-12 15:29:03.584947 AppName[7765:2094292] CMTimeMakeWithSeconds(0.240 seconds, timescale 1): warning: error of -0.240 introduced due to very low timescale 2016-12-12 15:29:03.585166 AppName[7765:2094292] CMTimeMakeWithSeconds(0.240 seconds, timescale 1): warning: error of -0.240 introduced due to very low timescale 2016-12-12 15:29:03.602243 AppName[7765:2094292] CMTimeMakeWithSeconds(0.280 seconds, timescale 1): warning: error of -0.280 introduced due to very low timescale 2016-12-12 15:29:03.618334 AppName[7765:2094292] CMTimeMakeWithSeconds(0.460 seconds, timescale 1): warning: error of -0.460 introduced due to very low timescale 2016-12-12 15:29:03.634987 AppName[7765:2094292] CMTimeMakeWithSeconds(0.480 seconds, timescale 1): warning: error of -0.480 introduced due to very low timescale 2016-12-12 15:29:03.651538 AppName[7765:2094292] CMTimeMakeWithSeconds(0.480 seconds, timescale 1): warning: error of -0.480 introduced due to very low timescale 2016-12-12 15:29:03.668358 AppName[7765:2094292] CMTimeMakeWithSeconds(0.720 seconds, timescale 1): warning: error of -0.720 introduced due to very low timescale 2016-12-12 15:29:03.697401 AppName[7765:2094292] CMTimeMakeWithSeconds(0.340 seconds, timescale 1): warning: error of -0.340 introduced due to very low timescale 2016-12-12 15:29:03.715348 AppName[7765:2094292] CMTimeMakeWithSeconds(0.480 seconds, timescale 1): warning: error of -0.480 introduced due to very low timescale
My problematic code is below.
let musicAsset = AVAsset(url: (item?.assetURL)!)
self.scwaveScrollView.waveformView.asset = musicAsset
self.scwaveScrollView.waveformView.precision = 0.25
self.scwaveScrollView.waveformView.timeRange = CMTimeRangeMake(self.scwaveScrollView.waveformView.timeRange.start, CMTimeMakeWithSeconds(15, 1));
我想我对CMTimeRange和CMTime有误解,但是当我阅读官方文档时我不知道确切的问题。
如何解决问题并得出上述结果?
提前致谢。
最佳答案
我已经用下面的代码解决了上述问题。
let musicAsset = AVAsset(url: (item?.assetURL)!)
self.scwaveScrollView.waveformView.asset = musicAsset
self.scwaveScrollView.waveformView.precision = 1
var duration = CMTimeMakeWithSeconds( 1.0 * CMTimeGetSeconds(self.scwaveScrollView.waveformView.asset.duration), 100000)
self.scwaveScrollView.waveformView.timeRange = CMTimeRangeMake(CMTimeMakeWithSeconds(0, 10000), duration);
let start = self.scwaveScrollView.waveformView.timeRange.start
duration = CMTime(seconds: 15, preferredTimescale: 1)
self.scwaveScrollView.waveformView.timeRange = CMTimeRangeMake(start, duration)
关于ios - 我不知道如何在 Swift 或 Objc 中正确使用 CMTimeRange 和 CMTime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41095653/