2012-01-05 1 views
-1

私は最初のカスタムクラス "AudioPlayer"を作成しようとしました。私はテーブルビューから "AudioPlayer"にデータ(オーディオタイトル)を渡し、initWithContentsOfURL:[NSURL fileURLWithPath:path]でAudioPlayerをロードしたいのですが、私の "MainViewController"に "AudioPlayer" "互換性のないポインタ型は、 'Audio Player * ___ strong'とAVAudioPLayer *型の式を初期化します。私の質問は、どのように私はカスタムオーディオプレーヤーをURLとして選択したオーディオのタイトルで初期化するのですか? AVAudioPlayerの変数を作成したときはうまく動作しますが、カスタムクラスに渡す方法はわかりません。ここでカスタムNSObjectにデータを渡しますか?

は...ここ

#import "AudioPlayer.h" 

@implementation AudioPlayer 

@synthesize audioPlayer; 

-(void)playPause{ 

    if ([audioPlayer isPlaying]) { 

     [audioPlayer pause]; 

    } else { 

     [audioPlayer play]; 

    } 
} 

-(void)volumeSlider 
{ 
    //Setup the volume slider 
    volumeTimer = [NSTimer scheduledTimerWithTimeInterval:0 target:self selector:@selector(updateVolumeSlider) userInfo:nil repeats:YES]; 
    [audioPlayer setVolume:volumeSlider.value]; 
} 
@end 

がMAINVIEW実装です...ここで

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

@interface AudioPlayer : NSObject{ 

    AVAudioPlayer *audioPlayer; 

    //Volume slider 
    NSTimer *volumeTimer; 
    IBOutlet UISlider *volumeSlider; 
} 

-(bool) isPlaying; 
-(bool) isPaused; 

-(void)playPause:(id)sender; 

@property (nonatomic, retain) AVAudioPlayer *audioPlayer; 

@end 

は私のカスタムクラスの実装ファイル..です私のカスタムクラスのヘッダである

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    //Instantiate performanceArray 
    performanceArray = [[NSMutableArray alloc]initWithObjects:@"Centering", nil]; 

    //Instantiate recoveryArray 
    recoveryArray = [[NSMutableArray alloc]initWithObjects:@"Power Nap", nil]; 

    //Instantiate the AudioPlayer 
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:self.tableView.indexPathForSelectedRow]; 
    NSString *path = [[NSBundle mainBundle] pathForResource:cell.textLabel.text ofType:@"m4a"]; 
    AudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];  
    [audioPlayer.audioPlayer prepareToPlay]; 
} 

答えて

0

あなたが作成したAudioPlayerオブジェクトはAVAudioPlayerタイプではありませんので、私は次の行を考えます: AudioPlayer * audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path]エラー:NULL];

は次のようになります。AudioPlayer * audioPlayer = [[AudioPlayer alloc] init];

カスタムクラスのオブジェクトを作成します。カスタムクラスのinitメソッドにparamsを追加する、オブジェクトを作成した直後にカスタムクラスに設定する別のプロパティを作成するなど、NSURL情報を渡すためのオプションが少なくとも2つあります。

新しい私のURL属性追加できAudioPlayerカスタムクラスで

:あなたのMAINVIEWで

-(id)initWithURL:(NSURL)theURL 
{ 
    self = [super init]; 
    if (self) { 
     // perform initialization of object here 
     myURL = theURL; 
    } 
    return self; 
} 

そして:

@interface AudioPlayer : NSObject{ 

    AVAudioPlayer *audioPlayer; 

    //Volume slider 
    NSTimer *volumeTimer; 
    IBOutlet UISlider *volumeSlider; 
    NSURL* myURL; 
} 

を次にAudioPlayerための実装ファイルに次のinitメソッドを追加

NSURL* theURL = [NSURL fileURLWithPath:path]; 
AudioPlayer *audioPlayer = [[AudioPlayer alloc] initWithURL:theURL]; 
+0

ありがとうございます。あなたは私にURLを渡す簡単な例を教えてください。 – arynhard

+0

ありがとうございます。 – arynhard

+0

** NSURL * theURL = [NSURL fileURLWithPath:path]エラー:NULL]; **でセレクタのインスタンスメソッドがわからないエラーが発生しました**インスタンスメッセージのReceiver type 'AudioPlayer'はメソッドを宣言していませんセレクタ 'initWitURL:' on ** AudioPlayer * audioPlayer = [[AudioPlayer alloc] initWitURL:theURL]; ** – arynhard

0

私はあなたが共同していると思いますカスタムクラスをAVAudioPlayerクラスでnfusingします。 AVAudioPlayerのデフォルトクラスにメソッドやオプションを追加する場合は、NSObjectではなくAVAudioPlayerをサブクラス化する必要があります。 deanandreakisが答えとして

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

@interface AudioPlayer : AVAudioPlayer 
// the rest of your code 

は、その後、あなたはあなたのクラスではなく、AVAudioPlayerのinit/ALLOCする必要があります。

意味:

AudioPlayer *audioPlayer = [[AudioPlayer alloc] init]; 

それとも、AVAudioPlayerに追加し、カスタムメソッドを追加するcategoriesを使用することができます。

+0

申し訳ありません – arynhard

+0

カスタムクラスで選択したオーディオを再生する方法を教えてください。私はavaudioplayer claでそれを行う方法を知っています私自身ではありません – arynhard

+0

@interface AudioPlayer:AVAudioPlayerの場合、クラスは親クラスのすべてのプロパティとメソッド、つまりAVAudioPlayerを継承します。だから、あなたが何もしなくても、あなた自身のクラスをインポートすることによって、あなたはいつものように再生/一時停止することができます。ここではいくつかの資料を読んでいます:http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Introduction/introObjectiveC.html – Canopus

関連する問題