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

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

新規登録して質問してみよう
ただいま回答率
85.50%
Objective-C

Objective-Cはオブジェクト指向型のプログラミング言語のひとつです。C言語をベースにSmalltalkが取り入れられています。

Q&A

解決済

1回答

2085閲覧

Objective-C、ボタンタップで音を再生したい。

TsksHsgw

総合スコア23

Objective-C

Objective-Cはオブジェクト指向型のプログラミング言語のひとつです。C言語をベースにSmalltalkが取り入れられています。

0グッド

0クリップ

投稿2016/12/01 14:29

###前提・実現したいこと
質問をご覧いただきまして、ありがとうございます。
元々swiftから勉強を始め、最近Objective-Cを勉強し始めました。

「ボタンがタップされた時に音が鳴る」
というシンプルな機能を実現したいのですが、コードにエラーはなく、
シミュレータでボタンをタップした時に

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter'

というエラーが出てしまいます。

NSURL initFileURLWithPathで設定したurlにnilが格納されてしまっていることが原因と思い、以下を参照しながら色々と試行錯誤しているのですが、なかなか上手くいきません。

参照①-Stack Overflow
参照②-Stack Overflow

以下にソースコードを付します。

###ソースコード
ViewController.m

#import "ViewController.h" #import <AVFoundation/AVFoundation.h> @interface ViewController()<AVAudioPlayerDelegate> - (IBAction)wineTapped:(UIButton *)sender; @property(nonatomic) AVAudioPlayer *audioplayer; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction)wineTapped:(UIButton *)sender{ long tagNum = sender.tag; printf("%ld", sender.tag); NSError *error = nil; if(tagNum == 1){ NSLog(@"%ld", tagNum); NSString *path1 = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"mp3"]; NSURL *url1 = [[NSURL alloc] initFileURLWithPath:path1]; self.audioplayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url1 error:&error]; [self.audioplayer prepareToPlay]; [self.audioplayer play]; } else if(tagNum == 2){ NSLog(@"%ld", tagNum); NSString *path2 = [[NSBundle mainBundle] pathForResource:@"2" ofType:@"mp3"]; NSURL *url2 = [[NSURL alloc] initFileURLWithPath:path2]; self.audioplayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url2 error:&error]; [self.audioplayer prepareToPlay]; [self.audioplayer play]; } else if(tagNum == 3){ NSLog(@"%ld", tagNum); NSString *path3 = [[NSBundle mainBundle] pathForResource:@"3" ofType:@"mp3"]; NSURL *url3 = [[NSURL alloc] initFileURLWithPath:path3]; self.audioplayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url3 error:&error]; [self.audioplayer prepareToPlay]; [self.audioplayer play]; } else if(tagNum == 4){ NSLog(@"%ld", tagNum); NSString *path4 = [[NSBundle mainBundle] pathForResource:@"4" ofType:@"mp3"]; NSURL *url4 = [[NSURL alloc] initFileURLWithPath:path4]; self.audioplayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url4 error:&error]; [self.audioplayer prepareToPlay]; [self.audioplayer play]; } else if(tagNum == 5){ NSLog(@"%ld", tagNum); NSString *path5 = [[NSBundle mainBundle] pathForResource:@"5" ofType:@"mp3"]; NSURL *url5 = [[NSURL alloc] initFileURLWithPath:path5]; self.audioplayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url5 error:&error]; [self.audioplayer prepareToPlay]; [self.audioplayer play]; } } @end

お詳しい方いらっしゃいましたら、改善方法をご教示いただけると幸いです。

よろしくお願い致します。

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

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

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

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

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

guest

回答1

0

ベストアンサー

Objective-Cのコードを実行する時は、
http://qiita.com/makoto_kw/items/a4346cf5d8124ecd13ec
を参考にして、Objecive-Cの例外でブレークする設定をしておくことをお勧めします。

そうすると、今回のような例外が発生した時に、例外を起こした箇所で停止するので、
原因を見つけやすくなります。

今回のケースは、
NSString *path1 = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"mp3"];
で、アプリにバンドルした"1.mp3"というファイルのパスを取得しようとして、
おそらくそれが存在しなかったためにpath1がnilになったのだと思います。
(1〜5まで同様)

もし"1.mp3"というファイルをXcodeに追加しているはずなのに、このエラーが出るのであれば、
http://useandmake.hatenablog.com/entry/2015/12/22/213554
を参考に、"1.mp3"が対象ターゲットに追加されているか確認してみてください。
この参考URLでは、再度コピーし直すことを勧めていますが、対象ファイル(1.mp3)の
File inspectorのTarget Membershipを確認し、対象ターゲットがチェックOFFに
なっていたらそれをONにするだけでも大丈夫です。

投稿2016/12/01 15:45

TakeOne

総合スコア6299

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

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

TsksHsgw

2016/12/03 12:23

TakeOne様 ご回答ありがとうございます。 解決致しました。ブレークの設定をしてエラーの内容を分析するクセをつけるよう心がけたいと思います!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問