【问题标题】:audioPlayerDidFinishPlaying never triggersaudioPlayerDidFinishPlaying 从不触发
【发布时间】:2011-04-17 22:54:43
【问题描述】:

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

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

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

【问题讨论】:

    标签: iphone ios avaudioplayer


    【解决方案1】:

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

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

    【讨论】:

    • 阿克冲是正确的。您正在重新初始化您的播放器,但没有重新设置委托。
    • 是的。就是这样。就是看不出来。谢谢!
    • 干得好!更改播放器的内容后,我遇到了同样的问题。谢谢!
    • 谢谢你,你是最棒的人之一 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-17
    • 2018-08-31
    • 1970-01-01
    • 2018-12-18
    • 2015-10-16
    • 2019-11-11
    相关资源
    最近更新 更多