0

I made a Dependency-Service for playing audio and background control in Xamarin.ios .

Audio was executed in background well. but, Lockscreen controller was not shown.

I don't know what is problem.

I registered "UIBackgroundMode" and "audio" in "info.plist" and set the AVAudioSession as playback.

I used play_pause(string url) function for playing audio and setting the MPNowPlayingInfo.

[assembly: Dependency(typeof(AudioService))]

namespace BibleHymn.iOS
{

 class AudioService : IAudio

{

   private bool isPlaying = false;

   private bool interrupted = false;

   int audioNum = 0;

    AVFoundation.AVPlayer player;



public AudioService()
{

     var avSession = AVAudioSession.SharedInstance();
            avSession.SetCategory(AVAudioSessionCategory.Playback);
            NSError activationError = null;
            avSession.SetActive(true, out activationError);

}

public bool Play_Pause(string url)
{

            if(player == null)
            {

                this.player = new AVFoundation.AVPlayer();
                this.player = AVFoundation.AVPlayer.FromUrl(Foundation.NSUrl.FromString(url));
                this.player.Play();
                UpdateNotification();

            }
                 isPlaying !=isPlaying;
                 return isPlaying;

} 


public void  UpdateNotification()
        {
          var item = new MPNowPlayingInfo
              {
                  Title = "My Title"
              };
              MPNowPlayingInfoCenter.DefaultCenter.NowPlaying = item;

              Device.BeginInvokeOnMainThread(() => {
                 UIKit.UIApplication.SharedApplication.BeginReceivingRemoteControlEvents();
              });
        }
}

I used play_pause from viewModel through button control's command.

init like ...

 public IAudio streamService = DependencyService.Get<IAudio>();

and in command ...

streamService.Play_Pause(url);

1 Answer 1

0

You need to register background support for Audio in the info.plist by following screenshot

enter image description here Also did you import Mediaplayer.framework

1
  • Yes ,I already done. It is equal to "UIBackgroundMode" key and string value as "audio" in xml source mode. also I already imported MediaPlayer in origin code.
    – jj cheo
    Dec 28, 2018 at 7:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.