答案 0 :(得分:1)
有两种方法可以做到:
1)使用AVAudioPlayer
在myPic.m
添加:
//at the top
#import <AudioToolbox/AudioToolbox.h>
#import <MediaPlayer/MediaPlayer.h>
@implementation myPic {
AVAudioPlayer *mySound;
}
//then use this function where ever you want to play sound
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource: @"mySound" ofType: @"mp3"]; //wav, aiff, ogg, ext
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
loopUno = [[AVAudioPlayer alloc] initWithContentsOfURL: fileURL error: nil];
loopUno.numberOfLoops = 0; // here loop setup infinite is -1
[mySound play];
2)我更喜欢,没有循环但更稳定的是SystemSoundID soundID
//at the top
#import <AudioToolbox/AudioToolbox.h>
#import <MediaPlayer/MediaPlayer.h>
@implementation myPic {
SystemSoundID soundID;
}
//then use this function where ever you want to play sound
AudioServicesDisposeSystemSoundID(soundID);
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef;
soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"mySound" ,CFSTR ("mp3") , NULL); //wav, aiff, ogg, ext
AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
AudioServicesPlaySystemSound(soundID);
覆盖发言人:
- (void)viewDidLoad {
[super viewDidLoad];
AVAudioSession* session = [AVAudioSession sharedInstance];
BOOL success;
NSError* error;
success = [session setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];
success = [session overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&error];
success = [session setActive:YES error:&error];
}
或者如果您在应用中使用更多音频,请在AppDelegate.m
下添加application didFinishLaunchingWithOptions:
内的功能并将您的代表导入到您需要的地方
希望这能帮到你;)
答案 1 :(得分:0)