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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:25:13+00:00 2026-05-30T23:25:13+00:00

I am building a MP3 player for iOS that plays audio files hosted on

  • 0

I am building a MP3 player for iOS that plays audio files hosted on the web. I want to offer the ability to play the files offline so I have the files downloading using ASIHTTP but I am cannot seem to find info on initiallzing AVPlayer with a mp3 in the app documents directory. Has anyone done this before? Is it even possible?

*I posted an answer below that shows how to use the iOS AvPlayer for both local and http files. Hope this helps!

  • 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-30T23:25:15+00:00Added an answer on May 30, 2026 at 11:25 pm

    I decided to answer my own question because I felt like there is very little documentation on how to use the Apple provided AVPlayer for both local and stream (over http) files. To help understand the solution, I put together a sample project on GitHub in Objective-C and Swift The code below is Objective-C but you can download my Swift example to see that. It is very similar!

    What I found is that the two ways of setting up the files are almost identical except for how you instantiate your NSURL for the Asset > PlayerItem > AVPlayer chain.

    Here is an outline of the core methods

    .h file (partial code)

    -(IBAction) BtnGoClick:(id)sender;
    -(IBAction) BtnGoLocalClick:(id)sender;
    -(IBAction) BtnPlay:(id)sender;
    -(IBAction) BtnPause:(id)sender;
    -(void) setupAVPlayerForURL: (NSURL*) url;
    

    .m file (partial code)

    -(IBAction) BtnGoClick:(id)sender {
    
        NSURL *url = [[NSURL alloc] initWithString:@""];
    
        [self setupAVPlayerForURL:url];
    }
    
    -(IBAction) BtnGoLocalClick:(id)sender {
    
        // - - - Pull media from documents folder
    
        //NSString* saveFileName = @"MyAudio.mp3";
        //NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        //NSString *documentsDirectory = [paths objectAtIndex:0];
        //NSString *path = [documentsDirectory stringByAppendingPathComponent:saveFileName];
    
        // - - -
    
        // - - - Pull media from resources folder
    
        NSString *path = [[NSBundle mainBundle] pathForResource:@"MyAudio" ofType:@"mp3"];
    
        // - - -
    
        NSURL *url = [[NSURL alloc] initFileURLWithPath: path];
    
        [self setupAVPlayerForURL:url];
    }
    
    -(void) setupAVPlayerForURL: (NSURL*) url {
        AVAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];
        AVPlayerItem *anItem = [AVPlayerItem playerItemWithAsset:asset];
    
        player = [AVPlayer playerWithPlayerItem:anItem];
        [player addObserver:self forKeyPath:@"status" options:0 context:nil];
    }
    
    
    - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    
        if (object == player && [keyPath isEqualToString:@"status"]) {
            if (player.status == AVPlayerStatusFailed) {
                NSLog(@"AVPlayer Failed");
            } else if (player.status == AVPlayerStatusReadyToPlay) {
                NSLog(@"AVPlayer Ready to Play");
            } else if (player.status == AVPlayerItemStatusUnknown) {
                NSLog(@"AVPlayer Unknown");
            }
        }
    }
    
    -(IBAction) BtnPlay:(id)sender {
        [player play];
    }
    
    -(IBAction) BtnPause:(id)sender {
        [player pause];
    }
    

    Check out the Objective-C source code for a working example of this.
    Hope this helps!

    -Update 12/7/2015 I now have a Swift example of the source code you can view here.

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

Sidebar

Related Questions

Possible Duplicate: Building a simple mp3 music player I want to create simplest mp3
I am building a website where the user can upload mp3 files. And, I
I am building a website which depends on serving lots of little mp3 files
I'm building a very simple mp3 player. What I need is to make it
I'm building a Flash mp3 player to handle streaming mp3s served by Amazon CloudFront
I am building an Android app in which client is selling some mp3 files.
So, I am building a web app that has a div with text that
I'm working on an iOS application that needs to play some sounds using the
For an audio player I'm building, I'd like to have a tag similar to
I'm building a rich web application that uses a lot of data. When I'm

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.