AVAudioPlayer - Crash bei Release

  • AVAudioPlayer - Crash bei Release

    Damit es für mich möglichst einfach wird, Sounds wiederzugeben, habe ich eine eigene Klasse erstellt, welche mit dem AVAudioPlayer arbeitet. Diese Klasse funktioniert auch ziemlich gut, nur dass sie einen Absturtz verursacht, sobald sie released wird.
    Kann mir jemand sagen, was ich falsch mache?

    JWSound.h

    Quellcode

    1. #import <Foundation/Foundation.h>
    2. @class AVAudioPlayer;
    3. @interface JWSound : NSObject{
    4. AVAudioPlayer *audioPlayer;
    5. }
    6. - (id)initJWSoundWithFile:(NSString*)file withLoop:(BOOL)loop;
    7. - (void) play;
    8. - (void) stop;
    9. - (void) changePosition:(int)pos;
    10. @end
    Alles anzeigen


    JWSound.m

    Quellcode

    1. #import "JWSound.h"
    2. #import <AVFoundation/AVFoundation.h>
    3. @implementation JWSound
    4. - (id)initJWSoundWithFile:(NSString*)file withLoop:(BOOL)loop {
    5. [super init];
    6. NSString *filePath = [[NSBundle mainBundle] pathForResource:file ofType:@"mp3"];
    7. NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
    8. audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
    9. [audioPlayer prepareToPlay];
    10. [filePath release];
    11. [fileURL release];
    12. if (loop == YES) {
    13. audioPlayer.numberOfLoops = -1;
    14. }
    15. return self;
    16. }
    17. - (void) play {
    18. NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
    19. if ([settings boolForKey:@"sound_setting"] == 1) {
    20. [audioPlayer stop];
    21. audioPlayer.currentTime = 0;
    22. [audioPlayer play];
    23. }
    24. }
    25. - (void) stop {
    26. NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
    27. if ([settings boolForKey:@"sound_setting"] == 1) {
    28. [audioPlayer stop];
    29. }
    30. }
    31. - (void) changePosition:(int)pos {
    32. NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
    33. if ([settings boolForKey:@"sound_setting"] == 1) {
    34. audioPlayer.currentTime = pos;
    35. }
    36. }
    37. - (void)dealloc {
    38. [audioPlayer release];
    39. [super dealloc];
    40. }
    41. @end
    Alles anzeigen
  • Hallo kingjan,

    Quellcode

    1. NSString *filePath = [[NSBundle mainBundle] pathForResource:file ofType:@"mp3"];

    Ich denke mal, pathForResource "bringt"ein Autorelease mit. Womit dann durch:

    Quellcode

    1. [filePath release];

    ... die "Bombe" anfängt zu ticken. Hochgehen tut sie, sobald der Autorelease-Pool dann irgendwann mal freigegeben wird.

    Also, einfach mal das Release auf den filePath weglassen!

    BG, Jörg
  • Hallo

    Ich = Anfänger, trotzdem "denke" ich mal, das mit:

    NSString *filePath = [[NSBundle mainBundle] pathForResource:file ofType:@"mp3"];

    der 'filePath' zu diesem Zeitpunkt "niemandem so richtig zu-gehört".

    Um das zu prüfen könnte man (als einzige Änderung):
    NSString *filePath = [[[NSBundle mainBundle] pathForResource:file ofType:@"mp3"] retain];
    schreiben.

    Grüsse,
    Ch.