在 Xcode 中循环音频
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11376269/
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
Looping Audio in Xcode
提问by JSA986
Im playing a sound in my app that I would like to loop. My research into this hasnt quite sorted my issue.
我在我的应用程序中播放我想循环播放的声音。我对此的研究还没有完全解决我的问题。
my code .m
我的代码.m
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef;
soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"ar4", CFSTR
("wav"), NULL);
UInt32 soundID;
AudioServicesCreateSystemSoundID (soundFileURLRef, &soundID);
AudioServicesPlaySystemSound (soundID);
I think I need to put something like this in
我想我需要把这样的东西放进去
numberOfLoops = -1;
But unsure how to implement in my code as I get undeclared error for numberOfLoops. Some advice would be very much appreciated
但是不确定如何在我的代码中实现,因为我收到 numberOfLoops 的未声明错误。一些建议将不胜感激
回答by Bazinga
Something like this should do your problem:
像这样的事情应该可以解决您的问题:
NSString* resourcePath = [[NSBundle mainBundle] resourcePath];
resourcePath = [resourcePath stringByAppendingString:@"/YOURMUSICNAME.mp3"];
NSLog(@"Path to play: %@", resourcePath);
NSError* err;
//Initialize our player pointing to the path to our resource
player = [[AVAudioPlayer alloc] initWithContentsOfURL:
[NSURL fileURLWithPath:resourcePath] error:&err];
if( err ){
//bail!
NSLog(@"Failed with reason: %@", [err localizedDescription]);
}
else{
//set our delegate and begin playback
player.delegate = self;
[player play];
player.numberOfLoops = -1;
player.currentTime = 0;
player.volume = 1.0;
}
Then if you want to stop it:
那么如果你想阻止它:
[player stop];
or pause it :
或暂停它:
[player pause];
and also import it in your header file:
并将其导入到您的头文件中:
#import <AVFoundation/AVFoundation.h>.
You should to ofcourse declare it in your header, then synthesize it.
你当然应该在你的头文件中声明它,然后合成它。
//.h and add the boldpart:
//.h 并添加粗体部分:
@interface ViewController : UIViewController <AVAudioPlayerDelegate> {
@interface ViewController : UIViewController < AVAudioPlayerDelegate> {
AVAudioPlayer *player;
}
@property (nonatomic, retain) AVAudioPlayer *player;
//.m
//.m
@synthesize player;