我正在尝试使用Xcode为iPhone播放目标c中的mp3文件。
在viewDidLoad中:
NSURL *mySoundURL = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/mySound.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError *myError;
mySound = [[AVAudioPlayer alloc] fileURLWithPath:heartBeatURL error:&myError];
[mySound play];
我在这里找到了一个建议:Problem while playing sound using AVAudioPlayer?
但它对我不起作用,只会产生更多问题。
当程序启动时,我在输出中得到了这个,程序崩溃了:
架构i386的未定义符号: “_OBJC_CLASS _ $ _ AVAudioPlayer”,引自: SecondViewController.o中的objc-class-ref ld:找不到架构i386的符号 collect2:ld返回1退出状态
我在这里做错了什么?
答案 0 :(得分:1)
在我看来,您还没有将AVFoundation框架链接到您的应用程序中。
假设最近有足够的xcode:
以下是一些可用于比较的AVAudioPlayer代码:
NSURL *mySoundURL = [NSURL URLWithString:[[NSBundle mainBundle] pathForResource:@"BadTouch" ofType:@"mp3"]];
NSError *myError;
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:mySoundURL error:&myError];
[self.player play];
答案 1 :(得分:1)
将AVFoundation.framework添加到您的项目目标Link Binary With Libraries
然后导入.h:
#import <AVFoundation/AVFoundation.h>
@interface ViewController : UIViewController <AVAudioPlayerDelegate> {
AVAudioPlayer *player;
}
@property (strong,nonatomic) AVAudioPlayer *player;
@end
你的.m中的:
@synthesize player;
NSString* resourcePath = [[NSBundle mainBundle] resourcePath];
resourcePath = [resourcePath stringByAppendingString:@"/mySound.mp3"];
NSLog(@"Path to play: %@", resourcePath);
NSError* err;
//Initialize our player pointing to the path to our resource
player = [[AVAudioPlayer alloc] initWithContentsOfURL:
[NSURL fileURLWithPath:resourcePath] error:&err];
if( err ){
//bail!
NSLog(@"Failed with reason: %@", [err localizedDescription]);
}
else{
//set our delegate and begin playback
player.delegate = self;
[player play];
}