Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to play music file after fetching list of songs from Music Library?

In my application i have to make music library. So for that i have to fetch songs from Music Library. I done till this bye following.

Test_MusicLibraryAppDelegate *appDeleg = (Test_MusicLibraryAppDelegate *)[[UIApplication sharedApplication]delegate];

    //NSMutableDictionary *musicList = [NSMutableDictionary dictionary]; 

    MPMediaQuery *query = [[MPMediaQuery alloc] init];
    [query addFilterPredicate:[MPMediaPropertyPredicate
                               predicateWithValue:[NSNumber numberWithInteger:(MPMediaTypeMusic)]
                               forProperty:MPMediaItemPropertyMediaType]];

    for (MPMediaItem* item in [query items]) { 
        [appDeleg.arryMusic addObject:item];
    }

    NSLog(@"Count :: %d ",[appDeleg.arryMusic count]);
    [query release];

than display it in table view.

when i have to play song on selected index i fetch song name but don`t know how to play that song,

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    Test_MusicLibraryAppDelegate *appDeleg = (Test_MusicLibraryAppDelegate *)[[UIApplication sharedApplication]delegate];

//      MPMediaItem *song = [appDeleg.arryMusic objectAtIndex:indexPath.row];

    NSURL *aURL = [appDeleg.arryMusic objectAtIndex:indexPath.row];
    //[song valueForProperty:MPMediaItemPropertyAssetURL];
    [[self avPlayer] replaceCurrentItemWithPlayerItem:
     [AVPlayerItem playerItemWithURL:aURL]];
    [avPlayer play];
}

but my sound file not going to play...

Hey any one know about this than help me..

Thanks AJPatel

like image 722
AJPatel Avatar asked Dec 02 '25 16:12

AJPatel


1 Answers

You are not storing a URL in your array, you are storing instances of MPMediaItem

What you need to do is extract the assets url from the media item. You can do this as follows ...

MPMediaItem *item = [appDeleg.arryMusic objectAtIndex:indexPath.row];
NSURL *url = [item valueForProperty:MPMediaItemPropertyAssetURL];
[[self avPlayer] replaceCurrentItemWithPlayerItem:[AVPlayerItem playerItemWithURL:url]];
[self.avPlayer play];

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!