0

我只是想让一个简单的 mp3 播放器工作。我收到一个我不太明白的错误。

我在我的 h 文件中声明了一个 AVAudioPlayer 实例,并在我的 .m 文件中合成它......

Undefined symbols:
  ".objc_class_name_AVAudioPlayer", referenced from:
      literal-pointer@__OBJC@__cls_refs@AVAudioPlayer in PromoTabOptionHome.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

我的 h 文件是这样定义的

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>


@interface PromoTabOptionHome : UIViewController {

    UIButton *playPause;
    AVAudioPlayer* audioPlayer;
    NSMutableString *audioPath;
    BOOL playing;

}

@property(nonatomic,retain)UIButton *playPause;
@property(nonatomic,retain)NSMutableString *audioPath;
@property(nonatomic,retain)AVAudioPlayer *audioPlayer;
-(id)initWithTabBar;
@end

而我的实现文件是这样的

#import "PromoTabOptionHome.h"
@implementation PromoTabOptionHome

@synthesize playPause;
@synthesize audioPath;
@synthesize audioPlayer;

-(id) initWithTabBar {
    if ([self init]) {
        self.tabBarItem.image = [UIImage imageNamed:@"home.png"];

        // set the long name shown in the navigation bar
        self.navigationItem.title=@"Nav Title";
    }
    return self;

}


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];

    playPause = [UIButton buttonWithType:UIButtonTypeCustom];
    playPause.frame = CGRectMake(-20,280, 100,100);
    [playPause setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];
    [playPause addTarget:self action:@selector(buttonPushed:) 
          forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:playPause];

    playing=NO;

    NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:audioPath, [[NSBundle mainBundle] resourcePath]]];

    NSError *error;
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    //audioPlayer.numberOfLoops = -1;

    if (audioPlayer == nil)
        NSLog([error description]);
    else
        [audioPlayer play];
}

- (void) buttonPushed:(id)sender
{
    NSLog(@"It works!");

    switch (playing) {
        case NO:
            playing=YES;
              [playPause setImage:[UIImage imageNamed:@"pause.png"] forState:UIControlStateNormal];
            break;
        case YES:
            playing=NO;
              [playPause setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];
            break;
        default:
            break;
    }
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end
4

2 回答 2

1

您是否将AVFoundation框架添加到您的项目中?

通过右键单击框架组并单击添加 > 现有框架来执行此操作

于 2010-02-16T15:43:08.357 回答
0

您可能会错过AVFoundation.framework项目中的参考。默认情况下,不添加此引用。您需要AVFoundation.framework手动添加。

  1. Groups and Files --> 扩展你的项目
  2. 右键单击Link Binaries with Libraries
  3. 选择添加
  4. 选择Existing Frameworks --> 弹出的现有框架列表
  5. 选择AVFoundation.framework
  6. 点击添加

这应该可以解决您的问题。这个对我有用。

于 2010-05-28T20:26:23.083 回答