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

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

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

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

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

1回答

547閲覧

UIbuttonのサウンドについて

iuys

総合スコア0

Xcode

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

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2020/10/06 07:26

編集2020/10/06 07:53

前提・実現したいこと

録画撮影アプリの撮影開始兼停止UIbuttonに撮影開始のサウンド、停止のサウンドをつけたいです。
インターネット等でUIbuttonのサウンドについて調べたのですが、storyboard上にUIbuttronを配置されている方の記事しか見つかりませんでした。当方コードによってボタンを配置していて、その方法に該当する記事を見つけることができませんでした。

発生している問題・エラーメッセージ

撮影自体は問題なく行えています。

該当のソースコード

Swift

1import UIKit 2import AVFoundation 3import Photos 4 5class ViewController: UIViewController, AVCaptureFileOutputRecordingDelegate { 6 let fileOutput = AVCaptureMovieFileOutput() 7 8 var recordButton: UIButton! 9 var isRecording = false 10 11 override func viewDidLoad() { 12 super.viewDidLoad() 13 14 setUpPreview() 15 } 16 17 func setUpPreview() { 18 let videoDevice = AVCaptureDevice.default(for: AVMediaType.video) 19 let audioDevice = AVCaptureDevice.default(for: AVMediaType.audio) 20 21 do { 22 if videoDevice == nil || audioDevice == nil { 23 throw NSError(domain: "device error", code: -1, userInfo: nil) 24 } 25 let captureSession = AVCaptureSession() 26 27 // video inputを capture sessionに追加 28 let videoInput = try AVCaptureDeviceInput(device: videoDevice!) 29 captureSession.addInput(videoInput) 30 31 // audio inputを capture sessionに追加 32 let audioInput = try AVCaptureDeviceInput(device: audioDevice!) 33 captureSession.addInput(audioInput) 34 35 // max 30sec 36 self.fileOutput.maxRecordedDuration = CMTimeMake(value: 30, timescale: 1) 37 captureSession.addOutput(fileOutput) 38 39 // プレビュー 40 let videoLayer : AVCaptureVideoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession) 41 videoLayer.frame = self.view.bounds 42 videoLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill 43 self.view.layer.addSublayer(videoLayer) 44 45 captureSession.startRunning() 46 47 setUpButton() 48 } catch { 49 // エラー処理 50 } 51 } 52 53 func setUpButton() { 54 recordButton = UIButton(frame: CGRect(x: 0,y: 0,width: 120,height: 50)) 55 recordButton.backgroundColor = UIColor.gray 56 recordButton.layer.masksToBounds = true 57 recordButton.setTitle("録画開始", for: .normal) 58 recordButton.layer.cornerRadius = 20.0 59 recordButton.layer.position = CGPoint(x: self.view.bounds.width/2, y:self.view.bounds.height-50) 60 recordButton.addTarget(self, action: #selector(ViewController.onClickRecordButton(sender:)), for: .touchUpInside) 61 62 self.view.addSubview(recordButton) 63 } 64 65 @objc func onClickRecordButton(sender: UIButton) { 66 if !isRecording { 67 // 録画開始 68 let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) 69 let documentsDirectory = paths[0] as String 70 let filePath : String? = "(documentsDirectory)/temp.mp4" 71 let fileURL : NSURL = NSURL(fileURLWithPath: filePath!) 72 fileOutput.startRecording(to: fileURL as URL, recordingDelegate: self) 73 74 isRecording = true 75 changeButtonColor(target: recordButton, color: UIColor.red) 76 recordButton.setTitle("録画中", for: .normal) 77 } else { 78 // 録画終了 79 fileOutput.stopRecording() 80 81 isRecording = false 82 changeButtonColor(target: recordButton, color: UIColor.gray) 83 recordButton.setTitle("録画開始", for: .normal) 84 } 85 } 86 87 func changeButtonColor(target: UIButton, color: UIColor) { 88 target.backgroundColor = color 89 } 90 91 func fileOutput(_ output: AVCaptureFileOutput, didFinishRecordingTo outputFileURL: URL, from connections: [AVCaptureConnection], error: Error?) { 92 // ライブラリへ保存 93 PHPhotoLibrary.shared().performChanges({ 94 PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: outputFileURL) 95 }) { completed, error in 96 if completed { 97 print("Video is saved!") 98 } 99 } 100 } 101} 102

試したこと

特に試せてないです。申し訳ないです。

補足情報(FW/ツールのバージョンなど)

swiftr5 Xcode12.01

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

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

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

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

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

guest

回答1

0

System Sound を鳴らすのが一番手っ取り早いかと思います。

Swift

1import UIKit 2// MARK: - AudioToolbox を import する 3import AudioToolbox 4 5class ViewController: UIViewController { 6 // 中略 7 8 @objc func onClickRecordButton(sender: UIButton) { 9 if !isRecording { 10 // 録画開始 11 // 中略 12 13 recordButton.setTitle("録画中", for: .normal) 14 // ID については下記を参照 15 // http://iphonedevwiki.net/index.php/AudioServices 16 let beginRecording: SystemSoundID = 1113 17 AudioServicesPlaySystemSound(beginRecording) 18 } else { 19 // 録画終了 20 // 中略 21 22 recordButton.setTitle("録画開始", for: .normal) 23 24 // 25 let endVideoRecording: SystemSoundID = 1118 26 AudioServicesPlaySystemSound(endVideoRecording) 27 } 28 } 29 // 後略 30}

登録されている音を鳴らすだけであれば、上記のメソッドと、予め準備されている ID を使えば、他に処理は必要ありません。

登録済みの ID については、下記の資料が一番充実しているようです。

  • [http://iphonedevwiki.net/index.php/AudioServices

](http://iphonedevwiki.net/index.php/AudioServices)

投稿2020/10/06 11:11

TsukubaDepot

総合スコア5086

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

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

Rosst

2020/10/20 03:20

アカウントが何故か入れなくなってしまい、返信が遅れてしまったのですが、解決しました!ありがとうございます
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

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

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

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問