Skip to content

Commit 248510f

Browse files
committed
working on chapter 14 examples
1 parent 62c4941 commit 248510f

File tree

29 files changed

+404
-211
lines changed

29 files changed

+404
-211
lines changed

bk2ch14p640systemSound2/ch27p910systemSound/ViewController.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ import AudioToolbox
55

66

77
class ViewController: UIViewController {
8-
9-
// test on device (doesn't work in simulator)
10-
8+
119
// cool new iOS 9 way: pass the sound and the completion handler all in one call
1210

1311
@IBAction func doButton (_ sender: Any!) {

bk2ch14p640systemSound2/ch27p910systemSound2.xcodeproj/project.pbxproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@
243243
PRODUCT_NAME = ch27p910systemSound2;
244244
SWIFT_OBJC_BRIDGING_HEADER = "";
245245
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
246-
SWIFT_VERSION = 3.0;
246+
SWIFT_VERSION = 4.0;
247247
WRAPPER_EXTENSION = app;
248248
};
249249
name = Debug;
@@ -260,7 +260,7 @@
260260
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
261261
PRODUCT_NAME = ch27p910systemSound2;
262262
SWIFT_OBJC_BRIDGING_HEADER = "";
263-
SWIFT_VERSION = 3.0;
263+
SWIFT_VERSION = 4.0;
264264
WRAPPER_EXTENSION = app;
265265
};
266266
name = Release;

bk2ch14p643ducking/ch27p912ducking.xcodeproj/project.pbxproj

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
3280FA5E1997C57900EA8B8B /* Player.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3280FA5D1997C57900EA8B8B /* Player.swift */; };
1212
3280FA601997C97700EA8B8B /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3280FA5F1997C97700EA8B8B /* ViewController.swift */; };
1313
3280FA621997D07D00EA8B8B /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3280FA611997D07D00EA8B8B /* AppDelegate.swift */; };
14+
81FC4D0A1F90F63700262535 /* test.flac in Resources */ = {isa = PBXBuildFile; fileRef = 81FC4D091F90F63700262535 /* test.flac */; };
1415
C9592EC01823107000CE45EA /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = C9592EBE1823107000CE45EA /* Main.storyboard */; };
1516
C9592EC51823107000CE45EA /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = C9592EC41823107000CE45EA /* Assets.xcassets */; };
1617
C9592EE51823110800CE45EA /* test.aif in Resources */ = {isa = PBXBuildFile; fileRef = C9592EE41823110800CE45EA /* test.aif */; };
@@ -21,6 +22,7 @@
2122
3280FA5D1997C57900EA8B8B /* Player.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Player.swift; sourceTree = "<group>"; };
2223
3280FA5F1997C97700EA8B8B /* ViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
2324
3280FA611997D07D00EA8B8B /* AppDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
25+
81FC4D091F90F63700262535 /* test.flac */ = {isa = PBXFileReference; lastKnownFileType = file; path = test.flac; sourceTree = "<group>"; };
2426
C9592EA91823107000CE45EA /* ch27p912ducking.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = ch27p912ducking.app; sourceTree = BUILT_PRODUCTS_DIR; };
2527
C9592EB41823107000CE45EA /* ch27p912ducking-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "ch27p912ducking-Info.plist"; sourceTree = "<group>"; };
2628
C9592EBF1823107000CE45EA /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
@@ -63,6 +65,7 @@
6365
3280FA5F1997C97700EA8B8B /* ViewController.swift */,
6466
3280FA5D1997C57900EA8B8B /* Player.swift */,
6567
C9592EE41823110800CE45EA /* test.aif */,
68+
81FC4D091F90F63700262535 /* test.flac */,
6669
C9592EC41823107000CE45EA /* Assets.xcassets */,
6770
3241C22C1BB5C6F300D09188 /* LaunchScreen.storyboard */,
6871
C9592EB41823107000CE45EA /* ch27p912ducking-Info.plist */,
@@ -105,7 +108,7 @@
105108
LastSwiftMigration = 0800;
106109
SystemCapabilities = {
107110
com.apple.BackgroundModes = {
108-
enabled = 0;
111+
enabled = 1;
109112
};
110113
};
111114
};
@@ -134,6 +137,7 @@
134137
isa = PBXResourcesBuildPhase;
135138
buildActionMask = 2147483647;
136139
files = (
140+
81FC4D0A1F90F63700262535 /* test.flac in Resources */,
137141
C9592EC51823107000CE45EA /* Assets.xcassets in Resources */,
138142
3241C22D1BB5C6F300D09188 /* LaunchScreen.storyboard in Resources */,
139143
C9592EE51823110800CE45EA /* test.aif in Resources */,
@@ -254,7 +258,7 @@
254258
ONLY_ACTIVE_ARCH = NO;
255259
PRODUCT_NAME = "$(TARGET_NAME)";
256260
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
257-
SWIFT_VERSION = 3.0;
261+
SWIFT_VERSION = 4.0;
258262
TARGETED_DEVICE_FAMILY = "1,2";
259263
WRAPPER_EXTENSION = app;
260264
};
@@ -273,7 +277,7 @@
273277
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
274278
ONLY_ACTIVE_ARCH = NO;
275279
PRODUCT_NAME = "$(TARGET_NAME)";
276-
SWIFT_VERSION = 3.0;
280+
SWIFT_VERSION = 4.0;
277281
TARGETED_DEVICE_FAMILY = "1,2";
278282
WRAPPER_EXTENSION = app;
279283
};

bk2ch14p643ducking/ch27p912ducking/AppDelegate.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ class AppDelegate : UIResponder, UIApplicationDelegate {
1111
// new in iOS 9, can check beforehand
1212
let ok = AVAudioSession.sharedInstance().availableCategories.contains(AVAudioSessionCategoryAmbient)
1313
print(ok)
14-
try? AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)
14+
let cat = AVAudioSession.sharedInstance().category
15+
print(cat)
16+
// default is solo ambient
1517

1618
// deliberate leak here
1719

@@ -33,6 +35,7 @@ class AppDelegate : UIResponder, UIApplicationDelegate {
3335
print("interruption began:\n\(n.userInfo!)")
3436
} else {
3537
print("interruption ended:\n\(n.userInfo!)")
38+
try? AVAudioSession.sharedInstance().setActive(true)
3639
guard let opt = n.userInfo![AVAudioSessionInterruptionOptionKey] as? UInt else {return}
3740
if AVAudioSessionInterruptionOptions(rawValue:opt).contains(.shouldResume) {
3841
print("should resume")
@@ -61,7 +64,6 @@ class AppDelegate : UIResponder, UIApplicationDelegate {
6164

6265
func applicationDidBecomeActive(_ application: UIApplication) {
6366
print("app became active")
64-
try? AVAudioSession.sharedInstance().setActive(true)
6567
}
6668

6769
func applicationWillResignActive(_ application: UIApplication) {
@@ -71,4 +73,8 @@ class AppDelegate : UIResponder, UIApplicationDelegate {
7173
func applicationDidEnterBackground(_ application: UIApplication) {
7274
print("app did enter background")
7375
}
76+
77+
func applicationWillEnterForeground(_ application: UIApplication) {
78+
print("app will enter foreground")
79+
}
7480
}
Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="6205" systemVersion="13E28" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" initialViewController="vXZ-lx-hvc">
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13528" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" colorMatched="YES" initialViewController="vXZ-lx-hvc">
3+
<device id="retina4_7" orientation="portrait">
4+
<adaptation id="fullscreen"/>
5+
</device>
36
<dependencies>
4-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6198"/>
7+
<deployment identifier="iOS"/>
8+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13526"/>
9+
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
510
</dependencies>
611
<scenes>
712
<!--View Controller-->
@@ -13,56 +18,50 @@
1318
<viewControllerLayoutGuide type="bottom" id="FWu-lM-DgI"/>
1419
</layoutGuides>
1520
<view key="view" contentMode="scaleToFill" id="kh9-bI-dsS">
16-
<rect key="frame" x="0.0" y="0.0" width="320" height="568"/>
21+
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
1722
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
1823
<subviews>
1924
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="PYP-s7-f1D">
20-
<rect key="frame" x="131" y="37" width="37" height="30"/>
25+
<rect key="frame" x="169" y="37" width="37" height="30"/>
2126
<state key="normal" title="Once">
22-
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
27+
<color key="titleShadowColor" red="0.5" green="0.5" blue="0.5" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
2328
</state>
2429
<connections>
25-
<action selector="doButton:" destination="vXZ-lx-hvc" eventType="touchUpInside" id="fwk-A6-DeQ"/>
30+
<action selector="doDuck:" destination="vXZ-lx-hvc" eventType="touchUpInside" id="f4x-fE-NrT"/>
2631
</connections>
2732
</button>
2833
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="hoa-Xy-UTv">
29-
<rect key="frame" x="123" y="129" width="52" height="30"/>
30-
<state key="normal" title="Forever">
31-
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
34+
<rect key="frame" x="160" y="129" width="55" height="30"/>
35+
<state key="normal" title="Looping">
36+
<color key="titleShadowColor" red="0.5" green="0.5" blue="0.5" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
3237
</state>
3338
<connections>
34-
<action selector="doButton:" destination="vXZ-lx-hvc" eventType="touchUpInside" id="Vr5-L5-pzD"/>
39+
<action selector="doLoop:" destination="vXZ-lx-hvc" eventType="touchUpInside" id="81m-vI-j5c"/>
40+
</connections>
41+
</button>
42+
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="gWS-N5-8XR">
43+
<rect key="frame" x="171.5" y="224" width="32" height="30"/>
44+
<state key="normal" title="Stop">
45+
<color key="titleShadowColor" red="0.5" green="0.5" blue="0.5" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
46+
</state>
47+
<connections>
48+
<action selector="doStop:" destination="vXZ-lx-hvc" eventType="touchUpInside" id="7Ke-GS-f89"/>
3549
</connections>
3650
</button>
37-
<label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="To stop after tapping Forever, summon Control Center to see remote controls, tap playpause button." textAlignment="center" lineBreakMode="wordWrap" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" preferredMaxLayoutWidth="280" translatesAutoresizingMaskIntoConstraints="NO" id="veA-6I-L3B">
38-
<rect key="frame" x="20" y="202" width="280" height="62"/>
39-
<fontDescription key="fontDescription" style="UICTFontTextStyleBody"/>
40-
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
41-
<nil key="highlightedColor"/>
42-
</label>
4351
</subviews>
44-
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
52+
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
4553
<constraints>
54+
<constraint firstItem="gWS-N5-8XR" firstAttribute="top" secondItem="hoa-Xy-UTv" secondAttribute="bottom" constant="65" id="03d-Ng-99n"/>
4655
<constraint firstItem="hoa-Xy-UTv" firstAttribute="top" secondItem="PYP-s7-f1D" secondAttribute="bottom" constant="62" id="EJh-Dg-2XR"/>
47-
<constraint firstAttribute="trailing" secondItem="veA-6I-L3B" secondAttribute="trailing" constant="20" id="IYT-58-bOM"/>
56+
<constraint firstItem="gWS-N5-8XR" firstAttribute="centerX" secondItem="kh9-bI-dsS" secondAttribute="centerX" id="P2z-LK-dLU"/>
4857
<constraint firstItem="PYP-s7-f1D" firstAttribute="top" secondItem="Vun-3T-snA" secondAttribute="bottom" constant="17" id="QLF-1K-AEy"/>
49-
<constraint firstAttribute="centerX" secondItem="PYP-s7-f1D" secondAttribute="centerX" constant="10.5" id="QgB-84-Pb0"/>
50-
<constraint firstItem="veA-6I-L3B" firstAttribute="leading" secondItem="kh9-bI-dsS" secondAttribute="leading" constant="20" id="XmN-5X-now"/>
51-
<constraint firstAttribute="centerX" secondItem="hoa-Xy-UTv" secondAttribute="centerX" constant="11" id="e3B-ae-lF7"/>
52-
<constraint firstItem="veA-6I-L3B" firstAttribute="top" secondItem="hoa-Xy-UTv" secondAttribute="bottom" constant="43" id="tOD-8o-Ktd"/>
58+
<constraint firstAttribute="centerX" secondItem="PYP-s7-f1D" secondAttribute="centerX" id="QgB-84-Pb0"/>
59+
<constraint firstAttribute="centerX" secondItem="hoa-Xy-UTv" secondAttribute="centerX" id="e3B-ae-lF7"/>
5360
</constraints>
5461
</view>
5562
</viewController>
5663
<placeholder placeholderIdentifier="IBFirstResponder" id="x5A-6p-PRh" sceneMemberID="firstResponder"/>
5764
</objects>
5865
</scene>
5966
</scenes>
60-
<simulatedMetricsContainer key="defaultSimulatedMetrics">
61-
<simulatedStatusBarMetrics key="statusBar"/>
62-
<simulatedOrientationMetrics key="orientation"/>
63-
<simulatedScreenMetrics key="destination" type="retina4">
64-
<size key="portraitSize" width="320" height="568"/>
65-
<size key="landscapeSize" width="568" height="320"/>
66-
</simulatedScreenMetrics>
67-
</simulatedMetricsContainer>
6867
</document>

bk2ch14p643ducking/ch27p912ducking/Player.swift

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ protocol PlayerDelegate : class {
1010

1111
class Player : NSObject, AVAudioPlayerDelegate {
1212
var player : AVAudioPlayer!
13-
var forever = false
13+
var looping = false
1414
weak var delegate : PlayerDelegate?
1515

1616
func playFile(atPath path:String) {
@@ -22,7 +22,7 @@ class Player : NSObject, AVAudioPlayerDelegate {
2222
// error-checking omitted
2323
self.player.prepareToPlay()
2424
self.player.delegate = self
25-
if self.forever {
25+
if self.looping {
2626
self.player.numberOfLoops = -1
2727
}
2828

@@ -31,12 +31,6 @@ class Player : NSObject, AVAudioPlayerDelegate {
3131

3232
self.player.play()
3333

34-
// cute little demo
35-
let mpic = MPNowPlayingInfoCenter.default()
36-
mpic.nowPlayingInfo = [
37-
MPMediaItemPropertyTitle:"This Is a Test",
38-
MPMediaItemPropertyArtist:"Matt Neuburg"
39-
]
4034
}
4135

4236
// delegate method
@@ -46,7 +40,7 @@ class Player : NSObject, AVAudioPlayerDelegate {
4640
}
4741

4842
func stop () {
49-
self.player?.pause()
43+
self.player?.stop()
5044
}
5145

5246
deinit {

0 commit comments

Comments
 (0)