Q&A
あるサイトを参考に、以下のようなコードを書きました。エラーなど起こらず、Simulatorは起動でき、画像ファイルやテキストなどは反映されていましたが、音楽ファイル(BGM)が鳴りません。
アプリを起動すると、即座にBGMとして音楽が鳴るようにしているつもりです。
間違いを探そうにも、エラーが起きていないため、対処方法がわからずに困っています。
誤っている箇所・修正箇所などありましたら、ご教授お願いいたします。
※音楽ファイル名は "chino.mp3"です。
import
1import AVFoundation 2 3class ViewController: UIViewController { 4 5var audioPlayerInstance : AVAudioPlayer! = nil 6 7override func viewDidLoad() { 8 super.viewDidLoad() 9 // Do any additional setup after loading the view, typically from a nib. 10 let soundFilePath = Bundle.main.path(forResource: "chino", ofType: "mp3")! 11 let sound:URL = URL(fileURLWithPath: soundFilePath) 12 do { 13 audioPlayerInstance = try AVAudioPlayer(contentsOf: sound, fileTypeHint: nil) 14 } catch { 15 print("失敗") 16 } 17 audioPlayerInstance.prepareToPlay() 18} 19 20override func didReceiveMemoryWarning() { 21 super.didReceiveMemoryWarning() 22 // Dispose of any resources that can be recreated.
回答1件
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
2017/08/24 11:53