Playing Sound On Button (AV Audio Player) returning nil string parameter

﹥>﹥吖頭↗ 提交于 2019-12-14 02:43:13

问题


I am attempting to play a sound on button press. Previously, I used Audiotoolbox which worked just fine with the same file type. However, i need to change to using AvAudioPlayer becuase it doesn't use the ringer volume.

So last week I developed an understanding of using the audio player to play a sound on click. I had it working a treat on my over app using the exact same code. However, this app despite trying countless time will not work with AVFoundation/AudioPlayer. I have tried adding new files, new buttons, changes the extension of the files. the file names are in the resources folder. I have not spelt the name wrong.

The exact problem is it is returning a nil string parameter when trying to find my file i guess:

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

I have looked on just about every forum post about this but none of which will help me. I can only think theres an underlying problem in my project maybe the resources folder not the actual AVAudioPlayer code due to the fact i had this exact code working the same on another project. Any help would be much appreciated as I am stumped.

Here is the important code:

This is in my .h

@interface MainViewController : UIViewController

@property (strong, nonatomic) AVAudioPlayer *theAudio;
- (IBAction)test:(id)sender;

This is in my .m

@synthesize theAudio;

- (IBAction)test:(id)sender {
NSString *path = [[NSBundle mainBundle] pathForResource:@"beast" ofType:@"wav" ];
self.theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path]      error:nil];
[self.theAudio play];
}

Help please


回答1:


The nil path returned by [NSBundle pathForResource:ofType:] means that the wav file has not been copied into your app package.

Have you added the files to your project? If so, are the files included in your target? Check in Target > Build Phases > Copy Bundle Resources or select them in the project navigator (⌘1) and check the box in the File Inspector (⌘⌥1).




回答2:


I had the same issue. Using AVPlayer instead of AVAudioPlayer save my day.



来源:https://stackoverflow.com/questions/22490952/playing-sound-on-button-av-audio-player-returning-nil-string-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!