ios - 我不知道如何在 Swift 或 Objc 中正确使用 CMTimeRange 和 CMTime

标签 ios objective-c iphone swift waveform

早上好。
首先,我一直在学习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有误解,但是当我阅读官方文档时我不知道确切的问题。
如何解决问题并得出上述结果?
提前致谢。

This is the current state.

最佳答案

我已经用下面的代码解决了上述问题。

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/

相关文章:

ios - 将 UIButton 添加到 UIView 结果为白色文本

objective-c - 我应该从哪里开始 Mac 开发?

ios - MFMailComposeViewController 不会关闭

iphone - iPhone 与数据库的安全通信

iphone - map 加载后显示带标题(注释)的 Pin 图

iphone - 核心图中的自定义 X 轴

objective-c - 在 Obj-C 2.0 中锁定合成原子 @properties 的细节

iphone - iOS获取用户信息

objective-c - 如何创建具有多个显示三角形的调整大小窗口?

iphone - 在其他类(class)中对触摸使用react