如何在iphone中播放itunes库中的歌曲 [英] How to play a song from the itunes library in iphone

查看:159
本文介绍了如何在iphone中播放itunes库中的歌曲的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨我需要播放来自itunes库的歌曲。我已经浏览了Apples ipod Library Access Guide并获得了代码。

Hi i need to play a song from the itunes library. I had gone through the Apples ipod Library Access Guide and got the code.

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

//assign a playback queue containing all media items on the device
[myPlayer setQueueWithQuery:everything];//setQueueWithQuery:everything];

//start playing from the begining
[myPlayer play];

但这将从图书馆列表的最开头开始播放。当我从列表中选择它时,我需要播放一首歌。任何人都可以帮助我...

But This will start playing from the very beginning of the library list. I need to play a song when i select it from the list. Can anyone help me please...

谢谢,
Shibin。

Thanks, Shibin.

推荐答案

使用 MPMediaPickerController 实例,你可以从iPod库的歌曲列表,专辑列表等中进行选择。这是一个选择所有歌曲的例子。 iPod并在模态视图控制器中显示。

Using the MPMediaPickerController instance you can choose from the iPod library's song list, album list, etc.. Here is an example which selects all the songs from the iPod and displays in a modal view controller.

- (IBAction) selectSong: (id) sender 
{   
    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];
    [picker release]; 
}

现在你需要实现委托将歌曲存储到你的本地变量中。这里, selectedSongCollection MPMediaItemCollection 的实例。

Now you need to implement the delegate to store the song into your local variable. Here, selectedSongCollection is an instance of MPMediaItemCollection.

- (void) mediaPicker: (MPMediaPickerController *) mediaPicker didPickMediaItems: (MPMediaItemCollection *) mediaItemCollection 
{
    [self dismissModalViewControllerAnimated: YES];
    selectedSongCollection=mediaItemCollection; 
}

完成选择歌曲后,执行委托以解除选择器:

After you are done with selecting the song, implement the delegate to dismiss the picker:

- (void) mediaPickerDidCancel: (MPMediaPickerController *) mediaPicker 
{   
    [self dismissModalViewControllerAnimated: YES]; 
}

这篇关于如何在iphone中播放itunes库中的歌曲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆