2013-02-05 77 views
2

我想通過使用循環播放多個文件。
我寫的代碼如下..
請幫我。!!
如何在AVAudioPlayer中循環播放多個音頻文件?

soundList = [[NSArray alloc] initWithObjects:@"mySong1.mp3",@"mySong2.mp3",@"mySong3.mp3",@"mySong4.mp3",@"mySong5.mp3",@"mySong6.mp3",@"mySong7.mp3",@"mySong8.mp3",@"mySong9.mp3", nil]; 
for (i=0; i<=([soundList count] - 1);) {     
    while(i<[soundList count]){ 
     NSLog(@"File is : %@",[soundList objectAtIndex:i]); 
     mediaPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:[soundList objectAtIndex:i] ofType:nil]] error:&error]; 
     [mediaPlayer setDelegate:self]; 
     self.lblCurrentSongName.text = [soundList objectAtIndex:i]; 
     [mediaPlayer prepareToPlay]; 
     i++; 
    } 
} 

請給我建議。!!

回答

5

我要給你一個準則做,但要注意它不是測試代碼。你給不用於多種原因工作的代碼,1。你不叫上avaudioplayer 2.邊玩邊循環將執行太快,歌曲將相互重疊或因爲你不存儲參考以往avaudioplayer它可能會造成麻煩(不知道確切)

NSInteger currentSoundsIndex; //Don't forget to set this in viewDidLoad or elsewhere 

//In viewDidLoad add this line 
{ 
... 
currentSoundsIndex = 0; 
... 
} 

-(void) playCurrentSong 
{ 
NSError *error; 
mediaPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:[soundList objectAtIndex:currentSoundsIndex] ofType:nil]] error:&error]; 
if(error !=nil) 
{ 
    NSLog(@"%@",error); 
    //Also possibly increment sound index and move on to next song 
} 
else 
{ 
self.lblCurrentSongName.text = [soundList objectAtIndex:currentSoundsIndex]; 
[mediaPlayer setDelegate:self]; 
[mediaPlayer prepareToPlay]; //This is not always needed, but good to include 
[mediaPlayer play]; 
} 

} 

//This is the delegate method called after the sound finished playing, there are also other methods be sure to implement them for avoiding possible errors 
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag 
{ 
//Increment index but don't go out of bounds 
currentSoundsIndex = ++currentSoundsIndex % [soundList count]; 
[self playCurrentSong]; 
} 
+0

非常感謝你[guenis(http://stackoverflow.com/users/837244/guenis) –

+0

HI [guenis(HTTP:// stackoverflow.com/users/837244/guenis)..我嘗試了它,它是成功的,但它不會自動播放下一首歌曲。我應該爲此採取行動。請幫助我自動播放所有歌曲。再次感謝.. –

+0

我已編輯的代碼,通知[MediaPlayer的setDelegate:自我]現在是裏面playCurrentSong。事實證明,當你分配你需要設置代理再次在vc新avaudioplayer,否則audioPlayerDidFinishPlaying方法開不被調用,它不會自動播放下一首歌曲,我的錯誤 – guenis

0

有可能沒有最優雅的解決方案,但你會發現在這裏有一個共同的想法。這是一個完整的工作代碼,您可以將其複製/粘貼到您的項目中,並且應該可以工作。當然,你必須改變你的文件名。如果您有任何問題,請告訴我。

#import "MainViewController.h" 
#import <AVFoundation/AVFoundation.h> 

@interface MainViewController() <AVAudioPlayerDelegate> 

@property (nonatomic, strong) AVAudioPlayer *audioPlayer; 
@property (nonatomic, strong) NSMutableArray *soundsPathsList; 
@property (nonatomic, assign) NSInteger curSoundIdx; 

@end 


@implementation MainViewController 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
     NSArray *soundList = [[NSArray alloc] initWithObjects:@"myAudioFile 1", @"myAudioFile 2", @"myAudioFile 3", 
           @"myAudioFile 4", @"myAudioFile 5", @"myAudioFile 6", @"myAudioFile 7", 
           @"myAudioFile 8", @"myAudioFile 9", @"myAudioFile 10", nil]; 

     self.soundsPathsList = [NSMutableArray new]; 
     for (NSString *fileName in soundList) { 
      NSString *pathToFile = [[NSBundle mainBundle] pathForResource:fileName ofType:@"mp3"]; 
      NSURL *url = [NSURL fileURLWithPath:pathToFile]; 
      [self.soundsPathsList addObject:url]; 
     } 

     [self playNext]; 
    } 
    return self; 
} 

- (void) playNext { 

    if (self.curSoundIdx >= [self.soundsPathsList count]) { 
     self.curSoundIdx = 0; 
    } 

    // destroy player object 
    [self.audioPlayer stop]; 
    self.audioPlayer = nil; 

    // create new player object 
    self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: 
         [self.soundsPathsList objectAtIndex:self.curSoundIdx] error:nil]; 
    self.audioPlayer.delegate = self; 
    [self.audioPlayer play]; 

    self.curSoundIdx++; 
} 

#pragma mark - AVAudioPlayerDelegate 

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag { 
    [self playNext]; 
} 

@end 
+0

非常感謝你[羅斯特(http://stackoverflow.com/users/706319/rost) –

+0

烏爾歡迎;)) –

0

首先聲明

AVAudioPlayer * audioPlayer;

的NSMutableArray * audioQueue;

,然後用下面的代碼

-(void) addToPlayerQueue: (NSString *) file { 

    if ([audioQueue count] == 0) 
    { 
     [audioQueue addObject:file]; 
     [self playSound: file]; 
    } 
    else 
    { 
     [audioQueue addObject:file]; 
    } 
} 


-(void) playSound : (NSString *) soundFile 
{ 
    NSURL *soundUrl = [NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:soundFile ofType:@"caf"]]; 
    if (soundUrl) { 
     audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil]; 
     [audioPlayer setDelegate:self]; 
     [audioPlayer setVolume: playerVolume]; 
     [audioPlayer play]; 
    } 
} 



#pragma mark - 
#pragma mark AVAudioPlayer Delegate Method 

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag { 
    NSLog(@"Just got done playing %@", [audioQueue objectAtIndex:0]); 
    [audioPlayer release]; 

    [audioQueue removeObjectAtIndex:0]; 

    if ([audioQueue count] > 0) 
    { 
     NSString *file = [audioQueue objectAtIndex:0]; 
     [self playSound:file]; 
    } 
} 
+0

非常感謝你[Bhu_zX](http://stackoverflow.com/users/938288/ bhu-zx) –

+0

歡迎男人!!! :) – BhushanVU

0
simply do it by using recursion .. 

@property (nonatomic, strong) NSArray *arrTrackList; 
@property (nonatomic, strong) NSMutableArray *arrAduioQueue; 
@property (nonatomic, strong) AVAudioPlayer *audioPlayer; 
@property (nonatomic) NSUInteger itemIndex; 

@end 

@implementation PLayMultipleTrackViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    self.arrTrackList = [NSArray arrayWithObjects:@"apple_ring",@"shukriya_pakistan",@"pakistan_anthem", nil]; 
    self.itemIndex = 0; 
} 

- (IBAction)TrackToPlay:(id)sender 
{ 
    self.btnPlayAll.enabled = NO; 
    NSString *trackFile = [self.arrTrackList objectAtIndex:self.itemIndex]; 
    [self AddToPlayList:trackFile]; 
} 

-(void)AddToPlayList:(NSString *)file 
{ 
     [self.arrAduioQueue addObject:file]; 
     [self playTrack:file]; 
} 

-(void)playTrack:(NSString *)soundFile 
{ 
    NSString *soundFilePath =[[NSBundle mainBundle] pathForResource: soundFile ofType: @"m4r"]; 
    NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath]; 
    self.audioPlayer =[[AVAudioPlayer alloc] initWithContentsOfURL: fileURL error: nil]; 
    [self.audioPlayer prepareToPlay]; 
    [self.audioPlayer setDelegate:self]; 
    self.audioPlayer.volume = 1.0; 
    [self.audioPlayer play]; 
} 

-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag 
{ 
    [self.arrAduioQueue removeObjectAtIndex:0]; 
    self.itemIndex++; 
    NSUInteger trackListIndex = [self.arrTrackList count]; 

    if (self.itemIndex < trackListIndex) 
    { 
     NSString *nextTrack = [self.arrTrackList objectAtIndex:self.itemIndex]; 
     [self AddToPlayList:nextTrack]; 
    } 
    else 
    { 
     NSLog(@"PLaylist empty"); 
    } 
} 
@end