AVAudioPlayer 的问题

发布于 2024-10-24 04:43:33 字数 1595 浏览 1 评论 0原文

我是 iPhone 开发新手。

我想在点击 UIButton 时播放声音。我尝试了以下操作:

ViewController.h:

@interface JahrenzeitenViewController : UIViewController <AVAudioPlayerDelegate> {

    UIButton *button_PlaySound;
    AVAudioPlayer *audioPlayer;
}

@property (nonatomic, retain) IBOutlet UIButton *button_PlaySound;

ViewController.m

- (IBAction)playSound {

    //[audioPlayer release];
    //audioPlayer = nil;

    NSString *audioFilePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"wav"];
    NSURL *audioFileURL = [NSURL fileURLWithPath:audioFilePath];
    NSError *error = nil;
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFileURL error:&error];
    [audioPlayer setDelegate:self];
    [audioPlayer prepareToPlay];
    [audioPlayer play];
    if (audioPlayer == nil) 
        NSLog(@"Error playing sound. %@", [error description]);
    else
        [audioPlayer play];

如果我尝试运行该应用程序,则会收到以下错误:

架构 i386 的未定义符号: “_OBJC_CLASS_$_AVAudioPlayer”,引用自: JahrenzeitenViewController.o 中的 objc-class-ref ld:未找到架构 i386 的符号 collect2:ld返回1退出状态

我还尝试将行更改

audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFileURL error:&error];

audioPlayer = [audioPlayer initWithContentsOfURL:audioFileURL error:&error];

Then 我没有收到上面的错误,但它仍然没有播放,并且我在“playSound”方法中从 NSLog 中获取了 DebugMessage:

播放声音时出错。 (空)

我希望你能帮助我。先感谢您 :)

I'm new to the iPhone-Development.

I want to play a Sound when an UIButton is tapped. I tried the following:

ViewController.h:

@interface JahrenzeitenViewController : UIViewController <AVAudioPlayerDelegate> {

    UIButton *button_PlaySound;
    AVAudioPlayer *audioPlayer;
}

@property (nonatomic, retain) IBOutlet UIButton *button_PlaySound;

ViewController.m

- (IBAction)playSound {

    //[audioPlayer release];
    //audioPlayer = nil;

    NSString *audioFilePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"wav"];
    NSURL *audioFileURL = [NSURL fileURLWithPath:audioFilePath];
    NSError *error = nil;
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFileURL error:&error];
    [audioPlayer setDelegate:self];
    [audioPlayer prepareToPlay];
    [audioPlayer play];
    if (audioPlayer == nil) 
        NSLog(@"Error playing sound. %@", [error description]);
    else
        [audioPlayer play];

If I try to run the App, I get the following error:

Undefined symbols for architecture i386:
"_OBJC_CLASS_$_AVAudioPlayer", referenced from:
objc-class-ref in JahrenzeitenViewController.o
ld: symbol(s) not found for architecture i386
collect2: ld returned 1 exit status

I also tried to change the Line

audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFileURL error:&error];

to

audioPlayer = [audioPlayer initWithContentsOfURL:audioFileURL error:&error];

Then I don't get the error above, but its still not playing and i get the DebugMessage from my NSLog in the "playSound"-Method:

Error playing sound. (NULL)

I hope you can help me. Thank you in advance :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

∞梦里开花 2024-10-31 04:43:33

您需要将 AVFoundation 框架添加到您的项目中。 (右键单击您的主要目标,然后添加现有框架)。

You need to add the AVFoundation framework to your project. (right-click on your main target then add existing framework).

渔村楼浪 2024-10-31 04:43:33

添加到其他答案。

您应该始终查看类“Ie AVAudioPlayer”的文档。当你打算使用它时你不熟悉它。类的文档(类参考)顶部有一个表格,显示该类属于哪个框架:

enter image description here

一旦您知道这一点,请将框架添加到项目中。并将其导入到头文件(.h)中,正如其他人指出的那样

To add to the other answers.

You should always look at the documentation of a class 'I.e AVAudioPlayer. That you are unfamiliar with when you intend to use it. There at the top of the documentation ( Class Reference) for the the Class, is a table showing what framework the class belongs to:

enter image description here

Once you know this add the framework to the project. And also import it into the header (.h) file as others have pointed out

最冷一天 2024-10-31 04:43:33

谢谢你,它成功了。我不知道我必须添加框架:S

我使用了以下说明:

  1. 在项目导航器中,选择您的项目选择
  2. 您的目标选择
  3. “构建阶段”选项卡
  4. 打开“将二进制文件与库链接”扩展器
  5. 单击“+” ' 按钮
  6. 选择您的框架

thank you, it worked. I didn't know that I have to add the Framework :S

I used the following instructions:

  1. In the project navigator, select your project
  2. Select your target
  3. Select the 'Build Phases' tab
  4. Open 'Link Binaries With Libraries' expander
  5. Click the '+' button
  6. Select your framework
苍暮颜 2024-10-31 04:43:33

请将 AVFoundation 框架添加到您的项目中。我认为您应该添加 以及

Pls add the AVFoundation framework to your project. And i think you should add <AVFoundation/AVFoundation.h> along with <AudioToolbox/AudioToolbox.h>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文