audioPlayerDidFinishPlaying 永远不会触发

发布于 2024-11-01 18:50:58 字数 1532 浏览 0 评论 0原文

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

AudioPlayback.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");
}

回调永远不会触发。我错过了什么明显的事情吗?

I created a custom class to handle audio playback.

AudioPlayback.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

Then, in 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");
}

The callback never triggers. Am I missing anything obvious?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

那支青花 2024-11-08 18:50:58

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

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

I think "player.delegate = self" is call too early. You "initWithContentsOfURL" again in your "PlaySoundFile", which I think that cause the problem.

Try to put "player.delegate = self" before the "[player prepareToPlay]" in your "PlaySoundFile" method

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文