Skip to content
This repository has been archived by the owner on May 28, 2021. It is now read-only.

Commit

Permalink
updated to use a better method of audio playback...
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrian Edwards authored and Adrian Edwards committed Apr 8, 2016
1 parent f75d5e9 commit cdd318b
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 45 deletions.
13 changes: 13 additions & 0 deletions MiamiSunglasses.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
3A5AD02F1CB80AF500F1EB27 /* AVFoundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3A5AD02D1CB80AF500F1EB27 /* AVFoundation.framework */; };
3A8912521BB634F7004A780B /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A8912511BB634F7004A780B /* AppDelegate.swift */; };
3A8912541BB634F7004A780B /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A8912531BB634F7004A780B /* ViewController.swift */; };
3A8912571BB634F7004A780B /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 3A8912551BB634F7004A780B /* Main.storyboard */; };
Expand Down Expand Up @@ -35,6 +36,7 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
3A5AD02D1CB80AF500F1EB27 /* AVFoundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AVFoundation.framework; path = System/Library/Frameworks/AVFoundation.framework; sourceTree = SDKROOT; };
3A89124E1BB634F7004A780B /* MiamiSunglasses.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = MiamiSunglasses.app; sourceTree = BUILT_PRODUCTS_DIR; };
3A8912511BB634F7004A780B /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
3A8912531BB634F7004A780B /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
Expand All @@ -56,6 +58,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
3A5AD02F1CB80AF500F1EB27 /* AVFoundation.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -76,6 +79,15 @@
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
3A5AD0301CB81A9400F1EB27 /* Frameworks */ = {
isa = PBXGroup;
children = (
3A5AD02D1CB80AF500F1EB27 /* AVFoundation.framework */,
);
name = Frameworks;
path = ..;
sourceTree = "<group>";
};
3A8912451BB634F7004A780B = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -106,6 +118,7 @@
3A89125A1BB634F7004A780B /* LaunchScreen.storyboard */,
3A89125D1BB634F7004A780B /* Info.plist */,
3A89127F1BB63D9B004A780B /* CSI Miami.mp3 */,
3A5AD0301CB81A9400F1EB27 /* Frameworks */,
);
path = MiamiSunglasses;
sourceTree = "<group>";
Expand Down
3 changes: 3 additions & 0 deletions MiamiSunglasses/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.

//show spash screen for 3 seconds to allow user to read it...
sleep(3)
return true
}

Expand Down
4 changes: 2 additions & 2 deletions MiamiSunglasses/Base.lproj/LaunchScreen.storyboard
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="9532" systemVersion="14F1509" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" initialViewController="01J-lp-oVM">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="10116" systemVersion="15D21" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" initialViewController="01J-lp-oVM">
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="9530"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="10085"/>
<capability name="Constraints to layout margins" minToolsVersion="6.0"/>
<capability name="Constraints with non-1.0 multipliers" minToolsVersion="5.1"/>
</dependencies>
Expand Down
4 changes: 2 additions & 2 deletions MiamiSunglasses/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="9532" systemVersion="14F1509" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="BYZ-38-t0r">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="10116" systemVersion="15D21" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="BYZ-38-t0r">
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="9530"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="10085"/>
<capability name="Constraints to layout margins" minToolsVersion="6.0"/>
</dependencies>
<scenes>
Expand Down
Binary file modified MiamiSunglasses/CSI Miami.mp3
Binary file not shown.
76 changes: 35 additions & 41 deletions MiamiSunglasses/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,73 +9,67 @@
import UIKit
import AVFoundation

class ViewController: UIViewController {

class ViewController: UIViewController, AVAudioPlayerDelegate {


var yeah:AVAudioPlayer = AVAudioPlayer()
var soundIsPlaying:Bool = false;


var bgMusic:AVAudioPlayer = AVAudioPlayer()
var counter = 0;
var timer = NSTimer();

override func viewDidLoad() {
super.viewDidLoad()


let bgMusicURL:NSURL = NSBundle.mainBundle().URLForResource("CSI Miami", withExtension: "mp3")!
//load resource
let audioURL:
NSURL = NSBundle.mainBundle().URLForResource("CSI Miami", withExtension: "mp3")!

do {
bgMusic = try AVAudioPlayer(contentsOfURL: bgMusicURL, fileTypeHint: nil) } catch _ { return }
bgMusic.numberOfLoops = 1
bgMusic.prepareToPlay()
yeah = try AVAudioPlayer(contentsOfURL: audioURL, fileTypeHint: nil)
yeah.delegate = self; //allows audioPlayerDidFinishPlaying to be called
} catch _ { return }

yeah.numberOfLoops = 1
//yeah.volume = 1.0 //interesting... maybe i could add a volume slider later...
yeah.prepareToPlay()




}

@IBAction func playSound(sender: AnyObject) {
if !timer.valid {
print(soundIsPlaying)

//print("HIIIIII", appendNewline: true);
startTimer();
bgMusic.play()
if !soundIsPlaying {


yeah.play()
soundIsPlaying = true
}
else
{
stopMusic()
yeah.stop()
yeah.currentTime = 0;

soundIsPlaying = false
}
}

func update() {

if (counter == 10) {

stopMusic()

}
func audioPlayerDidFinishPlaying(player: AVAudioPlayer, successfully flag: Bool) {

counter += 1;
// print(counter, appendNewline: true);
soundIsPlaying = false


}

func stopMusic() {

bgMusic.stop()
bgMusic.currentTime = 0;
counter = 0;
timer.invalidate()
}

func startTimer() {

timer = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: #selector(ViewController.update), userInfo: nil, repeats: true)
}





override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


}

0 comments on commit cdd318b

Please sign in to comment.