Question

I am trying to play Media Item that selected from UITableView in iOS.

I am using MediaQuery and i loaded songs list from iPod Library in my UITableView.

But i don't know how to play that selected songs from UITableView.

I know really basic simple AudioPlayer with following code.

NSString *soundFilePath = [[NSBundle mainBundle] pathForResource: @"sample" 
                                                              ofType: @"mp3"];
    NSURL *fileURL = [[[NSURL alloc] initFileURLWithPath: soundFilePath] autorelease];
    self.player = [[[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error: nil] 
                   autorelease];

That is used of ContentOfURL that already located in resource.There is no need to retrieve songs from iPod Library.

Here is my CellForRowAtIndexPath Method Codes

MPMediaItemCollection *songs = [self.arrayOfSongs objectAtIndex:indexPath.row];

    cell.textLabel.text = [songs.items[0] valueForProperty:MPMediaItemPropertyTitle];

Now i am trying to play that retrieved songs from iPod Library with MediaQuery and that selected from UITableView

So How can i retrieve the URL and play the song from iPod Library that selected from UITableView? Thanks for your reading.

Was it helpful?

Solution

You need to implement your didSelectRowAtIndexPath like the following code:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   MPMediaItemCollection *songs = [self.arrayOfSongs objectAtIndex:indexPath.row];
   MPMediaItem *item = [songs representativeItem];
   self.player = [[[AVAudioPlayer alloc] initWithContentsOfURL:[item valueForProperty:MPMediaItemPropertyAssetURL] error: nil] 
                   autorelease];

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top