Latest 4.3.0
Homepage https://github.com/crashoverride777/swifty-music
License MIT
Platforms ios 10.3, requires ARC
Authors

Swift 5.0
[Platform]()
CocoaPods Compatible

A Swift helper to handle music playback using AVFoundation.

Requirements

  • iOS 10.3+
  • Swift 5.0+

Installation

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

pod 'SwiftyMusic'

There is now an app which makes handling pods much easier

Altenatively you can drag the swift file(s) manually 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.
import SwiftyMusic 
  • Anywhere in your project create an extension of FileName 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 SwiftyMusic.FileName {
    static let menu = SwiftyMusic.FileName("MenuMusic")
    static let game = SwiftyMusic.FileName("GameMusic")

    static var all: [SwiftyMusic.FileName] = [.menu, .game]
}
  • Than setup the helper as soon as your app launches.
SwiftyMusic.shared.setup(withFileNames: SwiftyMusic.FileName.all)
  • To play music call the play method with the corresponding Music URL you created above. This will automatically pause (not stop and reset) any previously playing music

    SwiftyMusic.shared.play(.menu)
    SwiftyMusic.shared.play(.game)
  • To pause music manually, eg game paused, advertising etc

    SwiftyMusic.shared.pause()
  • To resume paused music

    SwiftyMusic.shared.resume()
  • To adjust volume (e.g game paused)

    SwiftyMusic.shared.setVolume(to: 0.5)
  • To reset volume (e.g game resumed)

    SwiftyMusic.shared.resetVolume()
  • To stop and reset all music players, eg gameover

    SwiftyMusic.shared.stopAndResetAll()
  • To mute music

    SwiftyMusic.shared.isMuted = true
  • To unmute music

    SwiftyMusic.shared.isMuted = false
  • To check if music is muted, eg when setting up your mute music button
    if SwiftyMusic.shared.isMuted {
    // music is muted, show unmute button
    } else {
    // music not muted, show mute button
    }

Latest podspec

{
    "name": "SwiftyMusic",
    "version": "4.3.0",
    "license": "MIT",
    "summary": "A swift helper to play music with AVFoundation.",
    "homepage": "https://github.com/crashoverride777/swifty-music",
    "social_media_url": "http://twitter.com/overrideiactive",
    "authors": {
        "Dominik": "[email protected]"
    },
    "requires_arc": true,
    "platforms": {
        "ios": "10.3"
    },
    "source": {
        "git": "https://github.com/crashoverride777/swifty-music.git",
        "tag": "4.3.0"
    },
    "source_files": "SwiftyMusic/**/*.{swift}"
}

Pin It on Pinterest

Share This