前提・実現したいこと
録音したファイルを削除できるようにしたいです。
該当のソースコード
ViewContrrler
1// 2// ViewController.swift 3// recordAPP 2 4// 5// Created by user on 2020/09/20. 6// 7 8import UIKit 9import AVFoundation 10 11class ViewController: UIViewController, AVAudioRecorderDelegate, UITableViewDelegate, UITableViewDataSource { 12 13 var recordingSession:AVAudioSession! 14 var audioRecorder:AVAudioRecorder! 15 //録音したものを聴けるようにする。 16 var audioPlayer:AVAudioPlayer! 17 var numberOfRecords:Int = 0 18 19 20 @IBOutlet weak var buttonLabel: UIButton! 21 @IBOutlet weak var myTableView: UITableView! 22 23 // fileprivate func record(at offsets: UITableView) { myTableView.delete(self)} 24 25 @IBAction func record(_ sender: Any) { 26 if audioRecorder == nil { 27 numberOfRecords += 1 28 let filename = getDirectory().appendingPathComponent("(numberOfRecords).m4a") 29 //AVsampleReteKeyは、サンプル 30 let settings = [AVFormatIDKey: Int(kAudioFormatMPEG4AAC), AVSampleRateKey: 12000, AVNumberOfChannelsKey: 1, AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue] 31 32 //start audio recording 33 do { 34 audioRecorder = try AVAudioRecorder(url: filename, settings: settings) 35 audioRecorder.delegate = self 36 audioRecorder.record() 37 buttonLabel.setTitle("□", for: .normal) 38 } catch { 39 displayAlert(title: "Ups!", message: "Recording failed") 40 }} else { 41 //stop audio recording 42 audioRecorder.stop() 43 audioRecorder = nil 44 UserDefaults.standard.set(numberOfRecords, forKey: "myNumber") 45 myTableView.reloadData() 46 buttonLabel.setTitle("○", for: .normal) 47 } 48 } 49 50 51 52 override func viewDidLoad() { 53 super.viewDidLoad() 54 // Do any additional setup after loading the view. 55 //Setting up session 56 myTableView.delegate = self 57 myTableView.dataSource = self 58 59 60 61 recordingSession = AVAudioSession.sharedInstance() 62 if let number:Int = UserDefaults.standard.object(forKey: "myNumber") as? Int 63 { 64 numberOfRecords = number 65 } 66 AVAudioSession.sharedInstance().requestRecordPermission { (hasPermisson) in 67 if hasPermisson { 68 print("ACCEPTED") 69 } 70 } 71} 72 73 74 75 76 77 //Functuon that gets path to directry 78 func getDirectory() -> URL { 79 let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask) 80 let documentDirectory = paths[0] 81 return documentDirectory 82 } 83 //Functuon that displays an alert 84 func displayAlert(title:String, message:String) { 85 let alert = UIAlertController(title: title, message: message, preferredStyle: .alert) 86 alert.addAction(UIAlertAction(title: "dismiss", style: .default, handler: nil)) 87 present(alert, animated: true, completion: nil) 88 } 89 90 //Setting up table view 91func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 92 return numberOfRecords } 93 94 95func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 96 let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) 97 cell.textLabel?.text = String(indexPath.row + 1) 98 return cell } 99 100func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle { 101 return .delete } 102 103func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath:IndexPath) { 104 if editingStyle == .delete { 105 tableView.beginUpdates() 106 107 108 ** _____.remove(at: indexPath.row) ** 109 110 tableView.deleteRows(at: [indexPath], with: .fade) 111 tableView.endUpdates() 112 } 113 } 114 115//録音したものを聴けるようにする。 116func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 117 let path = getDirectory().appendingPathComponent("(indexPath.row + 1).m4a") 118 do{ 119 audioPlayer = try AVAudioPlayer(contentsOf: path) 120 audioPlayer.play() 121 } catch { 122 123 } 124 } 125 } 126 127 128
試したこと
_____.remove(at: indexPath.row) この文字がスクロールで削除できると思うのですが、ファイルがどの文字化がわかっていません。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/09/21 04:34