A waveform based audio player, for SwiftUI.
File > Add Package Dependencies > Enter Package URL: https://github.com/nbrandes/Waavform
Add a dependency in your Package.swift
.package(url: "https://github.com/nbrandes/Waavform.git"),
First add an audio file to your project.
Initialize Waavform
with the name and the extension of the audio file.
Waavform(audio: "TheMoon", type: "mp3")
Waavform
can be customized with the following parameters
Required:
audio: String
- name of the audio file
type: String
- extension of the audio file
Optional:
progress: Color
- waveform progress color
background: Color
- waveform background color
canvas: Color
- color displayed behind the waveform
cursor: Color
- the selection cursor color
playhead: Color
- the playhead color
timeText: Color
- current time and duration time color
timeBg: Color
- current time and duration time background color
control: Color
- play/stop button tint color
category: AVAudioSession.Category
- category used for the session
showTransport: Bool
- show or hide the play/stop controls
showScroll: Bool
- show or hide the linear/scroll control
viewOnLoad: ViewType
- which view to display initially (.linear / .scroll)
Play - start playing the audio
play()
Pause - pause the audio
pause()
Stop - stop the audio and go to the beginning
stop()
ToggleView - switch between scroll or linear views
toggleView()
An example of three players stacked vertically.
import SwiftUI
import Waavform
struct ContentView: View {
var body: some View {
VStack {
Waavform(audio: "Song1", type: "mp3", progress: .blue, playhead: .cyan, viewOnLoad: .scroll)
Waavform(audio: "Song2", type: "mp3", progress: .orange, playhead: .cyan, viewOnLoad: .linear)
Waavform(audio: "Song3", type: "mp3", progress: .red, playhead: .cyan)
}
}
}
Shows how to use Waavform
with custom playback controls.
import SwiftUI
import Waavform
struct ContentView: View {
@State var wave : Waavform?
var body: some View {
VStack {
wave
HStack {
Button("Play") {
wave?.play()
}
.buttonStyle(.borderedProminent)
Button("Pause") {
wave?.pause()
}
.buttonStyle(.borderedProminent)
Button("Stop") {
wave?.stop()
}
.buttonStyle(.borderedProminent)
Button("Mode") {
wave?.toggleView()
}
.buttonStyle(.borderedProminent)
}
}
.padding()
.task {
wave = Waavform(audio: song.name, type: song.type, playhead: .cyan, showControls: false, viewOnLoad: .scroll)
}
}
}