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

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

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

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

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

Swift

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

Q&A

解決済

1回答

2484閲覧

【Swift】ボタン4つを押した際にそれぞれ別の音を鳴らしたいです。

退会済みユーザー

退会済みユーザー

総合スコア0

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

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

Swift

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

0グッド

0クリップ

投稿2017/03/19 14:22

###実現したいこと
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

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

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

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

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

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

guest

回答1

0

ベストアンサー

04.mp3がないためにエラーになっているようですね。下記確認してみてください。

①04.mp3というファイルはプロジェクトにちゃんとimportできていますか?
②プロジェクトの中に含められている場合、Bundleにコピーする設定に入っていますか?
イメージ説明

投稿2017/03/19 16:01

jollyjoester

総合スコア1585

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

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

退会済みユーザー

退会済みユーザー

2017/03/20 04:07

一回インポートし直してみたりしたんですがやはりダメでした。 Bundleのコピーする設定というのは、Copy items if neededのことでしょうか?それのチャックボックスが外れてたのでチェックしデバッグしたところ今度はブレークしたところにエラーでThread 1:EXC_BAD_INSTRUCTION(code=EXC_1386_INVOP.subcode=0x0)とエラーが発生し、また、AppDelegate.swiftのclass AppDelegate: UIResponder, UIApplicationDelegate{の部分でも同じエラーが出てました。
jollyjoester

2017/03/20 08:24

Copy items if neededのチェックをしないと元の音声ファイルを別の場所に移動したときにプロジェクトから音声ファイルが見つからなくて最初のエラーになる可能性はあります。Copy items if neededをつけると音声ファイルがプロジェクトの中にコピーされ上記のようなことが発生しにくくなります。慣れないうちはCopy items if neededにチェックをしとくのをお勧めします。 いずれにせよインポートし直したことで本件については最初のエラーがでなくなった=正常に動くようになったようです。次に出るようになったエラーですが「該当のソースコード」をこちらの手元で動かすと正常に動作しました。ですので別件として再度情報を整理して質問するのが良いかと思います!
退会済みユーザー

退会済みユーザー

2017/03/20 12:32

インポートする際にはチェック入れることをしっかり意識していきます!そちらでは正常に動作したようなのでこちらも新しいプロジェクトでやってみるなど試して見ます。ありがとうございました????
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.49%

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

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

質問する

関連した質問