iPhone SDK: Play a single WAV from a button

对着背影说爱祢 提交于 2019-12-10 10:38:19

问题


I am currently trying out this code:

NSString *path = [[NSBundle mainBundle] pathForResource:@"dream" ofType:@"m4a"];  
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];  
theAudio.delegate = self;  
[theAudio play];

However, the SDK says that the ViewController does not implement the AVAudioPlayer Delegate.

Any body any ideas on how to play a WAV (or M4a) using the 2.2 SDK?


回答1:


You need to add <AVAudioPlayerDelegate> to the end of your controller's class declaration, along these lines:

@interface MyViewController : UIViewController <AVAudioPlayerDelegate>

This'll probably generate some more warnings about delegate methods you need to implement - follow those and you'll be good to go.




回答2:


Interesting. Try NSURL -URLWithString: - that's what I use in combination with the bundle method you're using. I don't see anything that could be causing a problem other than that. You're testing it on the device, right?




回答3:


The answer I posted here should definitely help you. Just use audioservices for small wave sounds, unless you need concurrency, it's faster.




回答4:


iPhoneToot.com has its own itootSound class and a VIDEO to show you how to play a wav file in 2 lines of code. Check them out at iPhoneToot.com. it will save you hours if not days of time!!



来源:https://stackoverflow.com/questions/444595/iphone-sdk-play-a-single-wav-from-a-button

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