0

I'm trying to implement a sample iOS 8 today widget that includes a music player controls, the app is a basic music app with background-audio enabled.

I looked a lot around to find a way for notifying containing app, but I didn't find any!.

The scenario is basically, app is in suspended state and on a selected song/album (it might be paused on a certain song), then i need to display the names on the widget with play/stop/next buttons, etc.

Based on Apple's documentation, and this document which describes the usage scenarios: https://developer.apple.com/library/ios/documentation/General/Conceptual/ExtensibilityPG/ExtensibilityPG.pdf

It mentions that extensions shouldn't handle any long execution and stated specifically audio playback as a non-supported case as it needs to run outside the extension. but it didn't mention a way to trigger containing app in such scenario, it just mentioned NSURLSession technique which can handle big files downloads and wakeup app in background to call handleEventsForBackgroundUrlSession, and the App-Group share capabilities where both containing app and extension can access a shared data container. The only feasible solution that's stated is openURL solution which will forcibly bring the whole app to foreground.

I did setup a single App-Group for my demo and tried NSNotificationCenter's calls using a custom notification, by calling addObserver/postNotificationName on containing app and extension respectively, but it didn't work at all. I even tried to change NSUserDefaults of App-Group instead, but this doesn't fire in containing app's background too.

Here is some questions that might be related, but i didn't find an answer in it: (Sharing data between app extension and conainer app) --- (NSUserDefaultsDidChangeNotification not sent when app resumes from the background) --- (Set notifications from Today Extension Widget)

I thought that it might not be doable, but I just found an app called TapTunes that recently added a widget with similar features including all controls in the extension itself.

Thanks.

Community
  • 1
  • 1

1 Answers1

0

The question is, do you want to play some custom audio, or just tracks from the iOS music player?

To me it looks like TapTunes uses the standard music player. I think this can be accomplished by using MPMusicPlayerController (https://developer.apple.com/library/ios/documentation/MediaPlayer/Reference/MPMusicPlayerController_ClassReference/)

If you want to play custom audio, usually you have a special audio background mode enabled (see here: http://www.raywenderlich.com/29948/backgrounding-for-ios), and have a callback that is continuously called to feed in new audio data, e.g. coming from the internet. Maybe you could just observe a NSUserDefault key in the callback that tells you the current action ("next track", "previous track", etc.). I guess these could lead to some delays until the action is actually performed.

Anyway, don't be surprised if the widget gets rejected by App Store, as they seem to be very careful lately with widgets that do not follow the usual "show me some quick information" concept. That TapTunes was approved does not mean that your app will be approved as well.

Markus
  • 436
  • 3
  • 13