Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <hr> <p><strong>NOTE:</strong> As of iOS 7.1, you should be using <a href="https://developer.apple.com/library/ios/documentation/MediaPlayer/Reference/MPRemoteCommandCenter_Ref/" rel="nofollow">MPRemoteCommandCenter</a> instead of the answer below. </p> <p>You create various system-provided subclasses of <a href="https://developer.apple.com/library/ios/documentation/MediaPlayer/Reference/MPRemoteCommand_Ref/index.html#//apple_ref/swift/cl/c:objc(cs)MPRemoteCommand" rel="nofollow">MPRemoteCommand</a> and assign them to properties of the <code>[MPRemoteCommandCenter sharedCommandCenter]</code>.</p> <p>I'm keeping the rest of this around for historical reference, but the following is not guaranteed to work on recent iOS versions. In fact, it just might not.</p> <hr> <p>You definitely do need an audio <em>player</em> but not necessarily an explicit <em>session</em> to take control of the remote control events. (<code>AVAudioSession</code> is implicit to any app that plays audio.) I spent a decent amount of time playing with this to confirm this.</p> <p>I've seen a lot of confusion on the internet about where to set up the <code>removeControlEventRecievedWithEvent:</code> method and various approaches to the responder chain. I know this method works on iOS 6 and iOS 7. Other methods have not. Don't waste your time handling remote control events in the app delegate (where they used to work) or in a view controller which may go away during the lifecycle of your app. </p> <p>I made <a href="https://github.com/MosheBerman/ios-audio-remote-control/blob/master/README.md" rel="nofollow">a demo project</a> to show how to do this.</p> <p>Here's a quick rundown of what has to happen:</p> <ol> <li><p>You need to create a subclass of <code>UIApplication</code>. <em>When the documentation says <code>UIResponder</code>, it means <code>UIApplication</code>, since your application class is a subclass of <code>UIResponder</code>.</em> In this subclass, you're going to implement the <code>remoteControlReceivedWithEvent:</code> and <code>canBecomeFirstResponder</code> methods. You want to return <code>YES</code> from <code>canBecomeFirstResponder</code>. In the remote control method, you'll probably want to notify your audio player that something's changed. </p></li> <li><p>You need to tell iOS to use your custom class to run the app, instead of the default <code>UIApplication</code>. To do so, open main.m and change this:</p> <pre><code> return UIApplicationMain(argc, argv, nil, NSStringFromClass([RCAppDel`egate class])); </code></pre> <p>to look like this:</p> <pre><code>return UIApplicationMain(argc, argv, NSStringFromClass([RCApplication class]), NSStringFromClass([RCAppDelegate class])); </code></pre> <p>In my case <code>RCApplication</code> is the name of my custom class. Use the name of your subclass instead. Don't forget to <code>#import</code> the appropriate header.</p></li> <li><p>OPTIONAL: You should configure an audio session. It's not required, but if you don't, audio won't play if the phone is muted. I do this in the demo app's delegate, but do so where appropriate.</p></li> <li><p>Play something. Until you do, the remote controls will ignore your app. I just took an <code>AVPlayer</code> and gave it the URL of a streaming site that I expect to be up. If you find that it fails, put your own URL in there and play with it to your heart's content.</p></li> </ol> <p>This example has a little bit more code in there to log out remote events, but it's not all that complicated. I just define and pass around some string constants. </p> <p>I bet that a silent looping MP3 file would help work towards your goal. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload