如何设置iTunes中歌曲的声音本地通知



我尝试创建闹钟应用程序,但我不知道如何将iTunes中的歌曲设置为本地通知的声音。

现在我用这段代码调用iTunes

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        MPMediaPickerController *picker = [[MPMediaPickerController alloc] initWithMediaTypes: MPMediaTypeMusic];
        picker.delegate = self;
        picker.allowsPickingMultipleItems = NO;
        picker.prompt = NSLocalizedString (@"Select any song from the list", @"Prompt to user to choose some songs to play");
        //[self presentModalViewController: picker animated: YES];
        [self.navigationController pushViewController:picker animated:YES];
        NSLog(@"gsudifghukdsf");
        [picker release];
    }
}
- (void) mediaPicker: (MPMediaPickerController *) mediaPicker didPickMediaItems: (MPMediaItemCollection *) mediaItemCollection 
{
    [self.navigationController popToRootViewControllerAnimated:YES];
    //[self dismissModalViewControllerAnimated: YES];
    NSLog(@"%@",mediaItemCollection);
    UILocalNotification *local = [[UILocalNotification alloc] init];
    //selectedSongCollection=mediaItemCollection; 
}
- (void) mediaPickerDidCancel: (MPMediaPickerController *) mediaPicker
{    
    [self.navigationController popToRootViewControllerAnimated:YES];
    //[self dismissModalViewControllerAnimated: YES]; 
}

和一些关于本地通知的内容像这样

 UILocalNotification *localNotif = [[UILocalNotification alloc] init];
    if (localNotif == nil)
    {   //NSLog(@"Get in if localNotif");
        return;
    }
    localNotif.fireDate = DateAlarm;
    localNotif.timeZone = [NSTimeZone defaultTimeZone];
    // Notification details
    localNotif.alertBody = [NSString stringWithFormat:@"%@",DateAlarm];
    // Set the action button
    localNotif.alertAction = @"Oh Shit";

    localNotif.soundName = UILocalNotificationDefaultSoundName;

那么请指导我如何将歌曲设置为本地声音??

你只能使用主包中的声音,这意味着它们在提交到应用商店时必须包含在应用的构建中。

是的,你可以在应用程序中录制声音,下载声音等,但这些创建/保存的声音文件都不能使用,因为它们不在应用程序的捆绑包中。如果一个应用通过在bundle外部访问自定义声音,那么它就是在使用私有api。相信我,我已经试过了我能想到的所有方法。

正如@thephatp所指出的,通知(本地或远程)只能触发应用包中的声音播放。我觉得没有别的办法。

@r3dsm0k3在他的评论中问道,像Rise这样的应用是如何触发不在应用包中的声音回放的。如果我不得不猜测,我会说Rise将自己注册为需要audio后台模式的应用程序:

声明应用程序支持的后台任务

对某些类型的后台执行的支持必须在通过使用它们的应用程序推进。应用程序声明支持服务使用它的Info。plist文件。添加UIBackgroundModes键到你的信息。列表文件并将其值设置为包含一个或的数组以下更多字符串:

audio -该应用程序向用户播放可听的内容背景。(此内容包括流媒体音频或视频内容使用AirPlay)。

这实际上意味着允许Rise一直保持运行。允许这样做是因为它代表用户播放音频。它不能100%播放音频,这对苹果来说似乎不是问题。

Rise可以使用也可以不使用UILocalNotifications。最有可能的是,他们只使用这些作为备份,以防应用程序确实被卸载,而不是使用另一个计时器机制来触发唤醒警报序列。

最新更新