2009-11-03 37 views
7

Xin chào, tôi cần phát một bài hát từ thư viện itunes. Tôi đã đi qua Apples ipod Thư viện Hướng dẫn truy cập và nhận được mã.Cách phát một bài hát từ thư viện itunes trong iphone

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]; 

Nhưng điều này sẽ bắt đầu phát ngay từ đầu danh sách thư viện. Tôi cần phát một bài hát khi tôi chọn nó từ danh sách. Bất cứ ai có thể giúp tôi xin vui lòng ...

Cảm ơn, Shibin.

Trả lời

6

Sử dụng ví dụ MPMediaPickerController bạn có thể chọn từ danh sách bài hát của thư viện iPod, danh sách album, v.v. Đây là một ví dụ chọn tất cả các bài hát từ iPod và hiển thị trong bộ điều khiển chế độ xem.

- (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]; 
} 

Bây giờ bạn cần triển khai đại biểu lưu trữ bài hát vào biến cục bộ của bạn. Ở đây, selectedSongCollection là một phiên bản của MPMediaItemCollection.

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

Sau khi bạn đang thực hiện với việc lựa chọn bài hát, thực hiện các đại biểu để loại bỏ bộ chọn:

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

Tôi xin lỗi. Nhưng những gì được chọnSongCollection ?? Và loại của nó là gì ??? – crazyoxygen

+0

@crazyoxygen: Như tôi đã đề cập trong câu trả lời, chọnSongCollection là một thể hiện của MPMediaItemCollection. –

+0

Tôi có thể kết hợp điều này với âm thanh thông báo cục bộ không? – crazyoxygen

2

Bạn đang gán danh sách phát tất cả bài hát cho trình phát nhạc, vì vậy tất nhiên nó sẽ phát toàn bộ danh sách, bắt đầu từ đầu. Nếu bạn muốn người dùng chọn một bài hát cụ thể từ thư viện iPod, hãy sử dụng MPMediaPickerController.

2

tôi không thể sử dụng MPMediaPickerController trong kịch bản của tôi.

trả lời ngắn gọn của tôi cho câu hỏi là phải có một cái nhìn tại [musicplayer setNowPlayingItem:item]

đây là một số mã dưới đây từ thực hiện của tôi.

// Create a new query 
MPMediaQuery *query = [MPMediaQuery songsQuery]; 
MPMediaPropertyPredicate *mpp = [MPMediaPropertyPredicate predicateWithValue:@"a" forProperty:MPMediaItemPropertyTitle comparisonType:MPMediaPredicateComparisonContains]; 
[query addFilterPredicate:mpp]; 

// Retrieve the results and reload the table data 
DATAENV.songCollections = [NSMutableArray arrayWithArray:query.collections]; 

//populate cell rows with 

- (UITableViewCell *)tableView:(UITableView *)tView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    MPMediaItem *item = [[[DATAENV.songCollections objectAtIndex:indexPath.row] items] lastObject]; 
    titleLbl = [item valueForProperty:MPMediaItemPropertyTitle]; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    MPMediaItem *item = [[[self.songCollections objectAtIndex:indexPath.row] items] lastObject]; 
    [PLAYER setNowPlayingItem:item]; 
    [PLAYER play]; 
} 

đâu PLAYER/DATAENV là độc thân của tôi

#define PLAYER [[AudioController sharedAudioController_instance] musicPlayer] 
#define DATAENV [DataEnvironment sharedDataEnvironment_instance] 
Các vấn đề liên quan