Skip to content

Commit

Permalink
Add SoundManager to play sound and infinite loop
Browse files Browse the repository at this point in the history
(double click on cell to stop sound)
  • Loading branch information
Nicolas HOAREAU committed Mar 22, 2016
1 parent d398132 commit 4576f46
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 15 deletions.
12 changes: 12 additions & 0 deletions QuickSound.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
2228E3051CA1B1CE0041056A /* Sound+CoreDataProperties.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2228E3031CA1B1CE0041056A /* Sound+CoreDataProperties.swift */; };
2228E3061CA1B1CE0041056A /* Sound.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2228E3041CA1B1CE0041056A /* Sound.swift */; };
2228E3081CA1B3380041056A /* DataManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2228E3071CA1B3380041056A /* DataManager.swift */; };
3717907A1CA1FADC00A622A5 /* SoundManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 371790791CA1FADC00A622A5 /* SoundManager.swift */; };
92BE1D781C9C4B9B004F13BB /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 92BE1D771C9C4B9B004F13BB /* AppDelegate.swift */; };
92BE1D7A1C9C4B9B004F13BB /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 92BE1D791C9C4B9B004F13BB /* Assets.xcassets */; };
92BE1D861C9C4CA5004F13BB /* StatusItemController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 92BE1D851C9C4CA5004F13BB /* StatusItemController.swift */; };
Expand All @@ -28,6 +29,7 @@
2228E3031CA1B1CE0041056A /* Sound+CoreDataProperties.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Sound+CoreDataProperties.swift"; sourceTree = "<group>"; };
2228E3041CA1B1CE0041056A /* Sound.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Sound.swift; sourceTree = "<group>"; };
2228E3071CA1B3380041056A /* DataManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DataManager.swift; sourceTree = "<group>"; };
371790791CA1FADC00A622A5 /* SoundManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SoundManager.swift; sourceTree = "<group>"; };
92BE1D741C9C4B9B004F13BB /* QuickSound.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = QuickSound.app; sourceTree = BUILT_PRODUCTS_DIR; };
92BE1D771C9C4B9B004F13BB /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
92BE1D791C9C4B9B004F13BB /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
Expand All @@ -51,6 +53,14 @@
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
371790781CA1FABF00A622A5 /* Manager */ = {
isa = PBXGroup;
children = (
371790791CA1FADC00A622A5 /* SoundManager.swift */,
);
name = Manager;
sourceTree = "<group>";
};
92BE1D6B1C9C4B9B004F13BB = {
isa = PBXGroup;
children = (
Expand All @@ -71,6 +81,7 @@
isa = PBXGroup;
children = (
92BE1D771C9C4B9B004F13BB /* AppDelegate.swift */,
371790781CA1FABF00A622A5 /* Manager */,
92BE1D8C1C9C5079004F13BB /* Status Item */,
92BE1D8B1C9C506E004F13BB /* Popover */,
E96D54071C9CAC08006286EB /* Model */,
Expand Down Expand Up @@ -225,6 +236,7 @@
E942CF291CA1FBF8004FBCD2 /* Utils.swift in Sources */,
92BE1D891C9C506B004F13BB /* PopoverViewController.swift in Sources */,
2228E3061CA1B1CE0041056A /* Sound.swift in Sources */,
3717907A1CA1FADC00A622A5 /* SoundManager.swift in Sources */,
92BE1D781C9C4B9B004F13BB /* AppDelegate.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
24 changes: 9 additions & 15 deletions QuickSound/PopoverViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ class PopoverViewController: NSViewController {
@IBOutlet var soundsArrayController: NSArrayController!
@IBOutlet weak var tableView: NSTableView!

private var soundPlayer:[NSSound] = []

private let soundManager:SoundManager = SoundManager()

// MARK: - Lifecycle

Expand All @@ -32,7 +31,7 @@ class PopoverViewController: NSViewController {
super.viewWillDisappear()

// Deselect table view
self.tableView.deselectAll(nil)
self.tableView.deselectAll(nil)
}


Expand All @@ -44,18 +43,13 @@ class PopoverViewController: NSViewController {
guard let soundObject = arrangedObjects[tableView.clickedRow] as? Sound else { return }

// Play sound
if let soundURL = NSURL(string: soundObject.filePath!) {
let sound = NSSound(contentsOfURL: soundURL, byReference: true)
let successOrNil = sound?.play()

// Show alert
if successOrNil == nil || !successOrNil! {
let alert = NSAlert()
alert.messageText = "Impossible de lire le son"
alert.informativeText = "Le fichier est invalide ou introuvable."
alert.alertStyle = .CriticalAlertStyle
alert.runModal()
}
let repeatEnabled = soundObject.repeatEnabled!.boolValue
let filePath = soundObject.filePath!

if repeatEnabled && self.soundManager.soundIsPlaying(filePath) {
self.soundManager.stopSound(atPath: filePath)
} else {
self.soundManager.playSound(atPath: filePath, repeatSound: repeatEnabled)
}
}

Expand Down
89 changes: 89 additions & 0 deletions QuickSound/SoundManager.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
//
// SoundManager.swift
// QuickSound
//
// Created by Nicolas HOAREAU on 22/03/2016.
// Copyright © 2016 Thomas Di Meco. All rights reserved.
//

import Foundation
import Cocoa

class SoundManager:NSObject, NSSoundDelegate {

private var sounds:[NSSound] = []

/**
Use this method to play a sound
- parameter atPath: the path of the sound
- parameter repeatSound: a boolean to indicate if you want repeat the sound
*/
func playSound(atPath path: String, repeatSound: Bool) {

if let soundURL = NSURL(string: path) {
let sound = NSSound(contentsOfURL: soundURL, byReference: true)

sound?.loops = repeatSound
sound?.setName(path)

let successOrNil = sound?.play()

// Show alert
if successOrNil == nil || !successOrNil! {
let alert = NSAlert()
alert.messageText = "Impossible de lire le son"
alert.informativeText = "Le fichier est invalide ou introuvable."
alert.alertStyle = .CriticalAlertStyle
alert.runModal()
} else if repeatSound {
self.sounds.append(sound!)
}
}
}

/**
Use this method to stop a sound
- parameter atPath: the path of the sound
- returns: A boolean to indicate if sound is stopped
*/
func stopSound(atPath path: String) -> Bool {

return self.removeSound(path)
}

/**
Use this method to know if a sound if playing
- parameter name: the name of the sound
- returns: A boolean to indicate if sound is playing
*/
func soundIsPlaying(name: String) -> Bool {
for sound: NSSound in self.sounds {
if sound.name == name {
return true
}
}
return false
}

// MARK: - Private

private func removeSound(name: String) -> Bool {
var stopped = false
let soundsArray = NSArray(array:self.sounds)

for (index, value) in soundsArray.enumerate() {
let sound = value as! NSSound
if sound.name == name {
stopped = sound.stop()
self.sounds.removeAtIndex(index)
}
}
return stopped
}

}

0 comments on commit 4576f46

Please sign in to comment.