如何在iTunes中设置来自歌曲的声音本地通知?

时间:2011-08-01 03:35:04

标签: objective-c ios ipad itunes uilocalnotification

我尝试创建闹钟应用但我不知道如何将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;

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

2 个答案:

答案 0 :(得分:9)

您只能使用属于主要捆绑包的声音,这意味着,当提交到应用商店时,它们会在应用内置。

是的,您可以在应用中录制声音,下载声音等,但这些声音创建/保存的文件都不能使用,因为它们不在应用程序的捆绑包中。如果一个应用程序通过在捆绑包外部访问它们来使用自定义声音,那么他们正在使用私有API来执行此操作。相信我,我已经尝试了我能想到的每一个选择。

答案 1 :(得分:2)

如@thephatp所述,通知(本地或远程)只能触发应用程序包中声音的播放。我认为没办法解决这个问题。

@ r3dsm0k3在他的评论中询问像Rise这样的应用如何触发不在应用包中的声音播放。 If I had to guess, I would say that Rise registers itself as an app requiring the audio background mode

  

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

     

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

     

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

这实际上意味着Rise可以一直保持运行。允许这样做是因为它代表用户播放音频。它不会在100%的时间播放音频对Apple来说似乎不是问题。

上升可能会也可能不会使用UILocalNotifications。很可能他们只使用这些作为备份,因为应用程序可以卸载,而是使用另一个计时器机制来触发唤醒警报序列。