iOS MPNowPlayingInfoCenter causes error

ThomasUThomasU USMember ✭✭
edited October 2015 in Xamarin.iOS

In my application i have a radio stream and get the currently playing song information from a service.
In addition I want the user to be able to listen to it in the background, this works fine with the AVAudioSession and running good.
But I struggle to understand how I can add Information to the Lock Screen via MPNowPlaying or how to use the RemoteControlReceive.

I tried making my way up from this sample https://github.com/jgold6/Xamarin-iOS-LockScreenAudio but cannot get the hang of this.

As soon as I implement the MPNowPlayingInfo into my Code I get an error on the SpringBoard.

MPNowPlayingInfo np = new MPNowPlayingInfo();
np.Artist = "SongArtist";
np.Title = "SongTitle";
np.Artwork = new MPMediaItemArtwork(image);
MPNowPlayingInfoCenter.DefaultCenter.NowPlaying = np;

Answers

  • ThomasUThomasU USMember ✭✭

    Alright I tried some things and set up an fresh tabbed view Project in VS.

    I tried working with the standard AVPlayer and the AVAudioSession.

    NSUrl radioURL = new NSUrl("http://dreamsiteradiocp3.com:8086/;stream.mp3");
    player = new AVPlayer(radioURL);
    
    AVAudioSession session = AVAudioSession.SharedInstance();
    NSError error;
    session.SetCategory(AVAudioSession.CategoryPlayback, out error);
    session.SetActive(true, out error);
    

    I am listening for Remote Events which works fine.

    UIApplication.SharedApplication.BeginReceivingRemoteControlEvents();
    
    public override void RemoteControlReceived(UIEvent theEvent)
    {
           base.RemoteControlReceived(theEvent);
           if (theEvent.Subtype == UIEventSubtype.RemoteControlPlay)
               player.Play();
           else if (theEvent.Subtype == UIEventSubtype.RemoteControlPause)
              player.Pause();
     }
    

    Setting the MPNowPlayingInfo works

    MPNowPlayingInfo nowPlayingInfo;
    nowPlayingInfo = new MPNowPlayingInfo();
    nowPlayingInfo.Artist = "An OK Band";
    nowPlayingInfo.Title = "Our First Single!";
    
    MPNowPlayingInfoCenter.DefaultCenter.NowPlaying = nowPlayingInfo;
    

    But the InfoCenter don't know that actual audio is playing. Also it only shows it in the Control Center by swiping up from the bottom edge of your screen. There is nothing displayed on the LockScreen and it doesn't know that audio is playing.

    I attach the complete sample Project to watch all of the code.

    Thanks for for the time investment.

  • ThomasUThomasU USMember ✭✭

    Alright at the end it was pretty simple and thanks to the support I have the solution.
    For the controls to register the audio playing you have to set the MPNowPlayingInfo.PlaybackRate, if the Rate is 0 no audio is playing.
    After manually adding it to the Play() and Pause() it now displays and works correctly.

    Simplyfied something like this.

    Play()

    var np = new MPNowPlayingInfo();
    np.PlaybackRate = 1.0f;
    StreamingPlayer.SetNowPlayingInfo(np);
    

    Pause()

    var np = new MPNowPlayingInfo();
    np.PlaybackRate = 0.0f;
    StreamingPlayer.SetNowPlayingInfo(np);
    
  • MarselMutlak.3399MarselMutlak.3399 USUniversity
    edited August 2017

    @ThomasU Thank you alot for your question and for your answer on it :)

Sign In or Register to comment.