Swiftpack.co -  crashoverride777/swifty-music as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
crashoverride777/swifty-music
A Swift library to handle music playback using AVFoundation.
.package(url: "https://github.com/crashoverride777/swifty-music.git", from: "v4.1.5")

Swift 5.0 Platform SPM supported CocoaPods Compatible

SwiftyMusic

A Swift library to handle music playback using AVFoundation.

Requirements

  • iOS 11.4+
  • Swift 5.0+

Installation

Cocoa Pods

CocoaPods is a dependency manager for Cocoa projects. Simply install the pod by adding the following line to your pod file

pod 'SwiftyMusic'

Swift Package Manager

The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift compiler.

To add a swift package to your project simple open your project in xCode and click File > Swift Packages > Add Package Dependency. Than enter https://github.com/crashoverride777/swifty-music.git as the repository URL and finish the setup wizard.

Alternatively if you have a Swift package that requires adding SwiftyMusic as a dependency it is as easy as adding it to the dependencies value of your Package.swift.

dependencies: [
    .package(url: "https://github.com/crashoverride777/swifty-music.git", from: "4.4.0")
]

Manually

Alternatively you can drag the Sources folder and its containing files into your project.

Usage

  • Add your music tracks to your project.

SwiftyMusic supports the following file formats: mp3, wav, aac, ac3, m4a, caf

  • Add the import statements to your .swift file(s) if you installed via cocoa pods or swift package manager.
import SwiftyMusic 
  • Anywhere in your project create an extension of SwiftyMusicFileName to add the file names of the music tracks that you will use. These must be the same as the actual filename of the music file.
extension SwiftyMusicFileName {
    static let menu = SwiftyMusicFileName("MenuMusic")
    static let game = SwiftyMusicFileName("GameMusic")
    
    static var all: [SwiftyMusicFileName] = [.menu, .game]
}
  • Than setup the helper as soon as your app launches.
SwiftyMusic.shared.setup(withFileNames: SwiftyMusicFileName.all)
  • To play music call the play method with the corresponding Music URL you have created above. This will automatically pause any previously playing music
SwiftyMusic.shared.play(.menu)
SwiftyMusic.shared.play(.game)
  • Pause music
SwiftyMusic.shared.pause()
  • Resume paused music
SwiftyMusic.shared.resume()
  • Adjust volume
SwiftyMusic.shared.setVolume(to: 0.5)
  • Stop and reset all music players
SwiftyMusic.shared.reset()
  • Mute
SwiftyMusic.shared.setMuted(true)

if SwiftyMusic.shared.isMuted {
    // music is muted, show unmute button
} else {
    // music not muted, show mute button
}

Testing

To test your classes using SwiftyMusic you can inject the SwiftyMusicType protocol

class SomeClass {
    private let swiftyMusic: SwiftyMusicType
    
    init(swiftyMusic: SwiftyMusicType = SwiftyMusic.shared) {
        self.swiftyMusic = swiftyMusic
    }
}

and than provide a mock implementation when testing

class MockSwiftyMusic { }
extension MockSwiftyMusic: SwiftyMusicType { ... }

class SomeClassTests {
    func test() {
        let sut = SomeClass(swiftyMusic: MockSwiftyMusic())
    }
}

License

SwiftyMusic is released under the MIT license. See LICENSE for details.

GitHub

link
Stars: 7
Last commit: 4 weeks ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Release Notes

Readme
14 weeks ago
  • Readme updates and license fixes

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API