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

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

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

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

Q&A

解決済

1回答

875閲覧

AVAudioPlayerで再生したいです

退会済みユーザー

退会済みユーザー

総合スコア0

Swift

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

1グッド

1クリップ

投稿2018/10/27 01:43

前提・実現したいこと

テーブルをタッチすると音楽が再生されるようにしたいです

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

なし

該当のソースコード

swift

1 2import UIKit 3import AVFoundation 4 5var songs:[String] = [] 6var audioPlayer = AVAudioPlayer() 7 8 9class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 10 11 @IBOutlet weak var musicTableView: UITableView! 12 13 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 14 return songs.count 15 } 16 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 17 let musicCell = UITableViewCell(style: .default, reuseIdentifier: "MusicListCell") 18 musicCell.textLabel?.text = songs[indexPath.row] 19 return musicCell 20 } 21 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 22 do { 23 print("aaaaaaaaaaa") 24 let audioPath = Bundle.main.path(forResource: "music/(songs[indexPath.row])", ofType: "mp3") 25 print(audioPath) 26 try audioPlayer = AVAudioPlayer(contentsOf: NSURL(fileURLWithPath: audioPath!) as URL ) 27 28 } catch { 29 print("error") 30 } 31 } 32 33 override func viewDidLoad() { 34 super.viewDidLoad() 35 // Do any additional setup after loading the view, typically from a nib. 36 37 let songURL1 = URL(fileURLWithPath: Bundle.main.path(forResource: "music/Abolisher", ofType: "mp3")!) 38 let songURL2 = URL(fileURLWithPath: Bundle.main.path(forResource: "music/EndlessDemise", ofType: "mp3")!) 39 let songURL3 = URL(fileURLWithPath: Bundle.main.path(forResource: "music/SantisimaMuerte", ofType: "mp3")!) 40 let songURL4 = URL(fileURLWithPath: Bundle.main.path(forResource: "music/SixFeetUnder", ofType: "mp3")!) 41 let songURL5 = URL(fileURLWithPath: Bundle.main.path(forResource: "music/Unanswered", ofType: "mp3")!) 42 43 let songsURL = [ songURL1, songURL2, songURL3, songURL4, songURL5 ] 44 //print("あああああああああああああ") 45 for song in songsURL { 46 var mysong = song.absoluteString 47 let findString = mysong.components(separatedBy: "/") 48 mysong = findString[findString.count-1] 49 mysong = mysong.replacingOccurrences(of: ".mp3", with: "") 50 songs.append(mysong) 51 } 52 musicTableView.reloadData() 53 } 54} 55

試したこと

タップした曲名のpathを表示させて見たら正しく表示されました。

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

swift4.2
xcode10.0

DrqYuto👍を押しています

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

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

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

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

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

guest

回答1

0

ベストアンサー

.play()メソッド呼んでなく無いですか?

Swift

1func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 2 audioPlayer.play() 3}

投稿2018/10/27 05:47

hodoru3sei

総合スコア284

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

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

退会済みユーザー

退会済みユーザー

2018/10/27 13:15

その通りでした ありがとうございます
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問