简体   繁体   中英

Play two loops for .mp3 files together

Is there any way to play 2 loop for the sound file together in iphone sdk

Thanks & Regards shweta

    #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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM