Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set sound local notification from song in iTunes?

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 ??

like image 856
crazyoxygen Avatar asked Aug 01 '11 03:08

crazyoxygen


2 Answers

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.

like image 167
thephatp Avatar answered Sep 21 '22 15:09

thephatp


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.

like image 40
TomSwift Avatar answered Sep 21 '22 15:09

TomSwift