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 985807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:13:13+00:00 2026-05-16T05:13:13+00:00

Hello all this is my first post here i have been working in an

  • 0

Hello all this is my first post here
i have been working in an iphone application it sound like a small music instrument
i’ve used this code to load my music notes :

// get the path of the sound file
NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"do"
                                                      ofType:@"mp3"];

// create a URL with the given path
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:soundPath];

// initialize the AVAudioPlayer with the sound file
btn_do = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL
                                                   error:nil];
[fileURL release];

and so on for all other notes (re,me,fa,sol,la,ci)

my question :
is this method right because i have to write this code almost 16 times is there any better or stay like I am.
Thanks

  • 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-16T05:13:14+00:00Added an answer on May 16, 2026 at 5:13 am

    In essence the code is right. If you want to set up multiple notes, the easiest way to make this more efficient is to create a method you can run for each note. So, create a new method that takes a string and returns a new AVAudioPlayer item:

    - (AVAudioPlayer *)getNoteFromFilename:(NSString *)name {
      NSString *soundPath = [[NSBundle mainBundle] pathForResource:name
                                                      ofType:@"mp3"];
      NSURL *fileURL = [NSURL fileURLWithPath:soundPath];
      AVAudioPlayer *note = [[[AVAudioPlayer alloc] initWithContentsOfURL:fileURL
                                                      error:nil] autorelease];
      return note;
    }
    

    (Note I have swapped the NSURL method for a convenience, autorelease one which removes the need to separately release it. We are also returning an autoreleased object note, as per convention)

    You can then call this method, as follows:

    btn_do = [self getNoteFromFilename:@"do"];
    btn_re = [self getNoteFromFilename:@"re"];
    

    and so on!…

    Edit: next challenge – try sticking them in an array and so you just have one variable to look after! You could make the index in the array correspond to the ‘tag’ property of each different button that calls a note…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Usless Background Info Hello, all. This is my first post here, but I often
Hello all! This is my first post on stackoverflow. After hours of searching and
This is my first post, so first hallo all. I have a little problem
hello all this seems to be my problem I have a table in mysql
Problem Hello all! I have this code which takes my jpg image loops through
Hello I currently have this code checking if a cookie exists, it works all
Hell All, So I have been facing this behavior for quite some time and
hello all I have a small dialog which I created dynamically, which has a
This is my first post on the forum, hope all of you guys are
Hello everyone this is my first post on stack overflow.com I am trying to

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.