我已经搜索过,我相信我的问题非常独特。我知道使用 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.- 检查您的文件是否已添加到构建阶段中的“复制捆绑资源”。
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/