ios 声音不能用 AVAudioPlayer 播放
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9963230/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sound not playing with AVAudioPlayer
提问by Aen Tan
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.
我已经搜索过,我相信我的问题非常独特。在使用 AVAudioPlayer 时,我知道 Simulator 5.1 错误,这不是我的问题。我在 iOS 5.1 设备上运行。
Here's my header file:
这是我的头文件:
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <AVFoundation/AVAudioPlayer.h>
-(IBAction)pushBell;
@end
and my implementation file:
和我的实现文件:
#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
And I have a button in my .xib that plays the file.
我的 .xib 中有一个按钮可以播放文件。
To make sure I referenced my audio file correctly, I deliberately type a wrong file name and indeed I got an exception. To make sure the file is playable, I mailed it to myself and played it on the device.
为了确保我正确引用了我的音频文件,我故意输入了错误的文件名,实际上我得到了一个例外。为确保文件可播放,我将其邮寄给自己并在设备上播放。
When I tap the button I hear nothing. There are no errors. I don't know what is wrong.
当我点击按钮时,我什么也没听到。没有错误。我不知道出了什么问题。
More informationiPhone 4 with iOS 5.1 Xcode 4.3.2 Audio is about 700kbps converted to .caf format using afconvert from a big .wav file.
更多信息使用 iOS 5.1 Xcode 4.3.2 音频的 iPhone 4 大约 700kbps 使用 afconvert 从一个大的 .wav 文件转换为 .caf 格式。
回答by 3lvis
SOLVED.
解决了。
1.- Check if your file has been added to "Copy Bundle Resources" in Build Phases.
1.- 检查您的文件是否已添加到构建阶段的“复制捆绑资源”中。
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.- Connect the IBAction to your button in Interface Builder.
3.- 将 IBAction 连接到 Interface Builder 中的按钮。
Done.
完毕。
回答by jowie
This was my issue, so I'm just posting it here in case it helps anyone!
这是我的问题,所以我只是把它贴在这里,以防它对任何人有帮助!
Believe it or not, there appears to be a bug (in iOS 5.1 on iPad 2 at least) where if you had the Side Switch set to Mute and it was Muted, then set the Side Switch to Lock Rotation, AVAudioPlayer sounds WON'T PLAY except through headphones!
信不信由你,似乎有一个错误(至少在 iPad 2 上的 iOS 5.1 中),如果你将侧边开关设置为静音并且它被静音,然后将侧边开关设置为锁定旋转,AVAudioPlayer 听起来不会除了通过耳机播放!
So I had to go to the Settings app, change the switch back to Mute, then unmute the iPad, switch it back to Lock Rotation and my app started to work properly.
所以我不得不去设置应用程序,将开关改回静音,然后取消 iPad 静音,将其切换回锁定旋转,我的应用程序开始正常工作。
回答by Rok Jarc
Your theAudio
AVAudioPlayer
is deallocated before it can even start playing (since it's allocated as a local variable inside pushBell method.
您theAudio
AVAudioPlayer
在它甚至可以开始播放之前就被释放了(因为它在 pushBell 方法中被分配为局部变量。
Change your code to:
将您的代码更改为:
header file:
头文件:
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <AVFoundation/AVAudioPlayer.h>
@interface BellViewController:UIViewController
@property (nonatomic, strong) AVAudioPlayer *theAudio;
-(IBAction)pushBell;
@end
implementation file:
实现文件:
#import "BellViewController.h"
@implementation BellViewController
@synthesize theAudio = _theAudio;
- (void)viewDidLoad {
if (!theAudio) {
NSString *soundPath =[[NSBundle mainBundle] pathForResource:@"bell1" ofType:@"caf"];
NSURL *soundURL = [NSURL fileURLWithPath:soundPath];
NSError *error;
_theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:&error];
[self.theAudio setDelegate:self];
[self.theAudio setNumberOfLoops:0];
[self.theAudio prepareToPlay];
}
}
-(IBAction)pushBell {
[self.theAudio play];
}
@end
I'm assuming that BellController
is a UIViewController
, if it's a different kind of class just modify the code accordingly.
我假设这BellController
是一个UIViewController
,如果它是不同类型的类,只需相应地修改代码。
回答by Carles Estevadeordal
I think that it is a memory issue, you should instantiate the AVAudioPlayer *theAudio
as a global class object and initialise it in the init
method. Then just run the [theAudio play];
method in the -(IBAction)pushBell
method.
我认为这是一个内存问题,您应该将其实例AVAudioPlayer *theAudio
化为全局类对象并在init
方法中对其进行初始化。然后只需运行[theAudio play];
方法中的-(IBAction)pushBell
方法。
I responded do a similar issue related to playing a system sound (which in fact is what you are trying to do), check it out my response is at the bottom:
我回答了一个与播放系统声音相关的类似问题(这实际上是你想要做的),看看我的回答在底部:
Couldn't play system sound after switching to iOS 5
By the way consider playing the bell as a system sound, AVAudioPlayer
is more appropriate to play sound tracks, not bells and effects.
顺便考虑将铃声作为系统声音播放,AVAudioPlayer
更适合播放音轨,而不是铃声和效果。
I hope that it helps.
我希望它有帮助。
回答by Jon Hocking
I agree with hooleyhoop. Try
我同意hooleyhoop。尝试
NSLog(@"%@", error.userInfo)
to see what's coming out.
看看会发生什么。
Another alternative would be to set up the player object in either viewDidLoad
or initWithNib
and storing it as a property on your object. This way you can use something like
[self.theAudio prepareToPlay];
另一种选择是在viewDidLoad
或 中设置播放器对象initWithNib
并将其存储为对象的属性。这样你就可以使用类似 [self.theAudio prepareToPlay];
To avoid loading up the audio each time you press the button, which could save you some time if it's high quality audio. Then just call [self.theAudio play];
in you IBAction
method.
避免每次按下按钮时都加载音频,如果它是高质量的音频,这可以为您节省一些时间。然后只需调用[self.theAudio play];
你的IBAction
方法。
Sorry, if this answer is a little sloppy - typing on an iPad so can't double check properly.
抱歉,如果这个答案有点草率 - 在 iPad 上打字,所以无法正确复查。
回答by Sidwyn Koh
You should give it some time to prepare the pronunciation since it's probably buffering from a URL.
您应该给它一些时间来准备发音,因为它可能是从 URL 缓冲的。
The code:
编码:
[theAudio prepareToPlay];
[self performSelector:@selector(playMusic) withObject:nil afterDelay:3.0];