在Xcode中循环音频 [英] Looping Audio in Xcode
问题描述
我在我的应用中播放声音,我想循环播放。我对此的研究并没有完全解决我的问题。
Im playing a sound in my app that I would like to loop. My research into this hasnt quite sorted my issue.
我的代码.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;
但不确定如何在我的代码中实现,因为我收到numberOfLoops的未声明错误。一些建议将非常感谢
But unsure how to implement in my code as I get undeclared error for numberOfLoops. Some advice would be very much appreciated
推荐答案
这样的事情可以解决你的问题:
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;
}
然后如果你想停止它:
[player stop];
或暂停:
[player pause];
并将其导入头文件中:
#import <AVFoundation/AVFoundation.h>.
你应该在你的标题中声明它,然后合成它。
You should to ofcourse declare it in your header, then synthesize it.
//。h并添加粗体部分:
//.h and add the bold part:
@interface ViewController:UIViewController< AVAudioPlayerDelegate > {
@interface ViewController : UIViewController <AVAudioPlayerDelegate> {
AVAudioPlayer *player;
}
@property (nonatomic, retain) AVAudioPlayer *player;
//。m
@synthesize player;
这篇关于在Xcode中循环音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!