开发者

AVAudioPlayer initWithData Problem

开发者 https://www.devze.com 2023-02-10 18:01 出处:网络
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\"];

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号