audioPlayerDidFinishPlaying永远不会触发

时间:2011-04-17 22:54:44

标签: iphone ios avaudioplayer

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

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

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

1 个答案:

答案 0 :(得分:34)

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

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

相关问题