0
votes

I am working on iOS app let me play mp3 file from url. It works fine once loaded.The problem is it takes about 30 seconds to load and blocks UI from transition to other view controller. code:

I am using AVAudioPlayer class and here are my settings.

(void)setupAudioPlayer:(NSString*)fName
{

         // calls and pass url name to MyAudioPlayer class method
         [self.audioPlayer initWithData:fName];
         self.currentTimeSlider.maximumValue = 
         [self.audioPlayer getAudioDuration];

         //init the current time display and the labels.
         //if a current time was stored
        //for this player then take it and update the time display
        self.timeElapsed.text = @"0:00";

        self.duration.text = [NSString stringWithFormat:@"-%@",
                          [self.audioPlayer timeFormat:
                         [self.audioPlayer  getAudioDuration]]];

}
    (void)viewDidLoad
    {
        [super viewDidLoad];
        self.audioPlayer = [[MyAudioPlayer alloc] init];
        //pass the url where mp3 located i.e http://www.test.com/test.mp3
        [self setupAudioPlayer:self.urlStr];
    }

then, it play when button below pressed.

- (IBAction)playAudioPressed:(id)playButton
 {
     [self.timer invalidate];

     //play audio for the first time or if pause was pressed
     if (!self.isPaused) {

        //start a timer to update the time label display
        self.timer = 
        [NSTimer scheduledTimerWithTimeInterval:1.0
                                         target:self
                                       selector:@selector(updateTimer:)
                                       userInfo:nil
                                        repeats:YES];

         [self.audioPlayer playAudio];
         self.isPaused = TRUE;

     } else {
          [self.audioPlayer pauseAudio];
          self.isPaused = FALSE;
    }
}

As I mentioned earlier the problem is loading for very long and blocking UI. Please help or suggest me with how to decrease loading time and avoid blocking ui.

any help will be very appreciated.

Thank you

1

1 Answers

0
votes

Here is my code for the STREAMING of a remote mp3 file:

@import AVFoundation;
...

@property (nonatomic, retain) AVPlayer *objAVPlayer;
@property (nonatomic, getter=isPlaying) BOOL playing;

// in viewDidLoad
_objAVPlayer = [AVPlayer playerWithURL:[NSURL URLWithString:@"http://www.asdasd.com/file.mp3"]];

...

- (void)playPressed {
    if([self isPlaying]) {
        _playing = false;
        [_objAVPlayer pause];
    } else {
        _playing = true;
        [_objAVPlayer play];
    }
}

A possible time delay can be due to a slow connection. I hope this can help you.