1

I'm making a news iOS app, i want to implement a live audio streaming in all the pages like the picture below:

App Screenshot

The orange view is a UIView that says "Live Streaming" (in Arabic), it exists in all the viewControllers, i want to let the AVPlayer plays when the user clicks on it and still plays even if the user navigates to other viewControllers.

I can't seem to find a good solution or tutorial about this, sorry if this is a noob question cause i'm new to iOS.

So how can i do that ? and where(in which viewController) should i put the AVPlayer declaration ?

Thanks in advance.

Fadi Obaji
  • 1,454
  • 4
  • 27
  • 57
  • I would probably try using NSUserDefaults to store the users selection, and have each view check whether anything has been selected and if so to play it. – App Dev Guy Jul 03 '15 at 13:02
  • @SaSmith and how can i keep the AVPlayer playing, i think through the audio session, i tried it and didn't work – Fadi Obaji Jul 03 '15 at 13:12
  • To be honest, I am not too sure. I wouldn't think it should be dismissing the AVPlayer when switching views though, not unless you have coded that somewhere, that AVPlayer should stop playing when view is changed. The only thing I could think of is if the app is retrieving content from online it may not be allowed to keep playing in the background, but I am not 100% sure about that. – App Dev Guy Jul 03 '15 at 13:15
  • Have posted an answer, please check. – App Dev Guy Jul 03 '15 at 13:29

2 Answers2

1

If you are testing in the simulator, the change of views will not continue the audio in the background. If it is not working your device try this:

NSURL *url = [NSURL URLWithString: yourURLHere];    
AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:url options:nil];    
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:avAsset];    
AVPlayer *audioPlayer = [AVPlayer playerWithPlayerItem:playerItem];    
//This enables background music playing
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];    

[audioPlayer play];

Place this code in your viewDidLoad method, before [super viewDidLoad];

I found two links that exaplin some of the issues on this:

  1. Play Music in Background
  2. Play Audio from Internet

If this does not work, I would recommend placing a bounty on the question as it seems your issue's are a little more in depth than a simple solution.

Community
  • 1
  • 1
App Dev Guy
  • 5,396
  • 4
  • 31
  • 54
0

I found the solution, i had to make a singleton class.

The singleton class allow only one object to be used in the entire iOS application by all the ViewControllers.

And i did put all the needed methods like play and pause in the singleton class and i accessed them from all the ViewControllers.

If anybody wants an example, please ask in the comments.

Fadi Obaji
  • 1,454
  • 4
  • 27
  • 57