xcode 无法使用 AVAudioPlayer 获取声音文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15622694/
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
xcode cannot get sound file to play with AVAudioPlayer
提问by mablecable
I am new to iOS and trying to add sound to my app. I have followed several tutorials about adding a simple sound file to my project, but ALL with the same result. I get no errors, but no sound plays. Any ideas on what I am doing wrong? Any help would be appreciated!
我是 iOS 新手,正在尝试为我的应用添加声音。我遵循了几个关于向我的项目添加简单声音文件的教程,但结果都相同。我没有收到任何错误,但没有声音播放。关于我做错了什么的任何想法?任何帮助,将不胜感激!
- (IBAction)playSound:(id)sender {
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"mp3"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
NSLog(soundFilePath);
NSError *error;
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:&error];
player.numberOfLoops = 1;
[player play];
}
**UPDATE***
**更新***
So I never had any luck with the above code which is very discouraging, but I did end up getting it to work using the AudioToolbox Framework instead, using this:
所以我对上面的代码没有任何运气,这非常令人沮丧,但我最终使用 AudioToolbox Framework 让它工作,使用这个:
SystemSoundID soundID;
NSString *soundFile = [[NSBundle mainBundle]
pathForResource:@"test" ofType:@"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)
[NSURL fileURLWithPath:soundFile], & soundID);
AudioServicesPlaySystemSound(soundID);
Can anyone explain to me the difference, why this one works and the other does not?? I am curious more than anything. Would love some clarification.
谁能向我解释其中的区别,为什么这个有效而另一个无效?我比什么都好奇。希望得到一些澄清。
回答by mablecable
Finally figured it out. I needed to create a property for AVAudioPlayer in my header file in order to get it to work. Here is what I did:
终于想通了。我需要在我的头文件中为 AVAudioPlayer 创建一个属性才能让它工作。这是我所做的:
// .h file
@interface ThirdViewController : UIViewController {
AVAudioPlayer *audioPlayer;
}
@property (nonatomic, retain) AVAudioPlayer *audioPlayer;
- (void) playSound //method in .m file
{
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:@"test"
ofType:@"mp3"]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
[audioPlayer play];
}
回答by Apollo SOFTWARE
Simplify your code by doing the follow. Also drop your test.mp3 to the root of your project directory in xcode. Your mp3 placement and location is very important. Also ensure volume is up, and vibrate is off of your device, and it's unmuted.
通过执行以下操作来简化您的代码。还将您的 test.mp3 放到 xcode 中项目目录的根目录中。您的 mp3 位置和位置非常重要。还要确保音量已调高,振动已关闭您的设备,并且已取消静音。
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:@"test"
ofType:@"mp3"]];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:url
error:nil];
[audioPlayer play];
回答by Chester Cordero
Make sure you install quartz.h, or a specific framework that supports audio players. Just double check.
确保安装了quartz.h 或支持音频播放器的特定框架。仔细检查一下。
Had the same problem a while back and the framework must be installed properly.
前阵子遇到了同样的问题,必须正确安装框架。