我在设置currentTime属性时遇到了使用AVAudioPlayer循环单个音轨的问题。
如果我不对currentTime属性进行任何操作,则循环是正确的,并且它播放音频文件的次数与numberOfLoops中给出的次数相同,但是如果我对currentTime进行操作通过递减1设置音频文件的持续时间音频文件始终开始偶数numberOfLoops完成。
请检查以下代码。
@interface AVAudioPlayerInterface ()<AVAudioPlayerDelegate>
@property (strong,nonatomic) AVAudioPlayer *musicPlayer;
@end
@implementation AVAudioPlayerInterface
-(instancetype)init
{
self = [super init];
if(self)
{
[self downloadAudioContent];
}
return self;
}
-(void) downloadAudioContent
{
NSString *musicFilePath = [[NSBundle mainBundle] pathForResource:@"aleale" ofType:@"mp3"];
NSURL *completeUrl = [NSURL fileURLWithPath:musicFilePath];
NSError *err=nil;
self.musicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:completeUrl fileTypeHint:AVFileTypeMPEGLayer3 error:&err];
}
-(void)playAtSpecifiedTime:(NSUInteger)pointOfPlayTime
{
self.musicPlayer.currentTime = pointOfPlayTime;
}
-(void)play
{
[self.musicPlayer play];
}
-(void)playLoop:(NSUInteger)numberOfTimes
{
NSLog(@"Number of times to loop %lu",numberOfTimes);
self.musicPlayer.numberOfLoops=numberOfTimes;
}
@end
@interface ViewController ()
@property ( strong, nonatomic) AVAudioPlayerInterface *audioPlayer;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.audioPlayer = [[AVAudioPlayerInterface alloc]init];
self.currentVolume = 0.5;
NSLog(@"View loaded");
// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)playSong:(id)sender {
// Following is sequense i am executing
[self.audioRecord play];
[self.audioRecord playLoop:1];
[self.audioRecord playAtSpecifiedTime:200];//my audio file is 201 seconds so ending-1
[self.audioRecord playAtSpecifiedTime:200];
//At this point the audio file is still playing irrespective of number of calls on [self.audioRecord playAtSpecifiedTime:200];
}
我该如何解决这个问题?