Cephalopod 4.0.0

Cephalopod 4.0.0

测试测试
Lang语言 SwiftSwift
许可证 MIT
Released最近发布2019年4月
SPM支持 SPM

Evgenii Neumerzhitckii维护。




  • Evgenii Neumerzhitckii

Cephalopod,用 Swift 编写的用于 AvAudioPlayer 的声音淡入淡出 - iOS, tvOS 和 macOS

Carthage compatible CocoaPods Version License Platform

这个库可以帮助使用 AvAudioPlayer 淡入和淡出声音。可以设置持续时间、速度和完成函数。速度可以从线性变化到指数级。

cephalopod = Cephalopod(player: player)
cephalopod?.fadeIn()

Juvenile cuttlefish camouflaged against the seafloor

年轻的鱿鱼与大洋底部相伪装。图片由 Raul654 拍摄。来源:维基媒体共享资源

设置

您有三种方式可以将 Cephalopod 添加到您的 Xcode 项目中。

添加源代码(iOS 7+)

只需将 CephalopodDistrib.swift 文件添加到您的项目中。

使用 Carthage 设置(iOS 8+)

或者,将以下代码添加到 Cartfile 中,然后运行 carthage updategithub "evgenyneu/Cephalopod" ~> 4.0

使用 CocoaPods 配置(iOS 8+)

如果您正在使用 CocoaPods,请将以下内容添加到 Podfile 中,然后运行 pod install

use_frameworks!
target 'Your target name'
pod 'Cephalopod', '~> 4.0'

旧版 Swift 版本

如果您使用较旧的 Swift 版本,设置库的旧版本

用法

以下示例显示了如何以渐入效果播放 mp3 文件。

import AVFoundation
import Cephalopod // For CocoaPods and Carthage
// ---

var playerInstance: AVAudioPlayer?
var cephalopod: Cephalopod?

override func viewDidLoad() {
  super.viewDidLoad()

  // Create a player instance
  guard let path = Bundle.main.path(forResource: "squid", ofType: "mp3") else { return }
  guard let player = try? AVAudioPlayer(contentsOf: URL(fileURLWithPath: path)) else { return }
  playerInstance = player

  // Start audio playback
  player.play()
  player.volume = 0

  // Fade in the sound
  cephalopod = Cephalopod(player: player)
  cephalopod?.fadeIn()
}

渐入/渐出

cephalopod?.fadeIn()
cephalopod?.fadeOut()

// Supply fade duration and velocity, in seconds
cephalopod?.fadeIn(duration: 3, velocity: 2)
cephalopod?.fadeOut(duration: 3, velocity: 2)

// Supply finish closure
cephalopod?.fadeIn(duration: 3, velocity: 2) { finished in }
cephalopod?.fadeOut(duration: 3, velocity: 2)  { finished in }

提供渐入开始/结束音量和完成回调

cephalopod?.fade(fromVolume: 0.3, toVolume: 0.7, duration: 3, velocity: 2) { finished in
  print("Finished fading")
}

参数:

fromVolume - 开始音量,一个介于 0 和 1 之间的数字。

toVolume - 结束音量,一个介于 0 和 1 之间的数字。

duration - 渐变的持续时间,单位为秒。默认持续时间:3秒。

velocity - 一个指定声音音量变化速度的数字。速度为 0 创建线性渐变。大于零的值会产生更指数的渐变效果。指数渐变对人的耳朵来说更平缓。使用 2 到 5 的速度参数可以创建最自然的渐变效果。默认值:2。

onFinished - 可选的闭包,将在淡入淡出结束后被调用。闭包将传递一个布尔参数 finished,表示淡入淡出是否达到了其结束值(true)或者是否已被取消(false)。

设置渐变的品质

cephalopod?.volumeAlterationsPerSecond = 20

数字越大,会产生更细腻的渐变效果,但会消耗更多的CPU资源。默认值是:30

停止音量变化

可以通过调用 stop() 方法来取消正在进行的音量变化。注意,这会停止音量变化,但不会停止播放。

cephalopod?.stop()

音量功能

以下图表显示了淡入淡出过程中音量如何变化。

Sound fade logarithmic velocity function for iOS/Swift

渐显公式

Sound fade out logarithmic formula

渐隐公式

Sound fade in logarithmic formula

其中 x 是时间,v 是速度。

速度为 0 会创建线性渐变。大于 0 的值会创建更指数的渐变效果。指数渐变在人类耳朵听起来更平缓。我个人使用 2 到 5 的速度值。

实时图表演示:[点击查看](https://www.desmos.com/calculator/wnstesdf0h)

示例应用程序

Cephalopod sound fader for iOS written in swift

替代解决方案

以下是为iOS提供的其他声音库列表。

感谢👍

  • nschucky 更新到了 Swift 2.2 选择器语法。

致谢

许可证

Cephalopod 采用 MIT 许可证 发布。

欢迎反馈

如果您发现任何问题、遇到了麻烦或只是想聊天,请随时创建一个问题。我将很高兴帮助您。