Search code examples
uwpwin-universal-app

Background audio with third party audio library


Im developing a music player which displays spectrum of the audio. From what i found it is possible to do that by using Bass.dll(an audio library).

However, most of those samples does not support background audio playing. I tried to add the background audio capabilities on the app manifest but it didnt change anything.

How can i use the third party audio library as well as supporting the background audio playing? I did some research and found that involving the SystemMediaTransportControl in the custom audio player could be a option but im not sure. It would be appreciate if anyone can give me a hint


Solution

  • After doing some research and experiment, I finally found out the solution.

    Based on the documentation of Background audio, dev should manually integrate the SystemMediaTrnasportControl which is already included in MediaPlayer.

    Here is the quote:

    If you are not using the automatic SMTC integration provided by MediaPlayer you must manually integrate with the SMTC to enable background audio. At a minimum, your app must enable the play and pause buttons by setting IsPlayEnabled and IsPauseEnabled to true. Your app must also handle the ButtonPressed event. If your app does not meet these requirements, audio playback will stop when your app moves to the background.

    Apps that use the new one-process model for background audio should get an instance of the SystemMediaTransportControls by calling GetForCurrentView. Apps that use the legacy two-process model for background audio must use BackgroundMediaPlayer.Current.SystemMediaTransportControls to get access to the SMTC from their background process.

    In short, these are the two minimum code that for background audio

    public BassAudioPlayer(CoreDispatcher dispatcher)
        {
            _dispatcher = dispatcher;
            _smtc = SystemMediaTransportControls.GetForCurrentView();
    
            _smtc.ButtonPressed += systemMediaControls_ButtonPressedAsync;
    
            _smtc.IsPlayEnabled = true;
            _smtc.IsPauseEnabled = true;
        }
    
        private async void systemMediaControls_ButtonPressedAsync(SystemMediaTransportControls sender, SystemMediaTransportControlsButtonPressedEventArgs args)
        {
            await _dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
            {
                //Whatever you want to add in
            });
        }