2013-08-27 18 views
0

我用AVAudioPlayer播放歌曲。我有一個MPMediaItem數組。我想順序和洗牌按鈕點擊。如何播放序列和shuffeled在iphone sdk

我的代碼是:

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

請幫助。

+0

http://stackoverflow.com/questions/15677897/how-do-i-play-multiple-files-in-avaudioplayer/15678021#15678021 – Balu

回答

0

如果正在播放出現在你的iPod庫,然後您可以利用的MPMusicPlayerController的歌曲,這個類包含一堆的屬性來更改狀態像shuffleModerepeatMode

如果您正在使用AVAudioPlayer添加一個接收器通知AVPlayerItemDidPlayToEndTimeNotification和觀察者查詢裏面的陣列arrAnand獲取隨機mediaItem

希望這有助於你。

編輯:

內部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]; 
} 

注:其中i是任何隨機數(應小於[arrAnand計數])。

+0

先生,我是新的iPhone ..你可以詳細說明嗎? – Jason

+0

'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]; [玩家玩];} - (無效){itemDidFinishPlaying //當AVPlayer播放完畢playerItem 將被稱爲}' – Jason

+0

現在我如何使用代碼replaceCurrentItemWithPlayerItem? – Jason

相關問題