0

I have one music player with multiple sounds using picker view. My problem is when i click the next music, the previous one will overlap with the one that i've selected.Meaning when i scroll the pickerview to select a new object, it will play a new music/sound but the previous object will overlap the current selection. I want to stop the previous music so that it won't overlap. Here is the code.

H File :

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

@interface PickerViewController : 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 File :

     - (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


    {



        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];
 [theAudio setCurrentTime:0.0]; (our friend from this forum have suggested this but still doens't work)

            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];
            [theAudio setCurrentTime:0.0]; (our friend from this forum have suggested this but still doens't work)



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


        }

Code amendment :

 NSString *path = [[NSBundle mainBundle] pathForResource:@"you" ofType:@"mp3"];
        NSURL *file = [[NSURL alloc] initFileURLWithPath:path];

        AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:file error:NULL];

        [file release];

        self.audioPlayer = theAudio;
        [theAudio release];

        [audioPlayer prepareToPlay];
        [audioPlayer setDelegate:self];
        [audioPlayer setNumberOfLoops:0];
        [audioPlayer stop];

my silly mistake :

@property (nonatomic, retain) AVAudioPlayer *audioPlayer;
Amink
  • 35
  • 11

1 Answers1

1

Just stop the currently playing sound before starting another one, your AVAudioPlayer is an ivar so you could do it. Add

[theAudio stop];
ferostar
  • 7,074
  • 7
  • 38
  • 61
  • I had tried [theAudio stop]; to put inside the if statement but still doesn't work. How do i construct it ? – Amink Jan 04 '12 at 12:35
  • Have you set the player's delegate? This might help you http://stackoverflow.com/questions/2586284/play-multiple-audio-files-using-avaudioplayer – ferostar Jan 04 '12 at 12:43
  • That's a great help from the link. I need your favor because im fairly newbie. Its almost done. But I've done this self.theAudio = newAudio; it says property (theAudio) not found on object of type. How do i declare theAudio? I delcare this way AVAudioPlayer *theAudio; or else – Amink Jan 04 '12 at 14:01
  • No problem! Change AVAudioPlayer *theAudio for self.theAudio. You are currently creating another variable, local, that "hides" your ivar. I noticed it the first time but forgot to tell you about it. – ferostar Jan 04 '12 at 15:33
  • I have done some code amendment up there. And i don't quite understand what do you mean by Change AVAudioPlayer *theAudio for self.theAudio – Amink Jan 04 '12 at 16:06
  • I mean exactly that! In the line of code you create the player, you are currently creating a new local variable named theAudio, which happens to be the same name of your ivar, but they are two different things. You need to assign the AVAudioPlayer you are creating by [[AVAudioPlayer alloc] init...]; not to a new variable (AVAudioPlayer *theAudio) but to your ivar. That's why you need to do self.theAudio. Got it? :) – ferostar Jan 04 '12 at 16:14
  • Now, I've got it. But error remains the same :( I have amended the code above. – Amink Jan 04 '12 at 16:44
  • A couple of things: change the AVAudioPlayer local variable name, it's really confusing doing self.theAudio = theAudio; [theAudio release];. What are you releasing? In two weeks you won't even know. And where are you stoping playback? – ferostar Jan 04 '12 at 17:03
  • Hi ferostar. I thank you very much for helping me so far. I've managed to solve the main problem. Its my silly mistake as a beginner. See my code amendment – Amink Jan 04 '12 at 17:34
  • Now, i need to figure out how to attach that music file via email inside picker view. Any link for help ? – Amink Jan 06 '12 at 07:54