如何在AVAudioPlayer中循环播放多个音频文件?

编程入门 行业动态 更新时间:2024-10-26 05:28:46
本文介绍了如何在AVAudioPlayer中循环播放多个音频文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想通过循环播放多个文件。 我在下面编写代码.. 请帮助我。!! ..

I want to play multiple files by using loop. I written code below.. Please help me.!!..

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++; } }

请给我建议。!! ..

Please give me suggestion .!!..

推荐答案

我打算给你一个指导方针,但要注意它不是经过测试的代码。您提供的代码不会出于多种原因,1。您不会在avaudioplayer 2上调用play。while循环执行速度太快而且歌曲会相互重叠,或者因为您没有将引用存储到以前的avaudioplayer它可能会造成麻烦(不确切知道)

I'm going to give you a guideline doing it, but beware it is not tested code. The code you gave will not work for a number of reasons, 1. you don't call play on avaudioplayer 2. while loop will execute too fast and songs will overlap with each other or since you don't store the reference to previous avaudioplayer it might create trouble (don't know exactly)

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]; }

更多推荐

如何在AVAudioPlayer中循环播放多个音频文件?

本文发布于:2023-07-11 12:04:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1093991.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   音频文件   如何在   AVAudioPlayer

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!