0

pickerViewコントローラーを使用した複数のサウンドアプリを備えた1つの音楽プレーヤーがありますが、アプリを作成しようとすると、サウンドが出ません。誰かが私のコードのトラブルシューティングを手伝ってくれますか?これがトラブルシューティングのための私のコードです。

.Hファイル

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


@interface ViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource, AVAudioPlayerDelegate> {

    UIPickerView       *picker;
    UILabel            *musicTitle;
    NSMutableArray     *musicList;
    AVAudioPlayer      *audioPlayer;


}


@property (nonatomic, retain) IBOutlet UIPickerView *picker;
@property (nonatomic, retain) IBOutlet UILabel *musicTitle;
@property (nonatomic, retain) NSMutableArray *musicList;

-(IBAction)playSelectedMusic:(id)sender;


@end

.Mファイル

     - (void)viewDidLoad
        {
            [super viewDidLoad];

            musicList = [[NSMutableArray alloc] initWithObjects:@"music1",@"music2",@"music3",@"music4",@"music5",
                        @"music6",nil];
        }

    -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
          inComponent:(NSInteger)component
    {



        if ([[musicList objectAtIndex:row] isEqual:@"music1"])
        {


            NSURL *path = [[NSBundle mainBundle] URLForResource:@"music1" withExtension:@"mp3"];
            AVAudioPlayer *theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:path error:NULL];


            theAudio.delegate = self;
            [theAudio play];

            NSString *resultString = [[NSString alloc] initWithFormat:
                                      @"music1",
                                      [musicList objectAtIndex:row]];
            musicTitle.text = resultString;


        }

-(IBAction)playSelectedMusic:(id)sender{

    how do i call the didSelectRow & put it here ?

}
4

1 に答える 1

0
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
      inComponent:(NSInteger)component

pickerview.delegate = self; を設定してみてください。

ピッカービューのデリゲートを設定すると、自動的に呼び出されます。 >playSelected music ボタンを明示的に指定する必要はありません。

于 2012-01-03T10:56:19.590 に答える