帮我生成一个可以在iPhone中运行的swift中的storyboar录音和播放程序
时间: 2024-03-10 18:51:15 浏览: 209
IOS录音及播放
4星 · 用户满意度95%
好的,我可以为您提供一个简单的录音和播放程序。请按照以下步骤操作:
1. 创建一个新的 Xcode 项目,选择 Single View App 模板,语言选择 Swift。
2. 在 Main.storyboard 中添加两个按钮(录音和播放)和一个标签,用于显示录音和播放状态。
3. 在 ViewController.swift 文件中添加以下代码:
```swift
import UIKit
import AVFoundation
class ViewController: UIViewController, AVAudioRecorderDelegate, AVAudioPlayerDelegate {
@IBOutlet weak var statusLabel: UILabel!
var audioRecorder: AVAudioRecorder?
var audioPlayer: AVAudioPlayer?
override func viewDidLoad() {
super.viewDidLoad()
setupRecorder()
}
func setupRecorder() {
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setCategory(AVAudioSession.Category.playAndRecord)
try audioRecorder = AVAudioRecorder(url: getFileURL(), settings: getAudioSettings())
audioRecorder?.delegate = self
audioRecorder?.prepareToRecord()
} catch {
print("Error setting up audio recorder: \(error.localizedDescription)")
}
}
func getFileURL() -> URL {
let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let audioURL = documentsDirectory.appendingPathComponent("recording.caf")
return audioURL
}
func getAudioSettings() -> [String: Any] {
let settings = [
AVFormatIDKey: NSNumber(value: kAudioFormatAppleLossless),
AVEncoderAudioQualityKey: AVAudioQuality.max.rawValue,
AVEncoderBitRateKey: 320000,
AVNumberOfChannelsKey: 2,
AVSampleRateKey: 44100.0
]
return settings
}
@IBAction func startRecording(_ sender: UIButton) {
if audioRecorder?.isRecording == false {
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setActive(true)
audioRecorder?.record()
statusLabel.text = "Recording..."
} catch {
print("Error starting recording: \(error.localizedDescription)")
}
}
}
@IBAction func stopRecording(_ sender: UIButton) {
if audioRecorder?.isRecording == true {
audioRecorder?.stop()
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setActive(false)
statusLabel.text = "Recording stopped"
} catch {
print("Error stopping recording: \(error.localizedDescription)")
}
}
}
@IBAction func playRecording(_ sender: UIButton) {
if audioPlayer?.isPlaying == false {
do {
try audioPlayer = AVAudioPlayer(contentsOf: getFileURL())
audioPlayer?.delegate = self
audioPlayer?.play()
statusLabel.text = "Playing..."
} catch {
print("Error playing audio: \(error.localizedDescription)")
}
}
}
func audioRecorderDidFinishRecording(_ recorder: AVAudioRecorder, successfully flag: Bool) {
if flag {
statusLabel.text = "Recording saved"
} else {
statusLabel.text = "Recording failed"
}
}
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
if flag {
statusLabel.text = "Playback finished"
} else {
statusLabel.text = "Playback failed"
}
}
}
```
这段代码包括了以下内容:
- 创建 AVAudioRecorder 和 AVAudioPlayer 实例来处理录音和播放
- 设置录音的 URL 和参数
- 设置录音和播放的按钮动作
- 显示录音和播放状态的标签
- 实现 AVAudioRecorderDelegate 和 AVAudioPlayerDelegate 来处理录音和播放完成后的事件
4. 运行应用程序并在iPhone中测试录音和播放功能。
请注意,这只是一个简单的示例,您可以根据您的需求进行修改和扩展。同时,您需要确保在应用程序的 Info.plist 文件中添加了麦克风和音频播放权限,否则应用程序将无法访问麦克风或播放音频文件。
阅读全文