简体   繁体   中英

How can you play an AudioFile that will keep playing even if you navigate through pages in Xamarin Forms?

I am trying to make like my own music app. With my dependency service solution in iOS i can hear the mp3 just fine but once I navigate through other pages the file stops. So my question is, how can I make so the file keeps playing even if i navigate through different pages after i "play" it?

This is my code: My button where I pick the "track".

async void PlayThisSongButton (object s, EventArgs e)
    {
        DependencyService.Get<IPlayerVoice>().PlayAudioFile("myfilename.mp3");
    }

Interface:

public interface IPlayerVoice
{
    void PlayAudioFile(string fileName);
}

Dependency service iOS:

[assembly: Xamarin.Forms.Dependency(typeof (VoicePlayer_iOS))]

namespace myProject.iOS
{
  public class VoicePlayer_iOS : IPlayerVoice
  {
    AVAudioPlayer player;

    public VoicePlayer_iOS()
    {
    }

    public void PlayAudioFile(string fileName)
    {
        string sFilePath = NSBundle.MainBundle.PathForResource
        (Path.GetFileNameWithoutExtension(fileName), Path.GetExtension(fileName));
        var url = NSUrl.FromString(sFilePath);
        var _player = AVAudioPlayer.FromUrl(url);
        _player.FinishedPlaying += (object sender, AVStatusEventArgs e) =>
        {
            _player = null;
        };
        _player.Play();
     }

   }
}

So with this current code. I click on my button where i start the audiofile. I then navigate to a different page. The audiofile stops. Any idea how i can solve this?

I would say you need to Use MVVM or MVC and run the music on the main view.

this will allow you to browse the app, while your music still plays.

basically have the Main view and create a grid that you place your "browserView" in, add all the normal code you would use for the app to the viewmodel of the browser.

then add the code for the music to the viewmodel of the mainview.

Just make sure to add the functionality to pause the music.

I have never worked with audio in Xamarin Forms but looked into it once.

I think the comment from @yanyankelevich with a GitHub link would still stop the audio when the user navigates away from the page.

As @yanyankelevich suggested though, using a background service might be your best bet. Some links for that (there is a lot of code so I will not duplicate it in this post):

iOS AVAudioPlayer (specifically check out the PlayBackgroundMusic method and the other background related methods below that one)

Android Background Audio Streaming

For a super simple way of doing it (which I do not suggest and have not ever tried), have you tried just running your method within a Device.BeginInvokeOnMainThread() which will do a fire and forget?

Device.BeginInvokeOnMainThread(() => DependencyService.Get<IPlayerVoice>().PlayAudioFile("myfilename.mp3"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM