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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:48:33+00:00 2026-05-29T11:48:33+00:00

Okay, this should be an easy one for the pros out there (disclaimer, I’m

  • 0

Okay, this should be an easy one for the pros out there (disclaimer, I’m sort of a noob in Obj-C, mainly I use in C#). I have a UIViewController subclass; one of its properties is an AVAudioPlayer object. When I write this code:

NSString *path = [[NSBundle mainBundle] pathForResource:@"example" ofType:@"caf"];
NSURL *url = [[NSURL alloc] initFileURLWithPath:path];
[player initWithContentsOfURL:url error:NULL];
[url release];
[player setDelegate:self];
[player prepareToPlay];
[player play];

Nothing happens.

When I do this, which is clearly the correct way of doing it:

NSString *path = [[NSBundle mainBundle] pathForResource:@"example" ofType:@"caf"];
NSURL *url = [[NSURL alloc] initFileURLWithPath:path];
AVAudioPlayer *ap = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
self.player = ap;
[ap release];
[url release];
[player setDelegate:self];
[player prepareToPlay];
[player play];

…everything works beautifully.

Why do I need to allocate an object and then set my property equal to that? Apologies if this is a way-too-common question.

  • 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-29T11:48:34+00:00Added an answer on May 29, 2026 at 11:48 am

    This is because in your first example you are not creating the AVAudioPlayer with alloc. This means you don’t actually have a valid AVAudioPlayer in memory.

    And also you are not using taking the return value of init*. Since init often works on the instance itself, this would probably work in many cases if you had done the alloc and assigned it to player, but you can’t rely on this, and its the convention to use the return value with a Class *var = [[Class alloc] init] form.

    If you wanted shorter working code, you could replace

    [player initWithContentsOfURL:url error:NULL];
    

    with

    self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay this is definitley an easy question and a stupid one but since I
Okay so this query should be easy but I'm having a bit of difficult.
Okay, this should be a fairly easy question I think, but haven't been able
Okay, this should be painfully obvious, however I haven't been able to figure out
Okay, this should be simple, but I just can't get it to work. I
Okay, this should be really simple, but I have searched all over for the
Okay, so this should be a simple question, but I'm fairly new at programming,
Okay, if feel like this should be really simple and accomplished by a function
I've read somewhere that one should use a for loop when you are iterating
Feel like this should be something easy that I'm missing. I have a table

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.