From 7f89b99b143d856d7025b4c5b7e15360a1a05923 Mon Sep 17 00:00:00 2001 From: Jack Date: Mon, 20 Jul 2015 14:11:17 +0800 Subject: [PATCH] Add Property control need update progressTime --- Sources/SCScrollableWaveformView.h | 3 ++ Sources/SCScrollableWaveformView.m | 4 ++ Sources/SCViewController.m | 61 ++++++++++++++++++++++++++++-- 3 files changed, 64 insertions(+), 4 deletions(-) diff --git a/Sources/SCScrollableWaveformView.h b/Sources/SCScrollableWaveformView.h index 82a72d0..c998d73 100644 --- a/Sources/SCScrollableWaveformView.h +++ b/Sources/SCScrollableWaveformView.h @@ -18,4 +18,7 @@ */ @property (readonly, nonatomic) SCWaveformView *waveformView; +// Default is NO +@property (assign, nonatomic) BOOL shouldUpdateProgressTime; + @end diff --git a/Sources/SCScrollableWaveformView.m b/Sources/SCScrollableWaveformView.m index 036ccd5..a7a6114 100644 --- a/Sources/SCScrollableWaveformView.m +++ b/Sources/SCScrollableWaveformView.m @@ -69,6 +69,10 @@ - (void)_updateWaveform { // NSLog(@"Updating timeRange to %fs", CMTimeGetSeconds(newStart)); _waveformView.timeRange = CMTimeRangeMake(newStart, _waveformView.timeRange.duration); + + if (self.shouldUpdateProgressTime) { + self.waveformView.progressTime = newStart; + } } } } diff --git a/Sources/SCViewController.m b/Sources/SCViewController.m index 3e27654..1e686b2 100644 --- a/Sources/SCViewController.m +++ b/Sources/SCViewController.m @@ -9,19 +9,30 @@ #import "SCViewController.h" #import "SCWaveformView.h" -@interface SCViewController () { +static Float64 const defaultMaxAudioTime = 20.0; + +@interface SCViewController () { AVPlayer *_player; id _observer; } +@property (nonatomic, assign) Float64 maxAudioTime; + @end @implementation SCViewController -- (void)viewDidLoad -{ +- (void)awakeFromNib { + self.maxAudioTime = defaultMaxAudioTime; +} + +- (void)viewDidLoad { [super viewDidLoad]; + // should need update for scrolling + self.scrollableWaveformView.shouldUpdateProgressTime = YES; + + self.scrollableWaveformView.delegate = self; self.scrollableWaveformView.waveformView.precision = 1; self.scrollableWaveformView.waveformView.lineWidthRatio = 1; self.scrollableWaveformView.waveformView.normalColor = [UIColor colorWithRed:0.8 green:0.3 blue:0.3 alpha:1]; @@ -36,14 +47,27 @@ - (void)viewDidLoad self.slider.value * CMTimeGetSeconds(self.scrollableWaveformView.waveformView.asset.duration), 100000); - self.scrollableWaveformView.waveformView.timeRange = CMTimeRangeMake(CMTimeMakeWithSeconds(15, 10000), progressTime); + self.scrollableWaveformView.waveformView.timeRange = CMTimeRangeMake(CMTimeMakeWithSeconds(self.maxAudioTime, 10000), progressTime); _player = [AVPlayer playerWithPlayerItem:[AVPlayerItem playerItemWithAsset:asset]]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_playReachedEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:_player.currentItem]; __unsafe_unretained SCViewController *mySelf = self; + __unsafe_unretained AVPlayer *myPlayer = _player; _observer = [_player addPeriodicTimeObserverForInterval:CMTimeMake(1, 60) queue:dispatch_get_main_queue() usingBlock:^(CMTime time) { + + time = CMTimeMake(time.value + mySelf.scrollableWaveformView.waveformView.timeRange.start.value, (time.timescale + mySelf.scrollableWaveformView.waveformView.timeRange.start.timescale)); + + Float64 currentTime = CMTimeGetSeconds(myPlayer.currentTime); + + Float64 maxTime = CMTimeGetSeconds(mySelf.scrollableWaveformView.waveformView.timeRange.start) + self.maxAudioTime; + + if (currentTime >= maxTime) { + time = mySelf.scrollableWaveformView.waveformView.timeRange.start; + [myPlayer seekToTime:time]; + } + mySelf.scrollableWaveformView.waveformView.progressTime = time; }]; } @@ -106,4 +130,33 @@ - (IBAction)sliderProgressChanged:(UISlider*)sender self.scrollableWaveformView.waveformView.timeRange = CMTimeRangeMake(start, duration); } +#pragma mark - UIScrollView Delegate + +- (void)didScroll { + [_player pause]; +} + +- (void)didEndScroll { + + CMTime time = self.scrollableWaveformView.waveformView.timeRange.start; + [_player seekToTime:time]; + self.scrollableWaveformView.waveformView.progressTime = time; + + [_player play]; +} + +- (void)scrollViewDidScroll:(UIScrollView *)scrollView { + [self didScroll]; +} + +- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { + [self didEndScroll]; +} + +- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate { + if (!decelerate) { + [self didEndScroll]; + } +} + @end