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

发布于 2024-11-27 00:44:48 字数 1958 浏览 0 评论 0原文

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

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

I try to create alarm app but I don't know how to set song from iTunes to sound of local notification.

Now I use this code to call 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]; 
}

and something about local notification look like this

 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;

So please guide me how can I set song to local sound ??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

对不⑦ 2024-12-04 00:44:48

您只能使用主包中的声音,这意味着,当提交到应用程序商店时,它们已经存在于应用程序的构建中。

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

You can only use sounds that are a part of the main bundle, meaning, they have be in the build of the app when submitted to the app store.

Yes, you can record sound, download sound, etc in app, but none of those sounds files created/saved can be used, because they are not in the app's bundle. If an app is using custom sounds by accessing them outside of the bundle, then they are using private APIs to do so. Trust me, I've tried every option I can think of.

慵挽 2024-12-04 00:44:48

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

@r3dsm0k3 在他的评论中询问像 Rise 这样的应用程序如何触发应用程序中没有的声音的播放捆。 如果我不得不猜测,我会说 Rise 将自己注册为需要 audio 背景的应用程序模式

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

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

音频 - 应用程序在用户处于
背景。 (此内容包括流媒体音频或视频内容
使用 AirPlay。)

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

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

As noted by @thephatp, a notification (local or remote) can only trigger playback of sounds that are in the app bundle. I see no way around this.

@r3dsm0k3 asks in his comment how apps like Rise trigger playback of sounds that aren't in the app bundle. If I had to guess, I would say that Rise registers itself as an app requiring the audio background mode:

Declaring Your App’s Supported Background Tasks

Support for some types of background execution must be declared in
advance by the app that uses them. An app declares support for a
service using its Info.plist file. Add the UIBackgroundModes key to
your Info.plist file and set its value to an array containing one or
more of the following strings:

audio—The app plays audible content to the user while in the
background. (This content includes streaming audio or video content
using AirPlay.)

This effectively means that Rise is allowed to stay running all the time. It's allowed to do so because it plays audio on behalf of the user. That it doesn't play audio 100% of the time doesn't appear to be a problem for Apple.

Rise may or may not use UILocalNotifications. Most likely they only use these as a backup incase the app DOES get unloaded, and instead use another timer mechanism to trigger the wakeup alarm sequence.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文