audioPlayerDidFinishPlaying永远不会触发 [英] audioPlayerDidFinishPlaying never triggers

查看:230
本文介绍了audioPlayerDidFinishPlaying永远不会触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I created a custom class to handle audio playback.

AudioPlayback.h

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中

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?

推荐答案

我认为 player.delegate = self 打电话太早了。您在 PlaySoundFile 中再次 initWithContentsOfURL ,我认为这会导致问题。

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

尝试在 [player prepareToPlay] player.delegate = self $ c>在你的 PlaySoundFile 方法

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

这篇关于audioPlayerDidFinishPlaying永远不会触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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