gpt4 book ai didi

iphone - audioPlayerDidFinishPlaying 从不触发

转载 作者:可可西里 更新时间:2023-11-01 03:01:59 25 4
gpt4 key购买 nike

我创建了一个自定义类来处理音频播放。

音频播放.h

#import <Foundation/Foundation.h>
#import "AVFoundation/AVAudioPlayer.h"
#import <AVFoundation/AVFoundation.h>

@interface AudioPlayback : NSObject <AVAudioPlayerDelegate> {

AVAudioPlayer *player;

BOOL playing;
NSTimeInterval duration;

}
@property (nonatomic, assign) AVAudioPlayer *player;
@property (readonly) BOOL playing;
@property (readonly) NSTimeInterval duration;

-(NSTimeInterval)GetSoundFileDuration:(NSString *) sound_file_name;
-(void)PlaySoundFile:(NSString *) sound_file_name;
-(void)play;
-(void)stop;

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag;
@end

然后,在AudioPlayback.h中

#import "AudioPlayback.h"

@implementation AudioPlayback

@synthesize player;

-(id)init
{
self = [super init];
if (self != nil)
{
player = [[AVAudioPlayer alloc] init];
[player initWithContentsOfURL:nil error:nil];
player.delegate = self;
}
return self;
}

-(void)PlaySoundFile:(NSString *) sound_file_name
{
NSURL *sound_file = [[NSURL alloc] initFileURLWithPath: [[NSBundle mainBundle] pathForResource:sound_file_name ofType:@"mp3"]];

[player initWithContentsOfURL:sound_file error:nil];
[player prepareToPlay];
[player play];
[sound_file release];
}

...

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
NSLog(@"audioPlayerDidFinishPlaying");
}

回调永远不会触发。我遗漏了什么明显的东西吗?

最佳答案

我认为“player.delegate = self”调用太早了。您在“PlaySoundFile”中再次“initWithContentsOfURL”,我认为这是导致问题的原因。

尝试在“PlaySoundFile”方法中的“[player prepareToPlay]”之前放置“player.delegate = self

关于iphone - audioPlayerDidFinishPlaying 从不触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5697029/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com