iOS 根据音乐来变动闪光灯和闪屏

前段时间一朋友需要实现该功能,其实实现起来不难,下面是实现效果

提示:模拟器看不到效果,需要真机才能看到效果


[简书查看demo代码下载地址]





闪光灯的开启与关闭


//开启闪光灯
- (void)openFlash{
    
    AVSession = [[AVCaptureSession alloc]init];
    device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    [device lockForConfiguration:nil];//此处以一定要调用这个方法不然程序运行到这里会崩溃
    [device setTorchMode:AVCaptureTorchModeOn];
    [device setFlashMode:AVCaptureFlashModeOn];
    [device unlockForConfiguration];
    [AVSession startRunning];
    
}
//关闭闪光灯
- (void)closeFlash{
    
    AVSession = [[AVCaptureSession alloc]init];
    device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    [device lockForConfiguration:nil];
    [device setTorchMode:AVCaptureTorchModeOff];
    [device setFlashMode:AVCaptureFlashModeOff];
    [device unlockForConfiguration];
    [AVSession stopRunning];
    AVSession = nil;    //置空
    device = nil;       //置空
}

屏幕亮度改变


- (void)changeScreenBright:(float)value
{
    if (self.screnSwitch.on) {
        [[UIScreen mainScreen] setBrightness:value];//修改屏幕亮度
    }
    
}

音乐频率计算


_vodioTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(vodiotimerFire:) userInfo:nil repeats:YES];
    [_vodioTimer fire];


//定时器事件
- (void)vodiotimerFire:(NSTimer *)sender{
    
    self.musicPlayer.meteringEnabled = YES;
    [self.musicPlayer updateMeters];
    
    
    float   level;                // The linear 0.0 .. 1.0 value we need.
    float   minDecibels = -80.0f; // Or use -60dB, which I measured in a silent room.
    float   decibels    = [self.musicPlayer averagePowerForChannel:0];
    
    if (decibels < minDecibels)
    {
        level = 0.0f;
    }
    else if (decibels >= 0.0f)
    {
        level = 1.0f;
    }
    else
    {
        float   root            = 2.0f;
        float   minAmp          = powf(10.0f, 0.05f * minDecibels);
        float   inverseAmpRange = 1.0f / (1.0f - minAmp);
        float   amp             = powf(10.0f, 0.05f * decibels);
        float   adjAmp          = (amp - minAmp) * inverseAmpRange;
        
        level = powf(adjAmp, 1.0f / root);
    }
    
    /* level 范围[0 ~ 1], 转为[0 ~120] 之间 */
    NSInteger newLevel = (NSInteger)(level * 120);
    NSLog(@"level== %d",newLevel);
    if (newLevel == 0) {
        self.oldLevel = newLevel;
    }
    
    NSInteger spaceLevel = newLevel - self.oldLevel;
    if (abs(spaceLevel) >= 10) {
        self.oldLevel = newLevel;
        
        if (self.lampSwitch.on) {
            [self openFlash];
            [self closeFlash];
        }
        
        float currentBright = 1- (newLevel / 120.0);
        [self changeScreenBright:currentBright];
        
        
    } else {
        [self changeScreenBright:self.defaultScreenBright];
    }
    
    
}

[简书查看demo代码下载地址]


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
闪烁的文字,多种闪烁效果 https://github.com/Zws-China/WSShiningLabel WSShiningLabel *label1 = [[WSShiningLabel alloc] init]; label1.frame = CGRectMake(50, 35, 200, 25); label1.text = @"当保护你的她"; label1.textColor = [UIColor grayColor]; label1.font = [UIFont systemFontOfSize:20]; [label1 startShimmer]; // 开启闪烁 [self.view addSubview:label1]; WSShiningLabel *label2 = [[WSShiningLabel alloc] init]; label2.frame = CGRectMake(50, 105, 200, 25); label2.text = @"变成要你保护的她"; label2.textColor = [UIColor grayColor]; label2.font = [UIFont systemFontOfSize:20]; label2.shimmerType = ST_RightToLeft; // 滚动方向 right to left label2.durationTime = 1; // 滚动时间 label2.shimmerColor = [UIColor orangeColor]; // 高亮颜色 [label2 startShimmer]; // 开启闪烁 [self.view addSubview:label2]; WSShiningLabel *label3 = [[WSShiningLabel alloc] init]; label3.frame = CGRectMake(50, 175, 200, 25); label3.text = @"当你远离了家"; label3.textColor = [UIColor grayColor]; label3.font = [UIFont systemFontOfSize:20]; label3.shimmerType = ST_AutoReverse; // 滚动方向 左右来回 label3.shimmerWidth = 20; // 高亮的宽度 label3.shimmerRadius = 20; // 阴影的宽度 label3.shimmerColor = [UIColor yellowColor]; // 高亮颜色 [label3 startShimmer]; // 开启闪烁 [self.view addSubview:label3]; WSShiningLabel *label4 = [[WSShiningLabel alloc] init]; label4.frame = CGRectMake(50, 245, 200, 25); label4.text = @"努力有了你爱的她"; label4.textColor = [UIColor grayColor]; label4.font = [UIFont systemFontOfSize:20]; label4.shimmerType = ST_ShimmerAll; // 闪烁 label4.durationTime = 0.8; label4.shimmerColor = [UIColor redColor]; [label4 startShimmer]; [self.view addSubview:label4];

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值