この記事では、AVFoundationを使って、音声を録音して再生する方法を紹介します。
実装方法
STEP.2
ボタンの紐付け

IBOutletで、recordButton、playButtonという名前で紐付け
IBActionで、recordButtonAction、playButtonActionという名前で紐付け
STEP.3
コードを使う準備
①importを追加します。
import AVFoundation
②Delegateします。
class ViewController: UIViewController, AVAudioRecorderDelegate, AVAudioPlayerDelegate {
③必要な変数を定義します。
var audioRecorder: AVAudioRecorder! var audioPlayer: AVAudioPlayer! var isRecording = false

STEP.4
録音したデータを保存
録音したデータを保存する処理を追記します。
private func getURL() -> URL {
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let docsDirect = paths[0]
let url = docsDirect.appendingPathComponent("sample.m4a")
return url
}

STEP.5
録音する処理
録音ボタンの@IBAction内に録音する処理を追記します。
if !isRecording {
let session = AVAudioSession.sharedInstance()
try! session.setCategory(.playAndRecord)
try! session.setActive(true)
let settings = [
AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
AVSampleRateKey: 44100,
AVNumberOfChannelsKey: 2,
AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue
]
audioRecorder = try! AVAudioRecorder(url: getURL(), settings: settings)
audioRecorder.delegate = self
audioRecorder.record()
recordButton.setTitle("停止", for: .normal)
playButton.isEnabled = false
} else {
audioRecorder.stop()
recordButton.setTitle("録音", for: .normal)
playButton.isEnabled = true
}
isRecording = !isRecording

STEP.6
再生する処理
再生ボタンの@IBAction内に再生する処理を追記します。
audioPlayer = try! AVAudioPlayer(contentsOf: getURL()) audioPlayer.delegate = self audioPlayer.play()

これで完成です。
実行して確認してみてください。
全てのコード
//
// ViewController.swift
// Record
//
// Created by RikutoSato on 2021/09/25.
//
import UIKit
import AVFoundation
class ViewController: UIViewController, AVAudioRecorderDelegate, AVAudioPlayerDelegate {
@IBOutlet weak var recordButton: UIButton!
@IBOutlet weak var playButton: UIButton!
var audioRecorder: AVAudioRecorder!
var audioPlayer: AVAudioPlayer!
var isRecording = false
override func viewDidLoad() {
super.viewDidLoad()
}
private func getURL() -> URL {
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let docsDirect = paths[0]
let url = docsDirect.appendingPathComponent("sample.m4a")
return url
}
@IBAction func recordButtonAction(_ sender: Any) {
if !isRecording {
let session = AVAudioSession.sharedInstance()
try! session.setCategory(.playAndRecord)
try! session.setActive(true)
let settings = [
AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
AVSampleRateKey: 44100,
AVNumberOfChannelsKey: 2,
AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue
]
audioRecorder = try! AVAudioRecorder(url: getURL(), settings: settings)
audioRecorder.delegate = self
audioRecorder.record()
recordButton.setTitle("停止", for: .normal)
playButton.isEnabled = false
} else {
audioRecorder.stop()
recordButton.setTitle("録音", for: .normal)
playButton.isEnabled = true
}
isRecording = !isRecording
}
@IBAction func playButtonAction(_ sender: Any) {
audioPlayer = try! AVAudioPlayer(contentsOf: getURL())
audioPlayer.delegate = self
audioPlayer.play()
}
}
参考文献


