SystemSoundID _soundID;
NSURL *soundUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"coin" ofType:@"mp3"]];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)soundUrl, &_soundID);
AudioServicesPlaySystemSound(_soundID);
//播放完释放
AudioServicesDisposeSystemSoundID(_soundID);
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>
@property (nonatomic, strong) AVAudioPlayer *audioPlayer;
NSURL *soundUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"coin" ofType:@"mp3"]];
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];
if (self.audioPlayer)
{
if ([self.audioPlayer prepareToPlay])
{
// 播放时,设置喇叭播放否则音量很小
AVAudioSession *playSession = [AVAudioSession sharedInstance];
[playSession setCategory:AVAudioSessionCategoryPlayback error:nil];
[playSession setActive:YES error:nil];
}
}
[self.audioPlayer play];
网友评论