简体   繁体   中英

How to play music from url in Xamarin.Forms?

I am gonna build Xamarin.Forms app which play music from url.

I used dependency service for each platform implementation.

[assembly: Dependency(typeof(AudioSerivce))]

namespace xxx.Droid {

public class AudioSerivce : IAudio
{
    int clicks = 0;
    MediaPlayer player;

    public AudioSerivce()
    {
    }

    public bool Play_Pause (string url)
    {
        if (clicks == 0) {
            this.player = new MediaPlayer();
            this.player.SetDataSource(url);
            this.player.SetAudioStreamType(Stream.Music);
            this.player.PrepareAsync();
            this.player.Prepared += (sender, args) =>
            {
                this.player.Start();
            };
            clicks++;
        } else if (clicks % 2 != 0) {
            this.player.Pause();
            clicks++;

        } else {
            this.player.Start();
            clicks++;
        }

        return true;
    }

    public bool Stop (bool val)
    {
        this.player.Stop();
        clicks = 0;
        return true;
    }
}

}

and calling it

DependencyService.Get<IAudio>().Play_Pause("https://www.searchgurbani.com/audio/sggs/1.mp3");

If I check log, it seems everything is ok. But I can't hear sound on android phone. If anyone has some suggestion, please let me know. Thanks

From:

https://forums.xamarin.com/discussion/64218/no-video-player-really

I suspect that there may be a problem related to an incompatibility between Octane Video player and Android Emulator, are you using a emulator/simulator?

Also another suggestion is to add this permissions:

INTERNET WRITE_EXTERNAL_STORAGE READ_EXTERNAL_STORAGE

To your Android app.

If that does not work try to use another url and compare if it working with other url's.

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