Mac 音频波形图——播放

开篇

播放音频的效果图:

效果图

需求分析

  1. 灰色波形底图
  2. 播放过程,白色波形覆盖

实现思路

灰色波形底图是固定的,可直接用图层绘制

1、波形的路径计算与上篇类似

//波形路径
    - (CGPathRef)pathWithPoints:(NSArray *)points{
        CGFloat midY = NSHeight(self.bounds) / 2.f;
        CGFloat leftX = NSMaxX(playBtnRect);
        
        CGMutablePathRef wavePath = CGPathCreateMutable();                 //绘制路径
        CGPathMoveToPoint(wavePath, nil, leftX, midY);
        for (NSInteger i = 0; i < _pointArray.count; i++) {
            NSValue *pointValue = _pointArray[i];
            NSPoint point = pointValue.pointValue;
            if (point.y == 0) {
                CGPathMoveToPoint(wavePath, nil, leftX + i - 1, midY);
                CGPathAddLineToPoint(wavePath, NULL, leftX + i, midY);
            }else{
                CGPathMoveToPoint(wavePath, nil, leftX + i, midY);
                CGPathAddLineToPoint(wavePath, NULL, leftX + i, midY + point.y);
                CGPathMoveToPoint(wavePath, nil, leftX + i, midY);
                CGPathAddLineToPoint(wavePath, NULL, leftX + i, midY - point.y);
            }
        }
        CGPathRef path = CGPathCreateCopy(wavePath);
        CGPathRelease(wavePath);
        return path;
    }

绘制灰色波形

//添加完整波形图层
- (void)addWaveLayerWithPath:(CGPathRef)wavePath{
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.lineWidth=1;
    shapeLayer.strokeColor=[NSColor lightGrayColor].CGColor;
    shapeLayer.lineCap = kCALineCapRound;
    shapeLayer.lineJoin = kCALineJoinRound;
    [self.layer addSublayer:shapeLayer];
    shapeLayer.path = wavePath;
}

2、播放图层,使用CAShapeLayer实现,CAShapeLayer是唯一一个可动画效果的图层了

//添加播放动画图层
- (void)addAnimationLayerWithPath:(CGPathRef)path{
    animationLayer = [CAShapeLayer layer];
    animationLayer.path = path;
    animationLayer.lineWidth = 1;
    animationLayer.strokeColor=[NSColor whiteColor].CGColor;
    animationLayer.lineCap = kCALineCapRound;
    animationLayer.lineJoin = kCALineJoinRound;
    [self.layer addSublayer:animationLayer];
    
    animationLayer.speed = 0;   //禁止动画执行
    animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    animation.duration = _playDuration;
    animation.fromValue = @(0.0f);
    animation.toValue = @(1.0f);
    animation.delegate = self;
    [animationLayer addAnimation:animation forKey:@""];
}

animationLayer.speed设为0,即动画速度为0,也就是不执行。

@"strokeEnd"是路径的结束点,始于0,止于1,当动画开始执行,就能看到绘制过程了。

3、播放的开始、暂停、继续、停止

对动画的控制,主要是对speedtimeOffsetbeginTime等属性的设置。

- (void)play{
    [self resume];
}

- (void)pause{
    _playing = NO;
    [self setNeedsDisplay:YES];
    
    CFTimeInterval pausedTime = [animationLayer convertTime:CACurrentMediaTime() fromLayer:nil];
    animationLayer.speed = 0;
    animationLayer.timeOffset = pausedTime;
}

- (void)resume{
    _playing = YES;
    [self setNeedsDisplay:YES];
    
    CFTimeInterval pausedTime = [animationLayer timeOffset];
    animationLayer.speed = 1.0;
    animationLayer.timeOffset = 0.0;
    animationLayer.beginTime = 0;
    CFTimeInterval timeSincePause = [animationLayer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
    animationLayer.beginTime = timeSincePause;
}

- (void)stop{
    _playing = NO;
    [self setNeedsDisplay:YES];

    animationLayer.timeOffset = 0;
    animationLayer.speed = 0;
    
    //动画播放完成后,默认自动removed
    [animationLayer addAnimation:animation forKey:@""];
}

Demo 地址:https://github.com/YunFei2015/AudioWaveAnimation.git

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥ios动画全貌。在这里你可以看...
    每天刷两次牙阅读 12,716评论 6 30
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥iOS动画全貌。在这里你可以看...
    F麦子阅读 10,528评论 5 13
  • 笔记主要来源iOS核心动画高级技巧,感谢作者与翻译的各位同学. 一、图层树 UIView、NSView都有一个关联...
    幸运的白鸽阅读 8,889评论 0 3
  • >*时间和空间最大的区别在于,时间不能被复用* --弗斯特梅里克 在上面两章中,我们探讨了可以用`CAAnimat...
    夜空下最亮的亮点阅读 1,930评论 0 0
  • 背景 后台广告系统匹配由串行转为并行,涉及到并发数控制和一些资源回收工作,利用channel去做非常容易实现。细节...
    董泽润阅读 3,287评论 0 0