---
title: william-weng/wwaudiorecorderhelper
framework: Swift Package Catalog
role: article
path: packages/william-weng/wwaudiorecorderhelper
---

# william-weng/wwaudiorecorderhelper

[English](./README.en.md) | [正體中文](./README.md)

## ✨ [功能特色](https://peterpanswift.github.io/iphone-bezels/)

- 使用 `AVAudioRecorder` 從麥克風錄音。 - 支援自訂錄音檔案儲存路徑。 - 預設將錄音檔寫入 `URL.temporaryDirectory`。 - 透過 Closure 回傳錄音完成結果。 - 透過 Closure 回傳錄音編碼錯誤。 - 可設定 `AVAudioSession` 的 category、mode、options 與 active 狀態。 - 支援 Swift Package Manager。

## 📦 安裝方式

### Swift Package Manager

```bash swift package add https://github.com/William-Weng/WWAudioRecorderHelper.git ```

或在 Xcode 中：

``` File → Add Packages → https://github.com/William-Weng/WWAudioRecorderHelper.git ```

## 🎙️ 麥克風權限

請在 App 的 `Info.plist` 加入麥克風使用說明：

```xml <key>NSMicrophoneUsageDescription</key> <string>此應用程式需要使用麥克風進行錄音</string> ```

## 📌 公開屬性

| 屬性 | 說明 | | --- | --- | | `basePath` | 設定錄音檔的預設儲存目錄。 |

## 🏗️ 公開 API

| API | 說明 | | --- | --- | | `init()` | 建立錄音工具實例。 | | `configureSession(_:isActive:mode:options:)` | 設定並啟用或停用 Audio Session。 | | `start(filename:onFinish:onFailure:)` | 建立錄音器並開始錄音。 | | `stop()` | 停止目前的錄音並釋放錄音器。 |

🧪 完整範例

```swift import SwiftUI import WWAudioRecorderHelper

struct ContentView: View {          @State private var isRecording = false     @State private var helper = WWAudioRecorderHelper()          var body: some View {                  Button(action: toggleRecording) {             HStack(spacing: 8) {                 Image(systemName: isRecording ? "stop.fill" : "circle.fill")                     .font(.title3)                 Text(isRecording ? "Stop" : "Record")                     .font(.headline)             }             .foregroundStyle(.white)             .padding(.horizontal, 8)             .padding(.vertical, 8)             .frame(minWidth: 128)             .background(isRecording ? .red : .blue)             .clipShape(                 RoundedRectangle(cornerRadius: 12)             )         }         .buttonStyle(.plain)         .padding()         .onAppear {             configureRecorder()         }         .onDisappear {             stopRecordingIfNeeded()         }     } }

private extension ContentView {          func toggleRecording() {                  if isRecording {             stopRecording()         } else {             startRecording()         }     }          func configureRecorder() {                  helper.basePath = .documentsDirectory                  do {             try helper.configureSession(.record, isActive: true)         } catch {             print("Failed to configure audio session: \(error.localizedDescription)")         }     }          func startRecording() {                  helper.start(filename: "demo.wav",             onFinish: { url in                 print("Recording finished: \(url)")             },             onFailure: { error in                 print("Recording failed: \(error.localizedDescription)")                 isRecording = false             }         )                  isRecording = true     }          func stopRecording() {                  helper.stop()         isRecording = false                  do {             try helper.configureSession(.record, isActive: false)         } catch {             print("Failed to deactivate audio session: \(error.localizedDescription)")         }     }          func stopRecordingIfNeeded() {         guard isRecording else { return }         stopRecording()     } }

#Preview {     ContentView() } ```

## Package Metadata

Repository: william-weng/wwaudiorecorderhelper

Default branch: main

README: README.md
