AVAudioPlayer initWithData Problem
I added a sound file in my Xcode resources folder.
I want to play the sound file by AVAudioPlayer
I can use the image by [UIImage imageNamed:@"xxxx.png"];
But I don't know how to attach the file to the AVAudioPlayer.
Thank 开发者_高级运维you for helping me.
In the .h file
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>
@interface RootViewController : UIViewController {
AVAudioPlayer *audioPlayer;
}
@property(nonatomic, retain) AVAudioPlayer *audioPlayer;
& also synthesize this in your .m file
Now in your .m file
NSString *mp3Path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"test.mp3"];
NSURL *mp3Url = [NSURL fileURLWithPath:mp3Path];
NSError *err;
AVAudioPlayer *audPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:mp3Url error:&err];
[self setAudioPlayer:audPlayer];
if(audioPlayer)
[audioPlayer play];
[audPlayer release];
Also add the follwing frameworks
AudioToolbox.framework & AVFoundation.framework
Hope it helps
精彩评论