我在 xcode 中进行了以下设置,但没有声音出来。我之前有 2 个 .wav 文件可以使用,但其他 .mp3 和 .wav 文件无法使用,我想知道它是编写的代码还是声音文件?文件的比特率或大小有什么限制吗?请参阅下面的代码。我还将声音文件添加到项目中。
**ViewController.h**
import UIKit/UIKit.h
import AVFoundation/AVFoundation.h
import AudioToolbox/AudioToolbox.h
@interface ViewController :UIViewController
-(IBAction)AntlerRabbit;
@end
**ViewController.m**
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
-(IBAction)AntlerRabbit {
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef;
soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"AntlerRabbit", CFSTR("aiff"), NULL);
UInt32 soundID;
AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
AudioServicesPlaySystemSound(soundID);
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
然后我转到 MainStoryboard.storyboard 并放置一个按钮并将其链接到“AntlerRabbit”,但模拟器中没有任何 react
最佳答案
答案在这里:
Using Audio in Xcode
模拟器没有声音播放的原因是因为当你使用时,
AudioServicesPlaySystemSound
声音最长不得超过 30 秒。我使用的声音略高于它。如果有人知道如何使用超过 30 秒的声音文件,请发布代码:) 将不胜感激。
关于xcode - Xcode 中的 .mp3 和 .wav 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13852266/