The Stop and Play buttons are initially disabled: there’s nothing to stop or play yet. See the first screenshot.
For class
AVAudioRecorder
and
protocol
AVAudioRecorderDelegate
,
see
this
document.
For class
AVAudioPlayer
and protocol
AVAudioPlayerDelegate
,
see
Switch.
The filename suffix
.caf
stands for “Core Audio Format”.
On iPhone 6,
the size of the
UISegmentedControl
defaults to
AppDelegate
ViewController
acts as the delegate for the
audio
recorder
and the
audio
player,
and as the target for the
UISegmentedControl
.
It also sets up the audio session.
View
contains two subviews: the
UISegmentedControl
and a litte square
UIView
whose
backgroundColor
changes from
white
to
red.
The
view
controller
imports the
AVFoundation
framework.
The
view
controller
also adopts two
protocols:
AVAudioRecorderDelegate
and
AVAudioPlayerDelegate
.
I recorded an audio file and listened to it twice:
inputNumberOfChannels = 2 ouputNumberOfChannels = 2 sampleRate = 44100.0 preferredSampleRate = 0.0 preferredIOBufferDuration = 0.0 path = /Users/myname/Library/Developer/CoreSimulator/Devices/01EBED35-F21B-4B4C-95BC-BFDDDC6264B4/data/Containers/Data/Application/1D186D68-B8D3-422F-9E0C-80CAB93FDE10/tmp/recording.caf url = file:///Users/myname/Library/Developer/CoreSimulator/Devices/01EBED35-F21B-4B4C-95BC-BFDDDC6264B4/data/Containers/Data/Application/1D186D68-B8D3-422F-9E0C-80CAB93FDE10/tmp/recording.caf audioRecorderDidFinishRecording:successfully: true audioPlayerDidFinishPlaying:successfully: true audioPlayerDidFinishPlaying:successfully: true
String
s
with an
array
of
UIImage
s.
We saw class
UIImage
in
America.
As in that app,
drag the files
usa.png
,canada.png
,mexico.png
Images.xcassets
file.
Change the
segmentedControl
property
of class
View
from
let segmentedControl: UISegmentedControl = UISegmentedControl(items: [ "Record", "Stop", "Play" ]);to the following. See Template Images.
let segmentedControl: UISegmentedControl = UISegmentedControl(items: [ UIImage(named: "usa.png")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal), UIImage(named: "canada.png")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal), UIImage(named: "mexico.png")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal), ]);Each
.png
UISegmentedControl
by assigning to its
bounds
property.
audioRecorderDidFinishRecording
.
if flag { //means if flag == true let fileManager: NSFileManager = NSFileManager.defaultManager(); var error: NSError?; let attributes: [NSObject: AnyObject]? = fileManager.attributesOfItemAtPath(url!.path!, error: &error); if attributes != nil { for (key, value) in attributes! { print("\(key): \(value)"); } } }
Decimal 16,777,218 is hexadecimal 1000002.
Decimal 420 is octal 644,
which represents the nine permission bits
rw-r--r--
.
Anyone can read this file,
but only the owner has permission to write it.
NSFileOwnerAccountID: 502 NSFileSystemFileNumber: 32395001 NSFileExtensionHidden: 0 NSFileSystemNumber: 16777218 NSFileSize: 130372 NSFileGroupOwnerAccountID: 20 NSFilePosixPermissions: 420 NSFileCreationDate: 2014-11-23 21:50:53 +0000 NSFileType: NSFileTypeRegular NSFileGroupOwnerAccountName: staff NSFileReferenceCount: 1 NSFileModificationDate: 2014-11-24 15:24:46 +0000
Button.swift
,
so that they can be used in both
View.swift
and
ViewController.swift
.
Select the
Segmented
folder in the Xcode Project Navigator.
Drag the new file
Button.swift
up to the other
.swift
files in the Project Navigator.
Append the following to
Button.swift
.
enum Button: Int { case Record = 0; case Stop; case Play; };
Change every
0
used as the second argument of
setEnabled(_forSegmentAtIndex:)
or as the number after a
case
to
Button.Record.rawValue
.
Change every
1
to
Button.Stop.rawValue
.
Change every
2
to
Button.Play.rawValue
.