2016-12-12 6 views
0

안녕하십니까.
우선, 나는 스위프트를 공부해 왔으며 3 주 이내에 IOS를 개발하지 않았다.
그리고 현재 사용자의 음악 파일 (mp3)을 가져 와서 파형을 표시하고 재생하는 기능을 구현하고 싶습니다.
우리는 SCWaveformView라는 오픈 소스를 사용하여 파형을 표시합니다.Swift 또는 Objc에서 CMTimeRange 및 CMTime을 올바르게 사용하는 방법을 모르겠습니다.

여기에 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 

예를 들어 3 분 25 초의 MP3 파일이 있습니다.
이 파일의 15 초 파형을 사용자에게 표시하고 스크롤하여 추가 파형을 볼 수 있습니다.
즉, 사용자가 볼 수있는 파형의 최대 길이는 15 초입니다.
파형을 스크롤하고 MP3 파일을 15 초 단위로 볼 수 있습니다.
스크롤을 통해 시작 시간이 2 분 15 초이면 최대 2 분 30 초의 파형을 볼 수 있습니다.
시작 시간이 2 분 20 초이면 최대 2 분 45 초의 파형을 볼 수 있습니다.

라이브러리는 문제가없는 것 같지만 CMTimeRange 및 CMTime을 이해하는 데 문제가 있습니다.
스크롤 속도가 느리고 때로는 작동하지 않습니다. 다음과 같은 오류 메시지가 나타납니다.

 
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 

내 문제 코드는 다음과 같습니다.

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.

답변

0

나는 아래의 코드를 사용하여 위의 문제를 해결했다.

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)