如果应用程序在ios中处于活动状态时通知如何发出声音?
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { [[NSNotificationCenter defaultCenter] postNotificationName:@"MessageReceived" object:self]; UIApplicationState state = [application applicationState]; if (state == UIApplicationStateActive) { // NSString *cancelTitle = @"Close"; NSString *showTitle = @"Show"; // NSString *message = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"]; UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"New Order" message:@"You just received new order" delegate:self cancelButtonTitle:nil otherButtonTitles:showTitle, nil]; UILocalNotification *localNotifcation = [[UILocalNotification alloc] init]; localNotifcation.userInfo = userInfo; localNotifcation.soundName = UILocalNotificationDefaultSoundName; // localNotifcation.alertBody = message; localNotifcation.fireDate = [NSDate date]; [[UIApplication sharedApplication] scheduleLocalNotification:localNotifcation]; [alertView show]; } else { //Do stuff that you would do if the application was not active } }
请给我一个解决方案,如果现在通知正在使用x-code 6.3,我怎样才能在应用程序运行时获得声音?
添加以下框架
#include
使用以下代码 –
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { UIApplicationState state = [application applicationState]; if (state == UIApplicationStateActive) { SystemSoundID soundID; CFBundleRef mainBundle = CFBundleGetMainBundle(); CFURLRef ref = CFBundleCopyResourceURL(mainBundle, (CFStringRef)@"mySoundName.wav", NULL, NULL); AudioServicesCreateSystemSoundID(ref, &soundID); AudioServicesPlaySystemSound(soundID); } else { // Push Notification received in the background } }
或者您可以使用系统声音 –
AudioServicesPlaySystemSound(1007);
对于振动 –
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
在这里看到所有声音列表。
您可以手动添加声音文件,并在应用处于活动状态时播放。
if ([application applicationState] == UIApplicationStateActive) { NSLog(@"active"); NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"alarm2" ofType:@"wav"]; NSURL *soundURL = [NSURL fileURLWithPath:soundPath]; AudioServicesCreateSystemSoundID((__bridge CFURLRef)soundURL, &_mySound); AudioServicesPlaySystemSound(self.mySound); }
如需更多默认声音,请查看以下答案: 播放系统声音而不导入自己的声音
希望能帮助到你
试试这段代码。 将此代码放在AppDelegate
.m中的AppDelegate
SystemSoundID soundID; CFBundleRef mainBundle = CFBundleGetMainBundle(); CFURLRef ref = CFBundleCopyResourceURL(mainBundle, (CFStringRef)@"Voicemail.wav", NULL, NULL); AudioServicesCreateSystemSoundID(ref, &soundID); AudioServicesPlaySystemSound(soundID);
这段代码完美地为我工作..
- Android – API请求
- 如何更深入地阅读Firebase Swift 3的快照?
- SWrevealViewController不使用navigationController
- 以swiftasynchronous加载tableView图像
- iOS Swiftmultidimensional array – 编译需要很长时间。 我应该改变什么?
- 编译FreeType for iPhone?
- 如何找到键盘未覆盖的视图部分(UIModalPresenationStyleFormSheet)?
- 将UIScrollViewPoint转换为UIImage + ZOOM + ROTATE
- 如何格式化单元格的文本标签的某些单词