| .. | ||
| Sources/AudioPlaybackKit | ||
| Tests/AudioPlaybackKitTests | ||
| Package.swift | ||
| README.md | ||
AudioPlaybackKit
A Swift package for audio playback functionality, specifically designed for white noise and ambient sound applications.
Features
- Audio Playback: High-quality audio playback with background support
- Sound Management: Load and manage audio files from bundles
- Wake Lock: Prevent device from sleeping during audio playback
- Interruption Handling: Automatic handling of audio interruptions (calls, etc.)
- Configuration: JSON-based sound configuration system
- Preview Support: Built-in audio preview functionality
Requirements
- iOS 16.0+ / macOS 13.0+
- Swift 5.9+
Installation
Swift Package Manager
Add the following to your Package.swift file:
dependencies: [
.package(url: "https://github.com/yourusername/AudioPlaybackKit.git", from: "1.0.0")
]
Or add it through Xcode:
- File → Add Package Dependencies
- Enter the repository URL
- Select the version and add to your target
Usage
Basic Audio Playback
import AudioPlaybackKit
// Get the shared player instance
let player = NoisePlayer.shared
// Create a sound
let sound = Sound(
name: "White Noise",
fileName: "white-noise.mp3",
category: "ambient",
description: "Classic white noise"
)
// Play the sound
player.playSound(sound)
// Stop the sound
player.stopSound()
Using the ViewModel
import AudioPlaybackKit
// Create a view model
let viewModel = NoiseViewModel()
// Get available sounds
let sounds = viewModel.availableSounds
// Play a sound
viewModel.playSound(sounds.first!)
// Preview a sound (3 seconds)
viewModel.previewSound(sounds.first!)
// Stop playback
viewModel.stopSound()
Sound Configuration
Create a sounds.json file in your app bundle:
{
"sounds": [
{
"id": "white-noise",
"name": "White Noise",
"fileName": "white-noise.mp3",
"category": "ambient",
"description": "Classic white noise for focus and relaxation",
"bundleName": "Ambient"
}
],
"categories": [
{
"id": "ambient",
"name": "Ambient",
"description": "General ambient sounds",
"bundleName": "Ambient"
}
],
"settings": {
"defaultVolume": 0.8,
"defaultLoopCount": -1,
"preloadSounds": true,
"preloadStrategy": "category",
"audioSessionCategory": "playback",
"audioSessionMode": "default",
"audioSessionOptions": ["mixWithOthers"]
}
}
Wake Lock Service
import AudioPlaybackKit
let wakeLock = WakeLockService.shared
// Enable wake lock (prevents device from sleeping)
wakeLock.enableWakeLock()
// Disable wake lock
wakeLock.disableWakeLock()
// Check if active
if wakeLock.isActive {
print("Wake lock is active")
}
Architecture
Core Components
- NoisePlayer: Main audio playback service
- SoundConfigurationService: Manages sound configuration and loading
- WakeLockService: Prevents device from sleeping during playback
- NoiseViewModel: SwiftUI-friendly view model for audio playback
Models
- Sound: Represents an audio file with metadata
- SoundConfiguration: JSON configuration structure
- AudioSettings: Audio session and playback settings
License
This package is available under the MIT license. See the LICENSE file for more info.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.