GithubHelp home page GithubHelp logo

crashoverride777 / swifty-music Goto Github PK

View Code? Open in Web Editor NEW
8.0 2.0 3.0 12.25 MB

A Swift library to handle music playback using AVFoundation. This library is deprecated and no longer supported.

License: MIT License

Swift 94.77% Objective-C 2.53% Ruby 2.71%

swifty-music's Introduction

Swift 5.6 Platform SPM supported CocoaPods Compatible

SwiftyMusic

A Swift library to handle music playback using AVFoundation.

Deprecation

This library is deprecated and no longer supported.

Requirements

  • iOS 12.0+
  • Swift 5.6+

Installation

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.0.0")
]

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'

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 all file formats supported by AVFoundation.

  • 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.mp3")
    static let game = SwiftyMusicFileName("GameMusic.mp3")
    
    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.

swifty-music's People

Contributors

crashoverride777 avatar dominik777 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

swifty-music's Issues

how to receive different events from player?

I would like to catch notifications of different events like music stopped, etc etc?

Is there a way to do that with this library using delegation instead of NSNotificationCenter?

Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.