声音与AVAudioPlayer玩 [英] Sound not playing with AVAudioPlayer
问题描述
我搜索,我相信我的问题是非常独特的。我知道使用AVAudioPlayer当模拟器5.1错误,这不是我的问题。我正在一个5.1的iOS设备上。
I've searched and I believe my problem is quite unique. I'm aware of the Simulator 5.1 bug when using AVAudioPlayer which isn't my problem. I'm running on a iOS 5.1 device.
下面是我的头文件:
#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用于播放文件。
And I have a button in my .xib that plays the file.
的为了确保我正确地引用我的音频文件,我故意输入一个错误的文件名,确实是我得到了一个例外。为了确保该文件是可玩的,我把它寄给自己,发挥其在设备上。的
当我点击按钮,我听不见。有没有错误。我不知道什么是错的。
When I tap the button I hear nothing. There are no errors. I don't know what is wrong.
更多信息
iPhone 4与iOS 5.1
X $ C $Ç4.3.2
音频约为700kbps转换使用afconvert从大的.wav文件格式.caf
More information iPhone 4 with iOS 5.1 Xcode 4.3.2 Audio is about 700kbps converted to .caf format using afconvert from a big .wav file.
推荐答案
解决。
1:检查您的文件已在构建阶段被添加到复制包资源。
1.- Check if your file has been added to "Copy Bundle Resources" in Build Phases.
2:ViewController.m
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按钮。
3.- Connect the IBAction to your button in Interface Builder.
完成。
这篇关于声音与AVAudioPlayer玩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!