为.mp3文件一起播放两个循环

时间:2013-02-28 09:38:17

标签: iphone ios

有没有办法在iphone sdk中一起播放声音文件的2个循环

谢谢&问候 shweta

1 个答案:

答案 0 :(得分: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