搖動(dòng)播放音頻的效果
導(dǎo)入頭文件#import-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{
NSLog(@"開(kāi)始搖動(dòng)");
//創(chuàng)建一個(gè)系統(tǒng)聲音ID
SystemSoundID soundID;
//設(shè)置短效音頻資源
```
AudioServicesCreateSystemSoundID((__bridge CFURLRef _Nonnull)([NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"音效" ofType:@"caf"]]), &soundID);
```
//開(kāi)啟震動(dòng)效果
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
//播放
AudioServicesPlaySystemSound(soundID);
}
-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{
NSLog(@"搖動(dòng)結(jié)束");
self.view.backgroundColor = [[UIColor alloc]initWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:1];
}
-(void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event{
}
設(shè)置后臺(tái)播放
//設(shè)置后臺(tái)播放模式nnn ------? AVAudioSession音頻會(huì)話
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback error:nil];
//后臺(tái)保持活躍
[session setActive:YES error:nil];
//設(shè)置
//? ? 拔出耳機(jī)暫停播放,to通過(guò)觀察者檢測(cè)
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(routeChange:) name:AVAudioSessionRouteChangeNotification object:nil];
}
-(void)routeChange:(NSNotification *)notification{
NSDictionary *dic=notification.userInfo;
int changeReason= [dic[AVAudioSessionRouteChangeReasonKey] intValue];
if (changeReason==AVAudioSessionRouteChangeReasonOldDeviceUnavailable) {
AVAudioSessionRouteDescription *routeDescription=dic[AVAudioSessionRouteChangePreviousRouteKey];
AVAudioSessionPortDescription *portDescription= [routeDescription.outputs firstObject];
//原設(shè)備為耳機(jī)則暫停
if ([portDescription.portType isEqualToString:@"Headphones"]) {
if ([_player isPlaying])
{
[_player pause];
_timer.fireDate=[NSDate distantFuture];
}
}
}
}
info.plist文件的設(shè)置