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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:24:50+00:00 2026-06-14T00:24:50+00:00

My goal is to store the reference of an audioPlayer instance in core data

  • 0

My goal is to store the reference of an audioPlayer instance in core data in that way, that i can use properties on that entry later. like this:

-(void)didTapButton
{
    if (!self.sound.audioPlayer.playing)
    {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"playSound" object:self.sound];
        self.soundProgress.hidden = NO;
    } else {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"stopSound" object:self.sound];
        self.soundProgress.hidden = YES;
    }
}

My MOS looks like this

@interface MCSound : NSManagedObject

@property (nonatomic, retain) AVAudioPlayer *audioPlayer;
@property (nonatomic, retain) NSString * category;
@property (nonatomic, retain) NSString * duration;
@property (nonatomic, retain) NSString * fileURL;
@property (nonatomic, retain) NSString * id;
@property (nonatomic, retain) NSNumber * isLooping;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSString * theme;
@property (nonatomic, retain) MCTheme *ofTheme;

@end

That works so far. But if i’d generate again the MOSs the AVAudioPlayer Class would be replaced by id. Is there a way how i can do that with categories? Would i have to use after that of the category the category for my sound instances?

EDIT

I need a reference to AVAudioPlayer while the app is running as i’m switching views a lot and the AVAudioPlayer are initialized one gridView. Structure is:

gridView -> detailView -> soundButton

Buttons send a notification to gridView where the AVAudioPlayer is instatiated. When i come back to the detailView i have to show if a sound is still playing and the playingTime must be shown on a progressBar. I had that all using a NSMutableDisctionary but now switching to a core data model cause i have to prefill the data base with default sound sets.

And therefore i thought its the best way to plase the reference to the AVAudioPlayer in the sound records. Because need the reference of the audioPlayer when i come back to the detailView. I have to know if it is still playing and the playingTime.

How would you solve that?

  • 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-06-14T00:24:51+00:00Added an answer on June 14, 2026 at 12:24 am

    AVAudioPlayer should not be added to a persistant store in core-data since it does not conform to the NSCoding protocol.

    What I would do is remove the AVAudioPlayer from your model but keep it in your NSManagedObject. Then in the implementation switch @dynamic to @synthesized for the audioPlayer. (Add a line too to release the audio player on dealloc).

    Then do something like this

    -(AVAudioPlayer*)audioPlayer {
    if (self.audioPlayer) return audioPlayer;
    else {
    NSError * error = nil;
    AVAudioPlayer * lAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:self.fileURL error:&error];
    
    if (!error) {
    self.audioPlayer = lAudioPlayer;
    [lAudioPlayer release];
    } else {
    [lAudioPlayer release];
    NSLog(@"%@",error);
    }
    
    }
    
    }
    

    I would also recommend to something similar with isLooping and duration and have them come off the audioPlayer directly and not store them in your persistant store unless you need them to persist.

    EDIT

    Sounds like your problem is that you are referencing the avaudioplayer in your object (for persistance sake) then saving the context then later when you come back you are rerequesting the object from the persistant store, this will not work since your persistant store can not handle avaudioplayer. There are multiple ways to solve this problem.

    The first is to store all your avaudioplayers in a nsmutabledictionary (i would make it static or in a singleton). This nsmutabledictionary would be keyed by a unique identifier. You can either create a unique identifier and store it in your persistent store (with a new attribute on your object) or just use the objectid of the sound object (I believe). At that point do what I said before about making the audioPlayer in your managed object class but instead of creating it there just get it back from the nsmutabledictionary with the unique identifier that was made, if there is none that means you need to make a new audio player and add it to the nsmutabledictionary.

    I hope I am understanding your problem, I think this is the easiest solution and also the most elegant. The other one that is also really easy is that you keep in memory all the sound managed objects that are playing, like that you won’t need to rerequest them from the persistant store and therefor your audioplayer will still be accessible.

    There are really a thousand ways to do this, I am just giving you the 2 I would use, and don’t forget to release your audioplayers and remove them from the mutable dictionary when you are done with them.

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

Sidebar

Related Questions

My goal is store temporary files in the directory returned by Context#getCacheDir(). /data/data/my.app/cache/somedir/foo.json I
My goal is to store some html-formatted data in a variable, then echo it
My goal is to read the data stored in a bio (assuming that it
my goal is to store information about projects within Jackrabbit. Each project can consist
My goal is to create an efficient structure to store the most relevant entries
Goal : I wants when I drag image it become fade so we can
Goal is to make a dialog that appears on menu_key pressed, but it keeps
Goal: Produce an Excel document with information from 3 associated models that is similar
Goal I am building an Eclipse plugin targeting the 3.7 environment and would like
I'd like to create a custom control in javascript. The goal is to create

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.