###実現したいこと
4つのボタンを用意し、それぞれのボタンを押した際に押されたボタンに応じて用意したそれぞれのmp3を流すプログラム
###発生している問題
ビルドまでできるもののアプリを開いた瞬間にクラッシュする。
###エラーメッセージ
0x10ee6563d <+3533>: callq 0x10ee646b0 ; ****.AudioPlayer4.unsafeMutableAddressor : Swift.ImplicitlyUnwrappedOptional<__ObjC.AVAudioPlayer> at ViewController.swift
デバッグをすると
AudioPlayer4 = try! AVAudioPlayer(contentsOf: fileURL4)でブレークされていると出ている。
###該当のソースコード
import UIKit
import AVFoundation
//変数宣言
var AudioPlayer1 : AVAudioPlayer!
var AudioPlayer2 : AVAudioPlayer!
var AudioPlayer3 : AVAudioPlayer!
var AudioPlayer4 : AVAudioPlayer!
var button1 = UIButton()
var button2 = UIButton()
var button3 = UIButton()
var button4 = UIButton()
class ViewController: UIViewController,AVAudioPlayerDelegate {
override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. let soundFilePath1 : String = Bundle.main.path(forResource: "01", ofType: "mp3")! let soundFilePath2 : String = Bundle.main.path(forResource: "02", ofType: "mp3")! let soundFilePath3 : String = Bundle.main.path(forResource: "03", ofType: "mp3")! let soundFilePath4 : String = Bundle.main.path(forResource: "04", ofType: "mp3")! let fileURL1 = URL(fileURLWithPath: soundFilePath1) let fileURL2 = URL(fileURLWithPath: soundFilePath2) let fileURL3 = URL(fileURLWithPath: soundFilePath3) let fileURL4 = URL(fileURLWithPath: soundFilePath4) AudioPlayer1 = try! AVAudioPlayer(contentsOf: fileURL1) AudioPlayer2 = try! AVAudioPlayer(contentsOf: fileURL2) AudioPlayer3 = try! AVAudioPlayer(contentsOf: fileURL3) AudioPlayer4 = try! AVAudioPlayer(contentsOf: fileURL4) AudioPlayer1.delegate = self AudioPlayer2.delegate = self AudioPlayer3.delegate = self AudioPlayer4.delegate = self //テキスト button1.setTitle("01",for:.normal) button2.setTitle("02",for:.normal) button3.setTitle("03",for:.normal) button4.setTitle("04",for:.normal) // ボタンのテキスト色を指定 button1.setTitleColor(UIColor.white, for: .normal) button2.setTitleColor(UIColor.white, for: .normal) button3.setTitleColor(UIColor.white, for: .normal) button4.setTitleColor(UIColor.white, for: .normal) button1.setTitleColor(UIColor.black, for: .highlighted) button2.setTitleColor(UIColor.black, for: .highlighted) button3.setTitleColor(UIColor.black, for: .highlighted) button4.setTitleColor(UIColor.black, for: .highlighted) // ボタン大きさ button1.frame.size = CGSize(width: 100, height: 100) button2.frame.size = CGSize(width: 100, height: 100) button3.frame.size = CGSize(width: 100, height: 100) button4.frame.size = CGSize(width: 100, height: 100) //ボタン場所 button1.layer.position = CGPoint(x: self.view.frame.width/2,y:100) button2.layer.position = CGPoint(x: self.view.frame.width/2,y:210) button3.layer.position = CGPoint(x: self.view.frame.width/2,y:320) button4.layer.position = CGPoint(x: self.view.frame.width/2,y:430) //ボタン色 button1.backgroundColor = UIColor.red button2.backgroundColor = UIColor.red button3.backgroundColor = UIColor.red button4.backgroundColor = UIColor.red //ボタンタグ設定 button1.tag = 1 button2.tag = 2 button3.tag = 3 button4.tag = 4 //角丸 button1.layer.cornerRadius = 10 button2.layer.cornerRadius = 10 button3.layer.cornerRadius = 10 button4.layer.cornerRadius = 10 //ボタン表示 self.view.addSubview(button1) self.view.addSubview(button2) self.view.addSubview(button3) self.view.addSubview(button4) button1.addTarget(self, action: #selector(onClickbutton1), for: UIControlEvents.touchUpInside) button2.addTarget(self, action: #selector(onClickbutton2), for: UIControlEvents.touchUpInside) button3.addTarget(self, action: #selector(onClickbutton3), for: UIControlEvents.touchUpInside) button4.addTarget(self, action: #selector(onClickbutton4), for: UIControlEvents.touchUpInside) button1.layer.masksToBounds = true button2.layer.masksToBounds = true button3.layer.masksToBounds = true button4.layer.masksToBounds = true } //押された func onClickbutton1(sender: UIButton){ if AudioPlayer1.isPlaying == true{ AudioPlayer1.stop() AudioPlayer1.currentTime = 0 }else{ AudioPlayer1.play() } } func onClickbutton2(sender: UIButton){ if AudioPlayer2.isPlaying == true{ AudioPlayer2.stop() AudioPlayer2.currentTime = 0 }else{ AudioPlayer2.play() } } func onClickbutton3(sender: UIButton){ if AudioPlayer3.isPlaying == true{ AudioPlayer3.stop() AudioPlayer3.currentTime = 0 }else{ AudioPlayer3.play() } } func onClickbutton4(sender: UIButton){ if AudioPlayer4.isPlaying == true{ AudioPlayer4.stop() AudioPlayer4.currentTime = 0 }else{ AudioPlayer4.play() } } //成功 func audioPlayerDidFinishPlaying(AudioPlayer1: AVAudioPlayer, successfully flag: Bool) { if !flag { return } print("Music Finish") } func audioPlayerDidFinishPlaying(AudioPlayer2: AVAudioPlayer, successfully flag: Bool) { if !flag { return } print("Music Finish") } func audioPlayerDidFinishPlaying(AudioPlayer3: AVAudioPlayer, successfully flag: Bool) { if !flag { return } print("Music Finish") } func audioPlayerDidFinishPlaying(AudioPlayer4: AVAudioPlayer, successfully flag: Bool){ if !flag { return } print("Music Finish") } //エラー func audioPlayerDecodeErrorDidOccur(AudioPlayer1: AVAudioPlayer, error: Error?) { if let e = error { print("Error") print(e.localizedDescription) return } } func audioPlayerDecodeErrorDidOccur(AudioPlayer2: AVAudioPlayer, error: Error?) { if let e = error { print("Error") print(e.localizedDescription) return } } func audioPlayerDecodeErrorDidOccur(AudioPlayer3: AVAudioPlayer, error: Error?) { if let e = error { print("Error") print(e.localizedDescription) return } }
func audioPlayerDecodeErrorDidOccur(AudioPlayer4: AVAudioPlayer, error: Error?) {
if let e = error {
print("Error")
print(e.localizedDescription)
return
}
}
}
###試したこと
3つだけにして試したところ問題なく動作
最初はバグが表示されていたのでそのバグの詳細や原因などを探したりしていた。
###補足情報(言語/FW/ツール等のバージョンなど)
言語『Swift3』
XCodeVersion 8.2.1 (8C1002)
参考サイト
h ttp://docs.fabo.io/swift/avfoundation/001_avfoundation.html
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2017/03/20 04:07
2017/03/20 08:24
退会済みユーザー
2017/03/20 12:32