如何在我的iPhone应用程序中访问iPod库

时间:2011-01-24 13:13:48

标签: iphone xcode ipod

如何在我的iPhone应用程序中访问iPod库,比如玩游戏时的用户listem音乐...就像在gameloft游戏中,或者来自Photos.app的幻灯片?

2 个答案:

答案 0 :(得分:0)

看看MPMusicPlayerController。我读过它可以访问iPod库。我从未使用它,我不知道它是否可以帮助你...

答案 1 :(得分:0)

- (void)addMusicBtnAction{
    MPMediaPickerController *mediaPicker = [[MPMediaPickerController alloc] initWithMediaTypes: MPMediaTypeAnyAudio];
    mediaPicker.delegate = self;
    //mediaPicker.prompt = @"Select Audio";
    mediaPicker.prompt = NSLocalizedString (@"Select any song from the list", @"Prompt to user to choose some songs to play");    
    for (UIWindow* window in [UIApplication sharedApplication].windows) {
        NSArray* subviews = window.subviews;
        if ([subviews count] > 0)
            for (UIAlertView *alrt in subviews) {
                if ([alrt isKindOfClass:[UIAlertView class]]){
                    if (alrt.tag == 10020) {
                        [alrt dismissWithClickedButtonIndex:0 animated:YES];
                    }
                }
            }
    }    
    [self presentModalViewController:mediaPicker animated:YES];
    //[mediaPicker release];
}
- (void) mediaPicker: (MPMediaPickerController *) mediaPicker didPickMediaItems: (MPMediaItemCollection *) mediaItemCollection
{
    NSArray * SelectedSong = [mediaItemCollection items];
    if([SelectedSong count]>0){
        MPMediaItem * SongItem = [SelectedSong objectAtIndex:0];
        NSURL *SongURL = [SongItem valueForProperty: MPMediaItemPropertyAssetURL];

        NSString *str = [NSString stringWithFormat:@"%@",SongURL];
        appDelegate.musicFilePath = str;
                //NSLog(@"Audio Loaded");
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Success!" message:@"Your audio has been selected"  delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil, nil];
        alert.tag = 78787878;
        [alert show];
       // [alert release];
    }

    [self dismissModalViewControllerAnimated: YES];   
}
// Responds to the user tapping done having chosen no music.
- (void) mediaPickerDidCancel: (MPMediaPickerController *) mediaPicker {

    [self dismissModalViewControllerAnimated: YES];

    [[UIApplication sharedApplication] setStatusBarStyle: UIStatusBarStyleBlackOpaque animated:YES];
}