简体   繁体   English

如何从Xamarin.Forms中的URL播放音乐?

[英]How to play music from url in Xamarin.Forms?

I am gonna build Xamarin.Forms app which play music from url. 我要构建Xamarin.Forms应用程序,该应用程序从URL播放音乐。

I used dependency service for each platform implementation. 我对每个平台实现都使用了依赖服务。

[assembly: Dependency(typeof(AudioSerivce))]

namespace xxx.Droid { 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. 但是我听不到Android手机上的声音。 If anyone has some suggestion, please let me know. 如果有人有任何建议,请告诉我。 Thanks 谢谢

From: 从:

https://forums.xamarin.com/discussion/64218/no-video-player-really 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? 我怀疑可能存在与Octane Video Player和Android Emulator不兼容有关的问题,您使用的是模拟器吗?

Also another suggestion is to add this permissions: 另外,建议添加此权限:

INTERNET WRITE_EXTERNAL_STORAGE READ_EXTERNAL_STORAGE 互联网WRITE_EXTERNAL_STORAGE READ_EXTERNAL_STORAGE

To your Android app. 转到您的Android应用。

If that does not work try to use another url and compare if it working with other url's. 如果这样不起作用,请尝试使用其他网址,并与其他网址进行比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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