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

7

我尝试创建一个闹钟应用程序,但是我不知道如何将来自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;

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

请指导我。还是它不能做到吗? - crazyoxygen
请使用此URL检查:http://www.techotopia.com/index.php/Scheduling_iOS_4_iPhone_Local_Notifications#Adding_a_Sound_File_to_the_Project - Naga Harish M
2个回答

8

您只能使用主要捆绑包中的声音,也就是说,它们必须在提交到应用商店的应用程序构建中。

是的,在应用程序中可以录制声音、下载声音等,但不能使用任何已创建/保存的声音文件,因为它们不在应用程序的捆绑包中。如果一个应用程序通过访问捆绑包外部的声音来使用自定义声音,则它们正在使用私有API来这样做。相信我,我尝试了我能想到的每个选项。


我见过一些应用程序做这件事情。例如,Rise。你知道他们是怎么做到的吗?看起来他们进行了一些声音处理,但我不确定。 - Ajith
我不确定他们是如何做到的。目前的UILocalNotification类参考本地和推送通知编程指南仍然说你只能使用主Bundle中的声音。所以,除非他们已经找到了一种将声音压缩在主Bundle中的方法,否则我毫无头绪。:( - thephatp

2
正如@thephatp所指出的那样,通知(本地或远程)只能触发播放应用程序包中存在的声音。我看不到任何绕过这个限制的方法。
@r3dsm0k3在他的评论中问道,像Rise这样的应用程序如何触发播放不在应用程序包中的声音。如果我必须猜测,我会说Rise将自己注册为需要audio后台模式的应用程序。
声明应用程序支持的后台任务:
某些类型的后台执行必须由使用它们的应用程序事先声明支持。应用程序使用其Info.plist文件声明对服务的支持。将UIBackgroundModes键添加到您的Info.plist文件中,并将其值设置为包含以下一个或多个字符串的数组:
audio-应用程序在后台向用户播放可听内容。 (此内容包括使用AirPlay流式传输音频或视频内容。)
这意味着Rise允许始终保持运行状态。这是允许的,因为它代表用户播放音频。苹果似乎并不关心它并非100%时间都在播放音频。
Rise可能会使用UILocalNotifications,但最有可能只将其用作备份,以防万一应用程序被卸载,而使用另一个计时器机制来触发唤醒闹钟序列。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接