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

相關問題