2013-02-28 22 views

回答

1
#import <Foundation/Foundation.h> 
    #import <AVFoundation/AVFoundation.h> 

    @interface MultiAVPlay : NSObject <AVAudioPlayerDelegate> { 
     AVAudioPlayer* myplayer; 
     NSArray* fileNames; 
     int ind; 
    } 

    @property (nonatomic, retain) AVAudioPlayer* myplayer; 
    @property (nonatomic, retain) NSArray* fileNames; 

    - (id)initWithFileNameQueue:(NSArray*)names; 
    - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag; 
    - (void)play:(int)i; 
    - (void)stop; 

    @end 


#import "MultiAVPlay.h" 
@implementation MultiAVPlay 
@synthesize myplayer, fileNames; 

- (id)initWithFileNameQueue:(NSArray*)files { 
    if ((self = [super init])) { 
     self.fileNames = files; 
     index = 0; 
     [self play:index]; 
    } 
    return self; 
} 

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag { 
    if (index < fileNames.count) { 
     [self playMp3:index]; 
    } else { 
     //reached end of queue 
    } 
} 

- (void)playMp3:(int)i { 
    self.myplayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:[fileNames objectAtIndex:i] ofType:nil]] error:nil]; 
    [myplayer release]; 
    myplayer.delegate = self; 
    [myplayer prepareToPlay]; 
    [myplayer play];  
    index++; 
} 

- (void)stop { 
    if (self.myplayer.playing) [myplayer stop]; 
} 

- (void)dealloc { 
    self.filenames = nil; 
    self.myplayer = nil;   
    [super dealloc]; 
} 

@end