Pregunta

Hola necesito para reproducir una canción de la biblioteca de iTunes. Yo había pasado por las manzanas Manual de acceso a la biblioteca del iPod y ya dispone del código.

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

Pero esto va a empezar a jugar desde el principio de la lista de bibliotecas. Necesito jugar una canción cuando seleccione de la lista. ¿Puede alguien ayudarme por favor ...

Gracias, Shibin.

¿Fue útil?

Solución

El uso de la instancia MPMediaPickerController se puede elegir entre la lista de canciones de la biblioteca del iPod, la lista de álbumes, etc .. Aquí hay un ejemplo que selecciona todas las canciones desde el iPod y muestra en un controlador de vista modal.

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

A continuación, debe aplicar el delegado para almacenar la canción en su variable local. Aquí, selectedSongCollection es una instancia de MPMediaItemCollection.

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

Una vez que haya terminado con la selección de la canción, poner en práctica el delegado para despedir el selector:

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

Otros consejos

Se le asigna una lista de reproducción de todas las canciones en el reproductor de música, así que por supuesto que va a jugar toda la lista, empezando por el principio. Si desea que el usuario seleccionar una canción específica de la biblioteca del iPod, utilice MPMediaPickerController.

No podía utilizar theMPMediaPickerController en mi escenario.

Mi respuesta corta a la pregunta es tener un vistazo a [musicplayer setNowPlayingItem:item]

Aquí hay un código de abajo de mi aplicación.

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

donde el jugador / DATAENV son mis únicos

#define PLAYER  [[AudioController sharedAudioController_instance] musicPlayer]
#define DATAENV [DataEnvironment sharedDataEnvironment_instance]
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top