2010-10-04 41 views
0

我正在使用AVAudioPlayer来管理一些声音,但isPlaying方法似乎崩溃了。AVAudioPlayer正在播放崩溃的应用程序

当我开始做页:

self.soundClass = [AVAudioPlayer alloc]; 

我怎么打了声:

-(void)playSound:(NSString *)fileName:(NSString *)fileExt { 

    if ([self.soundClass isPlaying]){ 
     [self.soundClass pause]; 
    } 
    else if (newSoundFile == currentSoundFile) { 
     [self.soundClass play]; 
    } 
    else { 
     NSLog(@"PlaySound with two variable passed function"); 
     [[NSBundle mainBundle] pathForResource: fileName ofType:fileExt]], &systemSoundID); 

     [self.soundClass initWithContentsOfURL:[NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource: fileName ofType:fileExt]] error:nil]; 

     [self.soundClass prepareToPlay]; 
     [self.soundClass play]; 
    } 

    self.currentSoundFile = fileName; 

} 

我soundClass是很空的现在:

soundclass.h

#import <UIKit/UIKit.h> 
#import <AudioToolbox/AudioToolbox.h> 
#import<AVFoundation/AVFoundation.h> 


@interface SoundClass : AVAudioPlayer <AVAudioPlayerDelegate> { 

} 

@end 

SoundCl ass.m

#import "SoundClass.h" 

@implementation SoundClass 
-(void)dealloc { 


    [super dealloc]; 
} 

@end 

你在这里看到什么我可能会做错吗?它崩溃在if (isPlaying)

回答

2

您指向在您列出的第一行中为AVAudioPlayer的实例分配的空间,但实际上并未初始化实例。在Cocoa中,“alloc”是99.9%的时间,后跟某种形式的-init(如-initWithContentsOfURL:error:在AVAudioPlayer的情况下)。

此外,“soundClass”是一个实例变量的奇怪名称。您应该了解一个类(对象的蓝图)和类的实例(从蓝图构建的实际对象)之间的区别。对这个概念的深入了解对于你理解Cocoa(以及所有面向对象)的编程至关重要。

+0

嗯,当我开始时,程序还没有被告知要玩什么,我可以初始化什么?或者我应该加载一些东西?如果我改变剪辑,我只是​​重新initWithContentsOfURL? – emachine 2010-10-04 19:47:54

+0

在这种情况下,将其设置为零。这通常是在你的类的init ...方法(“self”指向这种情况的实例的类)中完成的。 – 2010-10-04 19:52:04

+0

so self.soundClass = nil?这不是设置它被删除? – emachine 2010-10-04 20:01:22

相关问题