Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 91519

Setting the source for MediaPlayer

$
0
0

Please enlighten me in this scenario:

I have an interface:

public interface ISoundService
    {
        void SetSource(string pathToFile);
        Task<bool> Play();
        void Stop();
        void Pause();
    }

I have it simple implemented on Android project:

`public class SoundService : ISoundService
{
    private MediaPlayer player;
    private bool paused;

    private const string Mp3 = @"http://www.montemagno.com/sample.mp3";

    public SoundService ()
    {

    }

    #region IAudioOperations implementation

    private void IntializePlayer()
    {
        player = new MediaPlayer();

        //Tell our player to sream music
        player.SetAudioStreamType(Stream.Music);

        //When we have prepared the song start playback
        player.Prepared += (sender, args) => player.Start();

        //When we have reached the end of the song stop ourselves, however you could signal next track here.
        player.Completion += (sender, args) => Stop();

        player.Error += (sender, args) =>
        {
            //playback error
            Console.WriteLine("Error in playback resetting: " + args.What);
            Stop();//this will clean up and reset properly.
        };
    }

    public void SetSource (string pathToFile)
    {

    }

    public async Task<bool> Play ()
    {
        bool ret = false;

        if (paused && player != null) {
            paused = false;
            //We are simply paused so just start again
            player.Start();
            ret = false;
        }

        if (player == null) {
            IntializePlayer();

            ret = false;
        }

        if (player.IsPlaying)
            ret = true;

        try {
            await player.SetDataSourceAsync(global::Android.App.Application.Context, Android.Net.Uri.Parse(Mp3));

            player.PrepareAsync();

            ret = true;
        }
        catch (Exception ex) {
            Console.WriteLine("Unable to start playback: " + ex);
        }

        return ret;
    }

    public void Stop ()
    {
        if (player == null)
            return;

        if(player.IsPlaying)
            player.Stop();

        player.Reset();
        paused = false;
    }

    public void Pause ()
    {
        if (player == null)
            return;

        if(player.IsPlaying)
            player.Pause();

        paused = true;
    }

    #endregion
}`

at this moment I am simple using const for file to be played, but now I want to change it and use this SetSource method, but when I should set this file? of course I am using MVVM pattern.

        `Button playButton = new Button ()
        {
            Text = "Play",
            Image = "ic_play.png",
        };

        playButton.Clicked += (sender, e) => 
        {
            ViewModel.PlayCommand.Execute(null);
        };

        Button stopButton = new Button () 
        {
            Text = "Stop",
            Image = "ic_stop.png",
        };

        stopButton.Clicked += (sender, e) => 
        {
            ViewModel.StopCommand.Execute(null);
        };

        Button pauseButton = new Button () 
        {
            Text = "Pause",
            Image = "ic_pause.png",
        };

        pauseButton.Clicked += (sender, e) => 
        {
            ViewModel.PauseCommand.Execute(null);
        };`

Thank for any advices.


Viewing all articles
Browse latest Browse all 91519

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>