Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7013019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:19:55+00:00 2026-05-27T22:19:55+00:00

I have one music player with multiple sounds using picker view. My problem is

  • 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;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T22:19:55+00:00Added an answer on May 27, 2026 at 10:19 pm

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

    [theAudio stop];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have seen that google music is using one image for all the small
I have one website on my server, and my IIS Worker Process is using
Okey, this is my problem. I have one service class where Ive managed to
So I have a controller called Music with one action which is index. On
I am using ubuntu, fluxbox, pcmanfm as filemanager, xmms2 as music player. My goal:
A have a flash widget (a music player) and there are about 10 instances
I have built a music player which loads songs from a database in a
I am making a music player and I have a list with all the
I would like to have a playlist for my own music player in Delphi
I have made a music quiz similar to the one in the iPod, everything

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.