我如何在iPhone应用程序中播放iPod库中的歌曲?

时间:2022-04-19 07:30:55

I am very new to Audio playing in iPhone and ios development. I want to play songs from iPod library in my app. I have searched my level best in Google. But, i got the answer to use the iPod library but, i don't know to play a selected song from iPod library. This is my code used in app,

我对iPhone和ios开发中的音频播放非常陌生。我想在我的应用里播放iPod库的歌曲。我在谷歌上搜索了我的级别。但是,我得到了使用iPod库的答案,但是,我不知道如何播放从iPod库中挑选出来的歌曲。这是我在app中使用的代码,

 MPMediaPickerController *picker = [[MPMediaPickerController alloc] initWithMediaTypes: MPMediaTypeMusic];

        picker.delegate = self;
        picker.allowsPickingMultipleItems = NO;    
        picker.prompt = NSLocalizedString (@"Select songs to play", "Prompt in media item picker");
        [self presentModalViewController:picker animated:YES];
        [picker release];

       - (void) mediaPicker: (MPMediaPickerController *) mediaPicker didPickMediaItems: (MPMediaItemCollection *) mediaItemCollection 
    {
        [self dismissModalViewControllerAnimated: YES];
        NSLog(@"MediaItemCollection : %@", mediaItemCollection);

        MPMediaQuery *everything = [[MPMediaQuery alloc] init];
        NSLog(@"Logging items from a generic query...");
        NSArray *itemsFromGenericQuery = [everything items];
        for (MPMediaItem *song in itemsFromGenericQuery) 
        {
            NSString *songTitle = [song valueForProperty: MPMediaItemPropertyTitle];
            NSLog (@"%@", songTitle); // Here i retrieved all song titles.
        }
}

Can anyone please suggest me to play the song from the ipod library?

谁能建议我从ipod图书馆放这首歌吗?

1 个解决方案

#1


4  

- (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection
{
    MPMusicPlayerController *player = [MPMusicPlayerController iPodMusicPlayer];
    [player setQueueWithItemCollection:mediaItemCollection];
    [player play];
}

#1


4  

- (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection
{
    MPMusicPlayerController *player = [MPMusicPlayerController iPodMusicPlayer];
    [player setQueueWithItemCollection:mediaItemCollection];
    [player play];
}