播放音频Iphone的问题

时间:2010-02-16 15:29:41

标签: iphone objective-c avaudioplayer

我只是想让一个简单的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

2 个答案:

答案 0 :(得分:1)

您是否在项目中添加了AVFoundation框架?

通过右键单击Frameworks组并单击Add&gt;来执行此操作。现有框架

答案 1 :(得分:0)

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

  1. 群组和文件 - &gt;展开您的项目
  2. 右键单击链接二进制文件库
  3. 选择添加
  4. 选择现有框架 - &gt;弹出现有框架列表
  5. 选择 AVFoundation.framework
  6. 点击添加
  7. 这可以解决您的问题。它对我有用。

相关问题