Skip to Content
Swift游戏开发经典实例
book

Swift游戏开发经典实例

by Jonathon Manning, Paris Buttfield-Addison
January 2020
Intermediate to advanced
356 pages
6h 4m
Chinese
China Electric Power Press Ltd.
Content preview from Swift游戏开发经典实例
声音
131
讨论
AVAudioRecorder
对象就好比
AVAudioPlayer
对象一样,也需要保持一个强引用以
防它被意外释放。
要进行录音,首先要提供一个文件地址,用于存放录音文件。如果文件不存在,
AVAudioRecorder
会自动创建这个文件,如果文件存在,
AVAudioRecorder
将会擦
除文件中的内容并覆写它。因此,如果你要避免录音数据被意外擦除,不要用同一
个地址录两次音,或者在录音完成后将录音文件移到别的地方。
录音后声音文件应该存放到游戏的安全沙盒中。一个比较好的选择是放到游戏的
Documents
目录中;在这个文件中存放的文件在设备同步时会被备份。
通过
FileManager
类来获得游戏的
Documents
目录:
let documentsURL = FileManager.default
.urls(for: FileManager.SearchPathDirectory.documentDirectory,
in:FileManager.SearchPathDomainMask.userDomainMask).last!
一旦你准备好文件路径,你就可以创建它的
URL
。注意,
URL
所指向的文件不一
定存在,如果这样,在录音开始后会真正创建这个文件:
return documentsURL.appendingPathComponent("RecordedSound.wav")
4.3
使用多个
AVAudioPlayer
问题
虽然我们可以同时使用多个
AVAudioPlayer ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

精通機器學習

精通機器學習

Aurélien Géron
FreeBSD® Unleashed

FreeBSD® Unleashed

Brian Tiemann, Michael C. Urban
深入淺出代數

深入淺出代數

Tracey Pilone, Dan Pilone
Swift学习手册

Swift学习手册

Jon Manning, Paris Buttfield-Addison, Tim Nugent

Publisher Resources

ISBN: 9787519840129