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

  • Home
  • SEARCH
  • 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

I have created an application in facebook,all is working fine except the publish to
Hello all,I'm a junior on html,I have a question about css position : first
NOTE: Using .NET 2.0, and VS2005 as IDE Hello all, I'm working on logging
In this simplified scenario, I have a model called Post Each post has a
My first post here - it's a great site and I will certainly do
My first post here - it's a great site and I will certainly do
Hello all you helpful folks @ stackoverflow! Best resources for Java GUI's? Looking at
$('div.box').html(hello) puts the word hello inside all my div's of class 'box'. They all
All of my codes fail. They should print he, hello and 5 \n 3,
Hello I have the following error by git-fsck, which cannot be cleaned by git-gc

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.