增加客观cos中自定义本地通知声音的时间

时间:2018-03-28 07:11:28

标签: ios objective-c audio uilocalnotification duration

我希望为local notification增加自定义铃声的时间,但

Apple说:

声音文件的长度必须小于30秒。如果声音文件超过30秒,系统将播放默认声音。

Here is the documentation

但我的声音文件是5分钟

我试过这样的方式:

content.sound = [soundName:@"mycustomtone.aiff"];
content.time = [timeDuration: 300];

我知道300秒超过30秒,这不是定义时间的正确方法,因为通知时间没有时间定义。

如果可能,请帮助我将自定义声音的持续时间延长30秒以上!

我知道没有什么是不可能的如果开发人员想要的话!

这是我正在做的事情:

 #import "ViewController.h"

 @interface ViewController () {

 NSUserDefaults *defaults;
 }

 @end

 bool isGrantedNotificationAccess;

 @implementation ViewController

 - (void)viewDidLoad {
 [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

isGrantedNotificationAccess = false;
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
UNAuthorizationOptions options = UNAuthorizationOptionAlert+UNAuthorizationOptionSound;

[center requestAuthorizationWithOptions:options completionHandler:^(BOOL granted, NSError * _Nullable error) {
    isGrantedNotificationAccess = granted;
}];
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
 }


 - (IBAction)notifyButton:(id)sender {


 if (isGrantedNotificationAccess) {
    NSLog(@"clicked");

    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];

    content.title = @"Notification Title";
    content.subtitle = @"Notification Subtitle";
    content.body = @"Notification body";
    content.sound = [UNNotificationSound soundNamed:@"alarm_clock_2015.aiff"];
    content.timeDuration = 300;


UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:60 repeats:YES];

UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"UYLocalNotification" content:content trigger:trigger];

[center addNotificationRequest:request withCompletionHandler:nil];
}
}
@end

2 个答案:

答案 0 :(得分:1)

正如你所说:

  

声音文件的长度必须少于30秒。如果是声音文件   超过30秒,系统会播放默认声音。

我鼓励您考虑到这一点,因为即使您设法欺骗并解决问题,Apple也会拒绝您的应用。

除此之外,5分钟不是声音,它是音乐,所以要在应用程序处于后台时播放应用程序,您必须注册为音频应用程序,并在收到通知后,使用AVAudioPlayer播放您的文件。

http://www.sagorin.org/ios-playing-audio-in-background-audio/

答案 1 :(得分:0)

我会试一试,但我无法测试这段代码,但我会粗略地解释一下我将如何做这个...(不知道这是否会起作用) )

我会创建一个像这样的函数:

-(void)playsound {
    [[NSSound soundNamed:@"alarm_clock_2015"] play];
}

并从notifyButton IBAction

调用该功能
- (IBAction)notifyButton:(id)sender {


 if (isGrantedNotificationAccess) {
    NSLog(@"clicked");

    [self playsound];

    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
//Mute the sound of this notification
    content.title = @"Notification Title";
    content.subtitle = @"Notification Subtitle";
    content.body = @"Notification body";
    content.sound = [UNNotificationSound soundNamed:@"alarm_clock_2015.aiff"];
    //content.timeDuration = 300;


UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:60 repeats:YES];

UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"UYLocalNotification" content:content trigger:trigger];

[center addNotificationRequest:request withCompletionHandler:nil];
}
}