7

播放控制和快退按钮我想实现下面的东西,我怎么能知道用户点击快进在iPhone

  1. 应用程序在后台运行音乐或视频(使用MPMoviePlayerController)。
  2. 用户双击主页按钮并转到显示播放控件的第一个屏幕(快退,播放或暂停,快进按钮)
  3. 用户单击快退或快进按钮。 然后,应用播放上一首或下一首音乐或视频。

对于第三步,我应该知道哪个按钮被点击。 (正如我自然知道的那样,当前正在播放的项目被暂停,停止..使用MPMoviePlayerPlaybackStateDidChangeNotification通知)。

我应该注册哪个通知?或者还有其他方法吗?

+0

如何使用MPMoviePlayerController播放声音? – 2011-05-09 16:46:59

回答

6

我自己得到了答案。

这是使用UIApplication的beginReceivingRemoteControlEvents。

在一个合适的位置(如viewWillAppear中:)把下面的代码

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; 
[self becomeFirstResponder]; 

和视图控制器应实现以下方法返回YES

- (BOOL)canBecomeFirstResponder { 
    return YES; 
} 

然后你就可以接收遥控器事件在下面的方法。

- (void)remoteControlReceivedWithEvent:(UIEvent *)event { 

    if(event.type == UIEventTypeRemoteControl) { 
     NSLog(@"sub type: %d", event.subtype); 
    } 
} 

而且event.subtype是如下,

typedef enum { 
    // available in iPhone OS 3.0 
    UIEventSubtypeNone        = 0, 

    // for UIEventTypeMotion, available in iPhone OS 3.0 
    UIEventSubtypeMotionShake      = 1, 

    // for UIEventTypeRemoteControl, available in iPhone OS 4.0 
    UIEventSubtypeRemoteControlPlay     = 100, 
    UIEventSubtypeRemoteControlPause    = 101, 
    UIEventSubtypeRemoteControlStop     = 102, 
    UIEventSubtypeRemoteControlTogglePlayPause  = 103, 
    UIEventSubtypeRemoteControlNextTrack   = 104, 
    UIEventSubtypeRemoteControlPreviousTrack  = 105, 
    UIEventSubtypeRemoteControlBeginSeekingBackward = 106, 
    UIEventSubtypeRemoteControlEndSeekingBackward = 107, 
    UIEventSubtypeRemoteControlBeginSeekingForward = 108, 
    UIEventSubtypeRemoteControlEndSeekingForward = 109, 
} UIEventSubtype; 
+1

这在运行iPhone 4.1设备的模拟器中似乎无法工作?这是这种情况还是我做错了什么? – 2010-11-20 15:04:47

+0

我似乎有完全相同的设置,但只是不能得到任何事件。 – JOM 2010-12-22 21:08:21

+1

Plz检查你是否成为FirstResponder。并且检查其他类是否会在将接收名为becomeFirstResponder的事件的类后调用becomeFirstResponder。 – alones 2011-01-22 02:32:04

1

这可能是一个非常晚的答案,但我注意到,没有多少Q /经过关于音频播放和遥控器,所以我希望我的回答可以帮助其他有相同问题的人:

我目前使用AVAudioPlayer,但是远程控制方法- (void)remoteControlReceivedWithEvent:(UIEvent *)event不能与您使用的播放器的类型有关。

为了让进和快退的锁屏操作的按钮,遵循这样的:

在您的视图控制器的viewDidLoad方法添加以下代码:

//Make sure the system follows our playback status - to support the playback when the app enters the background mode. 
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil]; 
[[AVAudioSession sharedInstance] setActive: YES error: nil]; 

然后将这些方法: viewDidAppear: :(如果尚未实施)

- (void)viewDidAppear:(BOOL)animated { 
    [super viewDidAppear:animated]; 

    //Once the view has loaded then we can register to begin recieving controls and we can become the first responder 
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; 
    [self becomeFirstResponder]; 
} 

viewWillDisappear:(如果尚未实施)

- (void)viewWillDisappear:(BOOL)animated { 
    [super viewWillDisappear:animated]; 

    //End recieving events 
    [[UIApplication sharedApplication] endReceivingRemoteControlEvents]; 
    [self resignFirstResponder]; 
} 

和:

//Make sure we can recieve remote control events 
- (BOOL)canBecomeFirstResponder { 
    return YES; 
} 

- (void)remoteControlReceivedWithEvent:(UIEvent *)event { 
    //if it is a remote control event handle it correctly 
    if (event.type == UIEventTypeRemoteControl) 
    { 
     if (event.subtype == UIEventSubtypeRemoteControlPlay) 
     { 
      [self playAudio]; 
     } 
     else if (event.subtype == UIEventSubtypeRemoteControlPause) 
     { 
      [self pauseAudio]; 
     } 
     else if (event.subtype == UIEventSubtypeRemoteControlTogglePlayPause) 
     { 
      [self togglePlayPause]; 
     } 

     else if (event.subtype == UIEventSubtypeRemoteControlBeginSeekingBackward) 
     { 
      [self rewindTheAudio]; //You must implement 15" rewinding in this method. 
     } 
     else if (event.subtype == UIEventSubtypeRemoteControlBeginSeekingForward) 
     { 
      [self fastForwardTheAudio]; //You must implement 15" fastforwarding in this method. 
     } 

    } 
} 

这是工作在我的应用程序正常,但是如果你希望能够接收遥控事件中的所有视图控制器,然后你应该把它设置在AppDelegate

注意!此代码目前工作正常,但我看到两个更多的子类型称为UIEventSubtypeRemoteControlEndSeekingBackwardUIEventSubtypeRemoteControlEndSeekingBackward。我不确定他们是否必须执行,如果有人知道它,让我们知道。

+0

你在iOS 8中测试过吗?因为似乎没有工作@Neeku – NorthBlast 2015-02-03 19:44:03

+0

@NorthBlast不,我在一年前,但需要更新和测试它在iOS 8上很快... – Neeku 2015-02-03 23:08:33

+0

@NorthBlast顺便说一句,你可能认为它不工作,但实际上您需要长时间轻按(轻按并保持一段时间)才能使其倒带。它不如苹果自己的Podcast应用程序,但对图书馆的访问有限,这足够令人满意。 – Neeku 2015-02-06 11:36:28

相关问题