質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Q&A

0回答

652閲覧

swiftUI 録音したものをテキストに表示したい。

isekiryu

総合スコア15

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

0グッド

0クリップ

投稿2020/10/04 06:27

前提・実現したいこと

録音できるアプリの作成をしている際に、いろいろなサイトを参考にして作成してみました。
録音・録音停止・再生・停止はできるようになりました。
録音できて、再生できるという事は、録音のファイルができていると私は考えています。
それをテキストのに表示したいです。
音声名:_____のこの場所に入れたいです。

イメージ説明

該当のソースコード

AudioRecorder.swift

1 2// 3// AudioRecorder.swift 4// voiceAPP 5// 6// Created by user on 2020/10/02. 7// Copyright © 2020 user. All rights reserved. 8// 9 10import AVFoundation 11import SwiftUI 12 13class AudioRecorder { 14 15 private var audioRecorder: AVAudioRecorder! 16 internal var audioPlayer: AVAudioPlayer! 17 var audios : [URL] = [] 18 19 //レコードの中身。 20 21 internal func record() { 22 let session = AVAudioSession.sharedInstance() 23 try! session.setCategory(AVAudioSession.Category.playAndRecord) 24 try! session.setActive(true) 25 26 _ = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] 27 28 29 let settings = [ 30 AVFormatIDKey: Int(kAudioFormatMPEG4AAC), 31 AVSampleRateKey: 44100, 32 AVNumberOfChannelsKey: 2, 33 AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue 34 ] 35 36 audioRecorder = try! AVAudioRecorder(url: getURL(), settings: settings) 37 self.audioRecorder = try! AVAudioRecorder(url: getURL(), settings: settings) 38 audioRecorder.record() 39 } 40 41 42 internal func recordStop() -> Data?{ 43 audioRecorder.stop() 44 let data = try? Data(contentsOf: getURL()) 45 return data 46 } 47 48 internal func play() { 49 audioPlayer = try! AVAudioPlayer(contentsOf: getURL()) 50 audioPlayer.volume = 10.0 51 audioPlayer.play() 52 } 53 54 internal func playStop() { 55 audioPlayer.stop() 56 } 57 58 59 60 private func getURL() -> URL{ 61 62 do{ 63 64 let url = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] 65 let result = try! FileManager.default.contentsOfDirectory(at: url, includingPropertiesForKeys: nil, options: .producesRelativePathURLs) 66 67 self.audios.removeAll() 68 69 for i in result { 70 self.audios.append(i) 71 } 72 return FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0].appendingPathComponent("record.m4a") 73 } 74 75 76} 77 78 79 func getURL() { 80 do { 81 let url = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] 82 let result = try! FileManager.default.contentsOfDirectory(at: url, includingPropertiesForKeys: nil, options: .producesRelativePathURLs) 83 for i in result { 84 self.audios.append(i) 85 } 86 87 } 88 89 } 90 91 92 93 94} 95 96

該当のソースコード

AudioRecorder.swift

1 2 3import SwiftUI 4import Speech 5import MediaPlayer 6import AVKit 7import AVFoundation 8 9 10struct recordNew: View { 11 let audioRecorder: AudioRecorder = AudioRecorder() 12 @State var isRecording = false 13 @State private var selection: Int? = nil 14 15 @State var recognizedText: String? 16 17 @State var audios : [URL] = [] 18 19 @State var button = true 20 @State var record = false 21 @State var session : AVAudioSession! 22 @State var recorder : AVAudioRecorder! 23 internal var audioPlayer: AVAudioPlayer! 24 25 26 27 //スライダーの設定 28 @State private var currentValue: Double = 50 29 @State private var textActive = false 30 @State private var volume = 0.0 31 @State private var seekPosition : Double = 0.3 32 33 34 var body: some View { 35 36 // List(selection: $selection){ 37 38 // ForEach(self.audios, id, .self) {i in Text(i.relativeString) } 39 40 41 HStack(spacing: 30) { 42 43 44 Button(action: { 45 print("タップさた") 46 }) { 47 Image(systemName: "ellipsis") // システムアイコンを指定 48 Text("") 49 50 } 51 .foregroundColor(.blue) 52 .font(.headline) 53 .padding(.bottom, 40) 54 .padding(.top, 90) 55 56 57 Button(action: { 58 print("タップされま") 59 }) { 60 Image(systemName: "gobackward.15") // システムアイコンを指定 61 Text("") 62 } 63 .font(.title) 64 .foregroundColor(.blue) 65 .padding(.bottom, 40) 66 .padding(.top, 90) 67 68 69 70 71 Button(action: { 72 self.audioRecorder.play() 73 }) { 74 Image(systemName: "play.fill") 75 } 76 .font(.largeTitle) 77 .foregroundColor(.blue) 78 .padding(.bottom, 40) 79 .padding(.top, 90) 80 81 82 83 84 Button(action: { 85 self.audioRecorder.playStop() 86 }) { 87 Image(systemName: "stop") 88 } 89 .font(.largeTitle) 90 .foregroundColor(.blue) 91 .padding(.bottom, 40) 92 .padding(.top, 90) 93 94 95 96 97 98 99 //15秒進める。 100 Button(action: { 101 print("タッした") 102 }) { 103 Image(systemName: "goforward.15") // システムアイコンを指定 104 Text("") 105 } 106 .font(.title) 107 .foregroundColor(.blue) 108 .padding(.bottom, 40) 109 .padding(.top, 90) 110 111 112 113 //捨てる動作 114 Button(action: { 115 print("ました") 116 }) { 117 Image(systemName: "trash") // システムアイコンを指定 118 Text("") 119 } 120 .foregroundColor(.blue) 121 .font(.headline) 122 .padding(.bottom, 40) 123 .padding(.top, 90) 124 125 126 127 128 } 129 130 131 /* ForEach(self.audio ,id: .self){i in 132 VStack { 133 Text(i.relativeString) 134 135 }} 136 137 138 139 Text(" 音声名: record.m4a ") 140 .padding(.top, 20) 141 .padding(. bottom, 40) 142 .foregroundColor(.black) 143 .font(.title) 144 145 146 147 148 149 150 HStack { 151 Text("  ") 152 Slider(value: $seekPosition, in: 0...1) 153 Text("  ") 154 155 HStack { 156 // Text(formatTime(sec: Int(nowPlaying.duration * seekPosition))) 157 Spacer() 158 // Text("-" + formatTime(sec: Int(nowPlaying.duration * (1 - seekPosition)))) 159 160 } 161 } 162 // .padding(.bottom, 40) 163 164 165 166 167 168 169 HStack { 170 Text(" MIN") 171 .foregroundColor(.black) 172 Slider(value: $volume, in:0.0...100.0, onEditingChanged: { _ in 173 174 }) 175 Text("MAX ") 176 .foregroundColor(.black) 177 } 178 .padding(.top, 40) 179 HStack { 180 Spacer() 181 Text("音量:(Int(self.volume))") 182 .foregroundColor(.black) 183 Spacer() 184 } 185 186 187 188 HStack (spacing: 145){ 189 190 191 192 Button(action: { 193 self.audioRecorder.record() 194 }) { 195 Image(systemName: "waveform.circle") 196 } 197 .foregroundColor(.blue) 198 .padding(.bottom, 60) 199 .padding(.top, 20) 200 .font(.largeTitle) 201 202 203 Button(action: { 204 _ = self.audioRecorder.recordStop() 205 }) { 206 Image(systemName: "record.circle") 207 208 } 209 .foregroundColor(.blue) 210 .padding(.bottom, 60) 211 .padding(.top, 20) 212 .font(.largeTitle) 213 214 } 215 216 } 217 218 219 220 221 222 } 223 224 225 226struct recordNew_Previews: PreviewProvider { 227 static var previews: some View { 228 ContentView() 229 } 230} 231 232 233 234 235 236### 試したこと 237 238 239 240### 補足情報(FW/ツールのバージョンなど) 241 242ここにより詳細な情報を記載してください。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問