2012-01-03 1 views
0

複数のサウンドを持つ1つの音楽プレーヤーがあります。私の質問は、ピッカービューでオブジェクトの1つを選択すると、自動的に再生されますが、新しいサウンドを選択すると、古いサウンドは停止しません。音は新しい音と重なります。オートストップはどうすればいいですか?ここではトラブルシューティングのための私のコードです:iOS:新しいピッカーを選択したときにPickerViewControllerのサウンドファイルをオートストップする方法

Hファイル:

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

@interface musixGreetingViewController : 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:@"m1",@"m2",@"m3",@"m6",@"m4", @"m5",nil]; 
    } 

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


{ 

    //[self playSelectedMusic:self]; 

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

     NSString *path = [[NSBundle mainBundle] pathForResource:@"m1" ofType:@"mp3"]; 
     AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 

     pickerView.delegate = self; 
     [theAudio play]; 

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


    } 


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

     NSString *path = [[NSBundle mainBundle] pathForResource:@"m2" ofType:@"mp3"]; 
     AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 

     pickerView.delegate = self; 
     [theAudio play]; 



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


    } 

-(IBAction)playSelectedMusic:(id)sender{ 

I want the music play here, how to do so without autoplay the music in the picker view? 

} 

答えて

0

[theAudio setCurrentTime:0.0]; 
+0

はあなたには、いくつかを持ってください。アイディア??? –

+0

[TheAudio setCurrentTime:0.0]; [theAudio play];私はそれをこのように置くが、うまくいかない。 – Amink

+0

ここでは、[theAudio setCurrentTime:0.0]を配置します。 – Amink