如何在AVAudioplayer中播放多个文件 [英] How do I play multiple files in AVAudioplayer

查看:39
本文介绍了如何在AVAudioplayer中播放多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在plist中有一些歌曲,并想使用AVAudioplayer依次播放. 但是,当第一首歌曲结束时,它停止了.如何开始播放下一首歌? 我尝试了一个循环,该循环计数到plist中的下一个数字,但它不起作用.玩家在第一回合后停止.这应该很简单,但是如何? 这是我的代码的一部分.可以像我一样使用循环吗?

    NSString *soundsPath = [[NSBundle mainBundle] pathForResource:@"soundslist"  
ofType:@"plist"];
soundsList = [[NSArray alloc] initWithContentsOfFile:soundsPath];


 for (int n = 1; n<=5;n++) {
NSString *filename = [soundsList objectAtIndex:n]; //n is number in plist, counting upwards in a for...loop


NSString *path = [[NSBundle mainBundle] pathForResource:filename ofType:@"au"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: path];

 sound = [[AVAudioPlayer alloc] initWithContentsOfURL : fileURL error:nil];

sound.delegate = self;
 }

解决方案

尝试这样对您有帮助,

-(void)viewDidLoad{

 NSString *soundsPath = [[NSBundle mainBundle] pathForResource:@"soundslist"  
ofType:@"plist"];
soundsList = [[NSArray alloc] initWithContentsOfFile:soundsPath];
[self AddSongToAvplayer];
}

-(void)AddSongToAvplayer{

NSString *filename = [soundsList objectAtIndex:n]; //n is number in plist, counting upwards in a for...loop


NSString *path = [[NSBundle mainBundle] pathForResource:filename ofType:@"au"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: path];

 sound = [[AVAudioPlayer alloc] initWithContentsOfURL : fileURL error:nil];

sound.delegate = self;
n++;


 if(n<=[sound count])
    [NSTimer scheduledTimerWithTimeInterval:sound.duration target:self selector:@selector(AddSongToAvplayer) userInfo:nil repeats:NO];
}

上面的代码sound.duration中的

给出了该特定歌曲的timeInterval,并根据该歌曲的完成时间,可以使用计时器调用该方法.如果最后一首歌曲出现,则停止该NSTimer.

-(IBAction)AddSongToAvplayer{
        if(n<=[sound count]){

    NSString *filename = [soundsList objectAtIndex:n]; //n is number in plist, counting upwards in a for...loop


    NSString *path = [[NSBundle mainBundle] pathForResource:filename ofType:@"au"];
    NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: path];

     sound = [[AVAudioPlayer alloc] initWithContentsOfURL : fileURL error:nil];

    sound.delegate = self;
    n++;

    }

    }

I have some songs in a plist and want to play them one after the other using AVAudioplayer. But when the first song ends it stops. How do I start the player with next song? I have tried a loop which counts to next number in the plist but it does not work. The player stops after first round. This should be simple but how? This is parts of my code. Is it is possible to use a loop as I do?

    NSString *soundsPath = [[NSBundle mainBundle] pathForResource:@"soundslist"  
ofType:@"plist"];
soundsList = [[NSArray alloc] initWithContentsOfFile:soundsPath];


 for (int n = 1; n<=5;n++) {
NSString *filename = [soundsList objectAtIndex:n]; //n is number in plist, counting upwards in a for...loop


NSString *path = [[NSBundle mainBundle] pathForResource:filename ofType:@"au"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: path];

 sound = [[AVAudioPlayer alloc] initWithContentsOfURL : fileURL error:nil];

sound.delegate = self;
 }

解决方案

try like this it'l helps you,

-(void)viewDidLoad{

 NSString *soundsPath = [[NSBundle mainBundle] pathForResource:@"soundslist"  
ofType:@"plist"];
soundsList = [[NSArray alloc] initWithContentsOfFile:soundsPath];
[self AddSongToAvplayer];
}

-(void)AddSongToAvplayer{

NSString *filename = [soundsList objectAtIndex:n]; //n is number in plist, counting upwards in a for...loop


NSString *path = [[NSBundle mainBundle] pathForResource:filename ofType:@"au"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: path];

 sound = [[AVAudioPlayer alloc] initWithContentsOfURL : fileURL error:nil];

sound.delegate = self;
n++;


 if(n<=[sound count])
    [NSTimer scheduledTimerWithTimeInterval:sound.duration target:self selector:@selector(AddSongToAvplayer) userInfo:nil repeats:NO];
}

in the above code sound.duration gives the timeInterval of that particular song and based on that after completion of that song you can call that method by using the timer .if the last song coming then stop that NSTimer.

EDIT:

-(IBAction)AddSongToAvplayer{
        if(n<=[sound count]){

    NSString *filename = [soundsList objectAtIndex:n]; //n is number in plist, counting upwards in a for...loop


    NSString *path = [[NSBundle mainBundle] pathForResource:filename ofType:@"au"];
    NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: path];

     sound = [[AVAudioPlayer alloc] initWithContentsOfURL : fileURL error:nil];

    sound.delegate = self;
    n++;

    }

    }

这篇关于如何在AVAudioplayer中播放多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆