7

Voglio realizzare le seguenti cose,Come posso sapere gli utenti fanno clic avanzamento rapido e riavvolgimento rapido pulsanti sui controlli di riproduzione in iPhone

  1. applicazione è in esecuzione una musica o un video (utilizzando MPMoviePlayerController) in background.
  2. L'utente fa doppio clic sul pulsante Home e passa alla prima schermata che mostra i controlli di riproduzione (riavvolgimento rapido, riproduzione o pausa, pulsanti di avanzamento veloce)
  3. Pulsante di riavvolgimento rapido o avanzamento rapido dell'utente. Quindi l'app riproduce la musica o il video precedente o successivo.

Per il terzo passaggio, dovrei sapere quale pulsante è stato selezionato. (Come so naturalmente, l'elemento attualmente in riproduzione è in pausa, interrotto .. utilizzando la notifica MPMoviePlayerPlaybackStateDidChangeNotification).

Quale notifica dovrei registrare? O ci sono altri approcci?

+0

Come posso riprodurre i suoni utilizzando MPMoviePlayerController? –

risposta

6

Ho ricevuto la risposta da solo.

Questo sta utilizzando UIApplication beginReceivingRemoteControlEvents di UIA.

in un luogo appropriato (come viewWillAppear :) messo il seguente codice

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

E il controller della vista deve implementare il metodo seguente ritorno SI

- (BOOL)canBecomeFirstResponder { 
    return YES; 
} 

E poi è possibile ricevere evento telecomando nel seguente metodo.

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

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

E event.subtype è come sotto,

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

Questo non sembra funzionare nel simulatore che esegue un dispositivo iPhone 4.1? È questo il caso o sto facendo qualcosa di sbagliato? –

+0

Mi sembra di avere esattamente la stessa configurazione, ma non riesco a ottenere alcun evento. – JOM

+1

Plz controlla se sei diventato FirstResponder. E controlla se altre classi chiamano diventare FirstResponder dopo la classe che riceverà gli eventi chiamati becomeFirstResponder. – alones

1

questo potrebbe essere una risposta molto tardi, ma, come noto, non ci sono molti Q/sui riprodurre file audio e telecomandi, quindi spero che la mia risposta aiuti gli altri che hanno lo stesso problema:

Attualmente sto utilizzando AVAudioPlayer al momento, ma il metodo di controllo remoto che è - (void)remoteControlReceivedWithEvent:(UIEvent *)event non deve essere coinvolto con il tipo di lettore che stai utilizzando.

Per ottenere avanti e pulsanti sul lavoro schermata di blocco di riavvolgimento, segui questo:

Nel metodo viewDidLoad del controller della vista aggiungere il seguente codice:

//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]; 

Quindi aggiungere questi metodi: viewDidAppear: : (se non implementato già)

- (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: (se non è già implementato)

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

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

E:

//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. 
     } 

    } 
} 

Questo funziona bene nella mia app, se si vuole essere in grado di ricevere gli eventi di controllo a distanza in tutti i controller di vista, quindi dovresti impostarlo nello AppDelegate.

NOTA!Questo codice funziona correttamente al momento, ma vedo altri due sottotipi chiamati UIEventSubtypeRemoteControlEndSeekingBackward e UIEventSubtypeRemoteControlEndSeekingBackward. Non sono sicuro che debbano essere implementate o meno, se qualcuno ne è a conoscenza, faccelo sapere.

+0

Hai provato su iOS 8? perché sembra non funzionare @Neeku – NorthBlast

+0

@NorthBlast no, ci sono stato più di un anno fa, ma presto dovrò aggiornarlo e testarlo su iOS 8 ... – Neeku

+0

@NorthBlast A proposito, potresti pensare che non funzioni , ma in realtà è necessario toccare a lungo (toccare e tenere premuto per un po ') per farlo riavvolgere. Non è eccezionale come l'app Podcast di Apple, ma con accesso limitato alla libreria, questo è abbastanza soddisfacente. – Neeku

Problemi correlati