audioPlayerDidFinishPlaying never triggers

10,698

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

Share:
10,698

Related videos on Youtube

martin's
Author by

martin's

Current Projects Educational iOS apps for kids with disabilities. Web-based business automation tools. Summary of Experience Software Development Assembler, Forth, C, C++, Lisp, APL, VB, VBA, Objective-C, Java, JavaScript, JQuery, PHP, MySQL, HTML5, CSS3. Learning Python. Puzzled about Go. Electronics Design Embedded, RTOS, real-time mission-critical, robotics, automation, real-time image processing, FPGA, Verilog, Signal Integrity, Power Integrity, multi-GHz-multi-clock-domain high-speed design, Impedance controlled designs. Mechanical Design Solidworks, AutoCAD, Plastics, Sheet Metal, Machining, CNC, DFM, FEA, Thermal, Structural, Airflow, optics. Electronics Manufacturing PCB design and layout, Pick-and-Place, SMT, DFM, Optical Inspection, Test fixture design, Manufacturing Engineering Entrepreneurship Three self-funded tech startups covering areas in robotics, real-time image processing in hardware+software (FPGA, embedded and DSP) and software.

Updated on June 04, 2022

Comments

  • martin's
    martin's almost 2 years

    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?

  • Daniel Amitay
    Daniel Amitay about 13 years
    Arkchong is correct. You are re-initializing your player, but not re-setting the delegate.
  • martin's
    martin's about 13 years
    Yup. That was it. Just couldn't see it. Thanks!
  • Tommy Devoy
    Tommy Devoy over 11 years
    nice job! i had that same problem after changing contents of the player. thanks!
  • ondermerol
    ondermerol about 7 years
    Thank you, you are one of the best :)

Related