0

I am working on Audio playing app using "MpmusicPlayer" and want to resume it after paused.

I am using "self.appMusicPlayer.currentPlaybackTime" but it is not working in swift 4.1 (IOS 11).

Is there have any other way to resume audio after pause using MpMusicPlayer?

Galaxy Patel
  • 154
  • 7

1 Answers1

0

It seems that this is a problem with iOS 11 that some people have run into when setting the currentPlaybackTime after calling play(). There was a work around posted on this apple dev forum here, but please find the code below (thanks to RunLoop).

I had just run into this exact problem. I managed to get around it with the following code.

It creates a background thread that continuously checks the currentPlaybackTime of the player. As soon as currentPlaybackTime is not the time I set it to be I set currentPlaybackTime back to what I wanted.

It feels like a terrible hack but it's working for me so far.

MPMusicPlayerController *player = [MPMusicPlayerController systemMusicPlayer];  
player.currentPlaybackTime = _startTime;  
[player play];  

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul);  
dispatch_async(queue, ^{  
  while(true) {  
    if (player.currentPlaybackTime != _startTime) {  
      player.currentPlaybackTime = _startTime;  
      break;  
    }  
  }  
});
WoodyDev
  • 1,386
  • 1
  • 9
  • 19
  • @GalaxyPatel I've edited my answer with another code example – WoodyDev Jul 06 '18 at 14:11
  • thanks for your reply but this second one also not working for me because MPMusicPlayer always play music from start position and after it DispatchQueue is called. And I change value of currentPlaybackTime but MpMusicPlayer start playing music from 0.0 always. – Galaxy Patel Jul 09 '18 at 13:07