AudioPlayer is a simple class for playing audio in iOS, macOS and tvOS apps.

Overview

AudioPlayer

Travis Language CocoaPods Carthage compatible Platform

AudioPlayer is a simple class for playing audio in iOS, macOS and tvOS apps.

Usage

// Initialize
let audioPlayer = AudioPlayer("sound.mp3")

// Start playing
audioPlayer.play()

// Stop playing with a fade out
audioPlayer.fadeOut()

See the samples project to see advanced usage

Installation

CocoaPods

Add the following to your Podfile:

pod 'AudioPlayerSwift'

Carthage

Add the following to your Cartfile:

github 'tbaranes/AudioPlayerSwift'

Swift Package Manager

AudioPlayer is available on SPM. Just add the following to your Package file:

import PackageDescription

let package = Package(
    dependencies: [
        .Package(url: "https://github.com/tbaranes/AudioPlayerSwift.git", majorVersion: 1)
    ]
)

Manual Installation

Just drag the Source/*.swift files into your project.

AudioPlayer properties

name

The name of the sound. This is either the name that was passed to the init, or the last path component of the audio file.

url

The absolute URL of the audio file.

completionHandler

A callback closure that will be called when the audio finishes playing, or is stopped.

isPlaying

Is it playing or not?

duration

The duration of the sound.

currentTime

The current time offset into the sound of the current playback position.

volume

The volume for the sound. The nominal range is from 0.0 to 1.0.

numberOfLoops

Number of times that the sound will return to the beginning upon reaching the end.

  • A value of zero means to play the sound just once.
  • A value of one will result in playing the sound twice, and so on..
  • Any negative number will loop indefinitely until stopped.
pan

The left/right stereo pan of the file. -1.0 is left, 0.0 is center, 1.0 is right.

AudioPlayer methods

init(fileName: String) throws
init(contentsOfPath path: String) throws
init(contentsOf url: URL) throws

These methods create a new AudioPlayer instance from a file name or file path.

func play()

Plays the sound. Has no effect if the sound is already playing.

func stop()

Stops the sound. Has no effect if the sound is not already playing.

func fadeTo(volume: Float, duration: TimeInterval = 1.0)

This method fades a sound from it's current volume to the specified volume over the specified time period.

func fadeIn(duration: TimeInterval = 1.0)

Fades the sound volume from 0.0 to 1.0 over the specified duration.

func fadeOut(duration: TimeInterval = 1.0)

Fades the sound from it's current volume to 0.0 over the specified duration.

Notifications

SoundDidFinishPlayingNotification

This notification is fired (via NSNotificationCenter) whenever a sound finishes playing, either due to it ending naturally, or because the stop method was called. The notification object is an instance of the AudioPlayer class. You can access the AudioPlayer class's name property to find out which sound has finished.

What's next

  • AudioPlayerManager
  • Your features!

Contribution

  • If you found a bug, open an issue
  • If you have a feature request, open an issue
  • If you want to contribute, submit a pull request

Licence

AudioPlayerSwift is available under the MIT license. See the LICENSE file for more info.

Comments
  • Propose Logo

    Propose Logo

    Hi @tbaranes . I'm graphic disigner. I would like to know if you are interested that I make a logo for your project? If you allowed me, i"ll make logo for your project and it's free. Best Regard

    opened by mirzazulfan 5
  • None of your accounts are a member of 'TNVENL2JX3' error

    None of your accounts are a member of 'TNVENL2JX3' error

    Hi, Could you set "team" to none inside the General/Signing section of the settings in the AudioPlayerSwift.xcodeproj you ship ? I need to do it manually after updating carthage, otherwise when trying to build my project, i'm getting this error: "None of your accounts are a member of 'TNVENL2JX3': An unexpected error occurred. Xcode cannot find a team matching 'TNVENL2JX3'."

    opened by frranck 5
  • Ensures the Timer is scheduled on the main thread

    Ensures the Timer is scheduled on the main thread

    This actually got me. I was invoking a fadeTo as the result of a message from an XPC service and it wasn't working. The Timer needs to be scheduled on the main thread. This ensures that is the case.

    opened by theladyjaye 3
  • Improve SoundDidFinishPlayingNotification by including completion success in notification

    Improve SoundDidFinishPlayingNotification by including completion success in notification

    Modules wanting to be notified about the completion of an AudioPlayer playing a sound have two options:

    1. Provide completion handler, but only be notified about whether play completed successfully
    2. Register for NotificationCenter, but only be notified about the player that completed

    This creates an issue when the module wants to do something specifically based on the completion success while not knowing which of potentially multiple AudioPlayers is sending the message.

    Fixing #1 requires changing the method signature. While I would encourage this to be implemented as it makes the completion handler more powerful, it will break the current API.

    Fixing #2 is easy and non-breaking. I have implemented this here by adding a userInfo dictionary to the Notification including the boolean completion success.


    I have also made a few small code changes to the init methods to use best Swift practices.

    opened by jashenson 2
  • Compile error on swift 4

    Compile error on swift 4

    Hello,

    I have used successfully this library but had to make a small change in order to compile it for swift v 4.

    I am using xcode 9,1 here and the default configurations for a swift project.

    On compiling, xcode complained that the HandleFadeTo function (line 86 currently) was not able to get seen by objective c and asked to include an @objc directive before it.

    I did exactly that and then the compile went smoothly.

    I wanted to submit a pool request but git specially on github is still confusing enough that I would not risk playing with branches on a public project, not yet at least. But here it is if you want to patch it yourself or for other people who happen to have the same issue.

    opened by marlon-sousa 2
  • Song is silent in iOS 10

    Song is silent in iOS 10

    Hi, I use this library in iOS 11 (simulator) and work well. But, when i use in my iOS 10 real device, the song is silent. why is this happening? is it because ios version? Thank

    opened by bungkhus 2
  • Adds the ability to initialize the player with Data

    Adds the ability to initialize the player with Data

    AVAudioPlayer provides a data initialization. So in a similar vein to the other AudioPlayer initializers, this provides a wrapper around AVAudioPlayer's data initializer.

    This is useful when dealing with audio from sources like FileWrapper which only allows access to Data not a URL.

    opened by theladyjaye 2
  • Wrong duration time.

    Wrong duration time.

    Hi,

    I use this library to play some mp3 files, but always audioPlayer.duration time is incorrect. It is with approximately one minute less. I think that AVAudioPlayer doesn't render correct the audio file. For example for this file http://mybible.ro/audio/geneza-1.mp3 on the webpage duration is 6:23, while on iOS app is 5:06...

    Any suggestions?

    opened by Senocico 2
  • Set name

    Set name

    Please make it possible to set the name property so it is useable, so that it is possible to compare name to a custom variable to see if it is the song currently playing. Either change it to var, make a setter method or constructor argument.

    opened by mortenholmgaard 2
  • Streaming and caching

    Streaming and caching

    Is possible do such things with the library?

    start playing .mp3 from url immediately (like streaming or streaming) cache it and track caching progress during it's playback save cached file to local storage

    opened by bizibizi 1
  • Correct the spelling of CocoaPods in README

    Correct the spelling of CocoaPods in README

    This pull requests corrects the spelling of CocoaPods 🤓 https://github.com/CocoaPods/shared_resources/tree/master/media

    opened by ReadmeCritic 1
Releases(2.0.0)
Owner
Tom Baranes
👋
Tom Baranes
An iOS and macOS audio visualization framework built upon Core Audio useful for anyone doing real-time, low-latency audio processing and visualizations.

A simple, intuitive audio framework for iOS and OSX. Deprecated EZAudio has recently been deprecated in favor of AudioKit. However, since some people

Syed Haris Ali 4.9k Jan 2, 2023
AIB indicates for your app users which audio is playing. Just like the Podcasts app.

Audio Indicator Bars for iOS and tvOS Indicates for your app users which audio is playing. Just like the Podcasts app. Index Requirements and Details

Leonardo Cardoso 285 Nov 23, 2022
AudiosPlugin is a Godot iOS Audio Plugin that resolves the audio recording issue in iOS for Godot Engine.

This plugin solves the Godot game engine audio recording and playback issue in iOS devices. Please open the Audios Plugin XCode Project and compile the project. You can also use the libaudios_plugin.a binary in your project.

null 3 Dec 22, 2022
The Amazing Audio Engine is a sophisticated framework for iOS audio applications, built so you don't have to.

Important Notice: The Amazing Audio Engine has been retired. See the announcement here The Amazing Audio Engine The Amazing Audio Engine is a sophisti

null 523 Nov 12, 2022
AudioKit is an audio synthesis, processing, and analysis platform for iOS, macOS, and tvOS.

AudioKit is an audio synthesis, processing, and analysis platform for iOS, macOS (including Catalyst), and tvOS. Installation To add AudioKit

AudioKit 9.5k Dec 31, 2022
Swift audio synthesis, processing, & analysis platform for iOS, macOS and tvOS

AudioKit AudioKit is an audio synthesis, processing, and analysis platform for iOS, macOS (including Catalyst), and tvOS. Installation To add AudioKit

AudioKit 8.7k Sep 30, 2021
Soundable is a tiny library that uses AVFoundation to manage the playing of sounds in iOS applications in a simple and easy way

Soundable is a tiny library that uses AVFoundation to manage the playing of sounds in iOS applications in a simple and easy way. You can play

Luis Cárdenas 89 Nov 21, 2022
Simple command line utility for switching audio inputs and outputs on macOS

Switch Audio Simple command line utility for switching audio inputs and outputs

Daniel Hladík 3 Nov 22, 2022
Wordlebutimstupid - Fixed shortfall in intelligence when playing wordle

wordlebutimstupid fixed shortfall in intelligence when playing wordle ⬛️⬛️⬛️⬛️⬛️

Jia Chen 3 Feb 18, 2022
Creating Custom Audio Effects - Simple Universal Version (Mac Catalyst)

Creating Custom Audio Effects - Simple Universal Version (Mac Catalyst) Adaptation of the sample provided by Apple Creating Custom Audio Effects. Ever

Fred Anton Corvest (FAC) 19 Nov 2, 2022
Voice Memos is an audio recorder App for iPhone and iPad that covers some of the new technologies and APIs introduced in iOS 8 written in Swift.

VoiceMemos Voice Memos is a voice recorder App for iPhone and iPad that covers some of the new technologies and APIs introduced in iOS 8 written in Sw

Zhouqi Mo 322 Aug 4, 2022
Painless high-performance audio on iOS and Mac OS X

An analgesic for high-performance audio on iOS and OSX. Really fast audio in iOS and Mac OS X using Audio Units is hard, and will leave you scarred an

Alex Wiltschko 2.2k Nov 23, 2022
Audio Filters on iOS and OSX

Audio Filters on iOS and OSX Implement high quality audio filters with just a few lines of code and Novocaine, or your own audio library of choice. NV

Bart Olsthoorn 411 Dec 16, 2022
YiVideoEditor is a library for rotating, cropping, adding layers (watermark) and as well as adding audio (music) to the videos.

YiVideoEditor YiVideoEditor is a library for rotating, cropping, adding layers (watermark) and as well as adding audio (music) to the videos. YiVideoE

coderyi 97 Dec 14, 2022
App for adding and listening audio files

SomeSa SomeSa (самса) – приложение, позволяющее загружать и воспроизводить произвольные аудиофайлы. Протестировано на форматах файлов .wav и .mp3, раз

Yegor Dobrodeyev 0 Nov 7, 2021
This app demonstrates how to use the Google Cloud Speech API and Apple on-device Speech library to recognize speech in live recorded audio.

SpeechRecognitionIOS This app demonstrates how to use Google Cloud Speech API and Apple on-device Speech library to recognize speech in live audio rec

Josh Uvi 0 Mar 11, 2022
Beethoven is an audio processing Swift library

Beethoven is an audio processing Swift library that provides an easy-to-use interface to solve an age-old problem of pitch detection of musical signals.

Vadym Markov 735 Dec 24, 2022
FDWaveformView is an easy way to display an audio waveform in your app

FDWaveformView is an easy way to display an audio waveform in your app. It is a nice visualization to show a playing audio file or to select a position in a file.

William Entriken 1.1k Dec 21, 2022
SwiftAudioPlayer - Swift-based audio player with AVAudioEngine as its base

SwiftAudioPlayer Swift-based audio player with AVAudioEngine as its base. Allows for: streaming online audio, playing local file, changing audio speed

null 417 Jan 7, 2023