2009-07-31 6 views
0

私はいくつかの異なるサウンドファイル名を特定の順序で作成する配列を持っています。私は正常に配列を作成しましたが、私はファイルURLとして、または私はどのようにAudioServicesPlaySystemSoundIDに実装する配列から得た値を呼び出すか分からない。配列値を呼び出してサウンドファイルを再生するにはどうすればよいですか?

これは、これまで私が持っているコードです:

 - (void)viewDidLoad { 
      NSArray *sounds = [[NSArray alloc] initWithObjects: @"0.wav", @"1.wav", @"2.wav, nil]; 
      NSUInteger currentSound = 0; 
      soundArray = sounds 
      [super viewDidLoad]; 
     } 

     - (void) playFailSound { 
      currentSound++; 
      if (currentSound >= [sounds count]) { 
       currentSound = 0; 
      } 
      [self playSoundWithFilename:[sounds objectAtIndex:currentSound]]; 
     } 

私も、私が動作するように、このためのヘッダファイルで宣言する必要があるかわからないんだけど、私は、配列の値をどのように保存するのですか?

答えて

0

playFailSoundを呼び出す方法を尋ねていますか?または、ヘッダーファイルにサウンド配列を宣言してインスタンス変数にする方法を尋ねていますか?

最初の問題は、2つの方法で配列に異なる変数名を使用していることです。 viewDidLoadではsoundArrayを使用し、playFailSoundではサウンドを使用しています。あなたのヘッダファイルに

は、インスタンス変数として配列を宣言する必要があります:

#import <UIKit/UIKit.h> 

@interface MyObject : NSObject { 
    NSArray *_sounds; //declare the variables 
    NSInteger _currentSound; //this doesn't need to be unsigned, does it? 

} 

@property(nonatomic, retain) NSArray *sounds; //property 
@property(value) NSInteger currentSound; //property 


//method declarations 
- (void) playFailSound;  
- (void) playSoundWithFilename:(NSString *)fileName; 

@end 

あなたは、私は、変数の名前にアンダースコアを使用わかりますが、ではありませんでしたプロパティ。この方法では、プロパティを使用することを意味するときに間違って変数を使用することはありません。あなたの実装ファイル次が必要になりますで

#import "MyObject.h" 

@implementation MyObject 

//synthesize the getters and setters, tell it what iVar to use 
@synthesize sounds=_sounds, currentSound=_currentSound; 

    - (void)viewDidLoad { 
     NSArray *tempSounds = [[NSArray alloc] initWithObjects: @"0.wav", 
                  @"1.wav", 
                  @"2.wav, nil]; 
     self.currentSound = 0; //use the setter 
     self.sounds = tempSounds; //use the setter 
     [tempSounds release]; //we don't need this anymore, get rid of the memory leak 
     [super viewDidLoad]; 
    } 

    - (void) playFailSound { 
     self.currentSound=self.currentSound++; //use the getters and setters 
     if (self.currentSound >= [self.sounds count]) { 
      self.currentSound = 0; 
     } 
     [self playSoundWithFilename:[self.sounds objectAtIndex:self.currentSound]]; 
    } 

    - (void) playSoundWithFilename:(NSString *)filename { 
     //you fill this in 
    } 
@end 

は、今あなたがする必要があるのはどこかからplayFailSoundを呼び出して、実際に音を果たしている部分を埋めています。

本質的に、2つのメソッド間で渡されない同じ変数を参照するには、インスタンス変数である必要があります。

これはかなり基本的なものなので、私がここで説明していることが分からなければ、Appleの入門資料の一部を再読することをお勧めします。

+1

deallocを忘れないでください。 –

+0

もちろん、ありがとう! –

関連する問題