diff --git a/Sources/SCScrollableWaveformView.h b/Sources/SCScrollableWaveformView.h index 942ea63..775ab72 100644 --- a/Sources/SCScrollableWaveformView.h +++ b/Sources/SCScrollableWaveformView.h @@ -9,7 +9,7 @@ #import #import "SCWaveformView.h" -@interface SCScrollableWaveformView : UIScrollView +@interface SCScrollableWaveformView : UIScrollView @property (readonly, nonatomic) SCWaveformView *waveformView; diff --git a/Sources/SCScrollableWaveformView.m b/Sources/SCScrollableWaveformView.m index 772ffd7..1e276a3 100644 --- a/Sources/SCScrollableWaveformView.m +++ b/Sources/SCScrollableWaveformView.m @@ -48,6 +48,30 @@ - (void)_commonInit { [self addSubview:_waveformView]; [self addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:ScrollableWaveformContentOffsetContext]; + + self.delegate = self; +} + +- (void)_setAutoNeedsDisplayedEnabled:(BOOL)enabled { + _waveformView.needsDisplayOnProgressTimeChange = enabled; +} + +- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { + [self _setAutoNeedsDisplayedEnabled:YES]; +} + +- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView { + [self _setAutoNeedsDisplayedEnabled:YES]; +} + +- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { + [self _setAutoNeedsDisplayedEnabled:NO]; +} + +- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate { + if (!decelerate) { + [self _setAutoNeedsDisplayedEnabled:YES]; + } } - (void)dealloc { diff --git a/Sources/SCWaveformCache.m b/Sources/SCWaveformCache.m index 64dd799..2f0c2e9 100644 --- a/Sources/SCWaveformCache.m +++ b/Sources/SCWaveformCache.m @@ -148,13 +148,13 @@ - (BOOL)readTimeRange:(CMTimeRange)timeRange width:(CGFloat)width error:(NSError } if (shouldReadAsset) { - NSDictionary *outputSettingsDict = [[NSDictionary alloc] initWithObjectsAndKeys: - [NSNumber numberWithInt:kAudioFormatLinearPCM], AVFormatIDKey, - [NSNumber numberWithInt:16], AVLinearPCMBitDepthKey, - [NSNumber numberWithBool:NO], AVLinearPCMIsBigEndianKey, - [NSNumber numberWithBool:NO], AVLinearPCMIsFloatKey, - [NSNumber numberWithBool:NO], AVLinearPCMIsNonInterleaved, - nil]; + NSDictionary *outputSettingsDict = @{ + AVFormatIDKey : [NSNumber numberWithInt:kAudioFormatLinearPCM], + AVLinearPCMBitDepthKey : @16, + AVLinearPCMIsBigEndianKey : @NO, + AVLinearPCMIsFloatKey : @NO, + AVLinearPCMIsNonInterleaved : @NO + }; AVAssetReaderTrackOutput *output = [[AVAssetReaderTrackOutput alloc] initWithTrack:songTrack outputSettings:outputSettingsDict]; output.alwaysCopiesSampleData = NO; @@ -176,15 +176,14 @@ - (BOOL)readTimeRange:(CMTimeRange)timeRange width:(CGFloat)width error:(NSError NSUInteger bigSampleCount = 0; NSMutableData *data = [NSMutableData new]; UInt32 bytesPerInputSample = 2 * channelCount; - - CGFloat currentX = 0; + while (reader.status == AVAssetReaderStatusReading) { CMSampleBufferRef sampleBufferRef = [output copyNextSampleBuffer]; if (sampleBufferRef) { CMBlockBufferRef blockBufferRef = CMSampleBufferGetDataBuffer(sampleBufferRef); size_t bufferLength = CMBlockBufferGetDataLength(blockBufferRef); - + char *dataPointer; CMBlockBufferGetDataPointer(blockBufferRef, 0, &bufferLength, nil, &dataPointer); @@ -207,7 +206,6 @@ - (BOOL)readTimeRange:(CMTimeRange)timeRange width:(CGFloat)width error:(NSError [data appendBytes:&averageSample length:(sizeof(float))]; - currentX++; bigSample = 0; bigSampleCount = 0; } diff --git a/Sources/SCWaveformView.h b/Sources/SCWaveformView.h index 2cad45d..6d0ef3a 100644 --- a/Sources/SCWaveformView.h +++ b/Sources/SCWaveformView.h @@ -18,23 +18,8 @@ @property (assign, nonatomic) CMTime progressTime; @property (assign, nonatomic) BOOL antialiasingEnabled; +@property (assign, nonatomic) BOOL needsDisplayOnProgressTimeChange; + @property (assign, nonatomic) CMTimeRange timeRange; @end - - -//typedef enum : NSUInteger { -// /** -// The WaveformView draw manually in "drawRect:" method. -// This is the fastest way to render the waveform. -// -// */ -// SCWaveformViewRenderingModeDraw, -// -// /** -// The WaveformView render the waveform in UIImage's. -// This is slower, however it will be. -// -// */ -// SCWaveformViewRenderingModeGenImages -//} SCWaveformViewRenderingMode; diff --git a/Sources/SCWaveformView.m b/Sources/SCWaveformView.m index dcce984..3ed87b2 100644 --- a/Sources/SCWaveformView.m +++ b/Sources/SCWaveformView.m @@ -43,9 +43,11 @@ - (instancetype)initWithCoder:(NSCoder *)aDecoder { } - (void)commonInit { + _needsDisplayOnProgressTimeChange = YES; self.normalColor = [UIColor blueColor]; self.progressColor = [UIColor redColor]; _timeRange = CMTimeRangeMake(kCMTimeZero, kCMTimePositiveInfinity); + _progressTime = kCMTimeZero; _cache = [SCWaveformCache new]; } @@ -151,7 +153,10 @@ - (void)setAsset:(AVAsset *)asset { - (void)setProgressTime:(CMTime)progressTime { _progressTime = progressTime; - [self setNeedsDisplay]; + + if (self.needsDisplayOnProgressTimeChange) { + [self setNeedsDisplay]; + } } - (void)setAntialiasingEnabled:(BOOL)antialiasingEnabled {