0

I am using AVAudioPlayer to play songs. I have an array of MPMediaItem. I want sequential and shuffled on button click.

My code is:

 AVAudioPlayer *player2;
 MPMediaItem *item = [arrAnand objectAtIndex:i];
 NSURL *url = [item valueForProperty:MPMediaItemPropertyAssetURL];
[player2 release];
player2 = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[player2 play];

please help.

Prasad Devadiga
  • 2,573
  • 20
  • 43
Jason
  • 151
  • 1
  • 13
  • http://stackoverflow.com/questions/15677897/how-do-i-play-multiple-files-in-avaudioplayer/15678021#15678021 – Balu Aug 27 '13 at 04:47

1 Answers1

0

If you are playing the songs present in your iPod library then you can make use of MPMusicPlayerController, this class contains bunch of property to change the states like shuffleMode or repeatMode.

If you are using AVAudioPlayer add a receiver for notification AVPlayerItemDidPlayToEndTimeNotification and inside the observer query your array arrAnand to fetch the random mediaItem

Hope this helps you.

Edit :

Inside itemDidFinishPlaying

-(void)itemDidFinishPlaying 
{
    MPMediaItem *item = [arrAnand objectAtIndex:i]; 
    NSURL *url = [item valueForProperty:MPMediaItemPropertyAssetURL];

    if(player2)
    {
          [player2 release];
          player2 = nil;
    }
    player2 = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil]; 
    [player2 play];
}

Note : where i is any random number (should be less than the [arrAnand count]).

Prasad Devadiga
  • 2,573
  • 20
  • 43
  • sir i am new in iPhone..can YOU elaborate? – Jason Aug 27 '13 at 05:16
  • ` MPMediaItem *item = [arrAnand objectAtIndex:i]; NSURL *url = [item valueForProperty:MPMediaItemPropertyAssetURL]; AVPlayerItem* playerItem = [AVPlayerItem playerItemWithURL:url]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidFinishPlaying) name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem]; AVPlayer* player = [[[AVPlayer alloc] initWithPlayerItem:playerItem] autorelease]; [player play];} -(void)itemDidFinishPlaying { // Will be called when AVPlayer finishes playing playerItem }` – Jason Aug 27 '13 at 05:45
  • ok, inside `-(void)itemDidFinishPlaying` get the media item using MPMediaItem *item = [arrAnand objectAtIndex:i]; NSURL *url = [item valueForProperty:MPMediaItemPropertyAssetURL]; where i is any random number. Then play that media item using player2 = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil]; [player2 play]; – Prasad Devadiga Aug 27 '13 at 11:22
  • If you want shuffled then you need to query to your array to fetch the media item at random position else get the media item by just increasing the array index. – Prasad Devadiga Aug 27 '13 at 11:48
  • hey i hav array of media item how can i add into whole array to url? – Jason Aug 27 '13 at 13:52
  • You cant load whole array to URL, you need to fetch each media item randomly from array and take its URL property. – Prasad Devadiga Aug 29 '13 at 05:05
  • thnx sir...nw i have loaded but problem arise-the play button not go to next row when next song is play..help – Jason Aug 29 '13 at 10:05
  • That u need to handle,may be you can have some reference to currently playing item and in the tableview show the play button for that specific row. – Prasad Devadiga Aug 29 '13 at 13:35