iphone - 声音不使用 AVAudioPlayer 播放

标签 iphone ios audio avaudioplayer core-audio

我已经搜索过,我相信我的问题非常独特。我知道使用 AVAudioPlayer 时的 Simulator 5.1 错误,这不是我的问题。我在 iOS 5.1 设备上运行。

这是我的头文件:

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

-(IBAction)pushBell;

@end

和我的实现文件:

#import "BellViewController.h"

@interface BellViewController ()

@end

@implementation BellViewController

-(IBAction)pushBell {

    NSString *soundPath =[[NSBundle mainBundle] pathForResource:@"bell1" ofType:@"caf"];
    NSURL *soundURL = [NSURL fileURLWithPath:soundPath];
    NSError *error;

    AVAudioPlayer *theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:&error];
    [theAudio setDelegate:self];
    [theAudio setNumberOfLoops:0];
    [theAudio play];

}

@end

我的 .xib 中有一个播放文件的按钮。

为了确保我正确引用了我的音频文件,我故意输入了错误的文件名,结果确实出现了异常。为确保文件可播放,我将其邮寄给自己并在设备上播放。

当我点击按钮时,我什么也没听到。没有错误。我不知道出了什么问题。

更多信息 装有 iOS 5.1 的 iPhone 4 Xcode 4.3.2 使用 afconvert 从一个大的 .wav 文件将大约 700kbps 的音频转换为 .caf 格式。

最佳答案

已解决。

1.- 检查您的文件是否已添加到构建阶段中的“复制捆绑资源”。 Copy Bundle Resources

2.- ViewController.m

#import <AVFoundation/AVAudioPlayer.h>

@interface ViewController ()
@property (nonatomic, strong) AVAudioPlayer *theAudio;
- (IBAction)pushBell;
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSString *soundPath =[[NSBundle mainBundle] pathForResource:@"beep-beep" ofType:@"caf"];
    NSURL *soundURL = [NSURL fileURLWithPath:soundPath];

    NSError *error = nil;
    self.theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:&error];
}

- (IBAction)pushBell {
    [self.theAudio play];
}

3.- 将 IBAction 连接到 Interface Builder 中的按钮。

完成。

关于iphone - 声音不使用 AVAudioPlayer 播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9963230/

相关文章:

c# - 使用C#控制扬声器音量

iphone - 如何在 iPhone 中加载、渲染和显示由 Maya 或 Blender 创建的 obj 格式的 3D 模型,纹理为 png 或 jpg?

iphone - 带有 Facebook 单点登录的 iOS 应用程序中的“赞”按钮

iphone - 如何确定 View Controller

ios - iPad:在 IOS 应用程序中设置计时器以取消对已登录用户的授权,即使应用程序处于后台

ios - 主要细节中的 child 计数器

iphone - iPhone 上的视频卡拉 OK 开发

iphone - 使用完成 block 呈现和关闭 UIViewController - 没有协议(protocol)和委托(delegate)

ios - 使用 Auto Size 类隐藏 View

android 录音机启动无效状态 4