Xamarin IOS:无法从互联网上播放mp3

问题描述 投票:2回答:2

我阅读了很多关于这个问题的文档,但我没有看到任何答案。

我有一个Xamarin表单应用程序,播放来自Groove音乐服务的mp3样本音乐。

在Android上,一切正常。在Ios上,我无法播放声音。我得到了网址,它不可能听到任何东西(我能够播放本地mp3)

我发现使用SWIFT的人也遇到了这个问题:How to play MP3 From URL in iOS

我也在Xamarin论坛上找到了几个例子但是没有声音:https://forums.xamarin.com/discussion/19883/how-do-i-get-the-avplayer-to-play-an-mp3-from-a-remote-url

这是我使用的代码:

public class AudioService:IAudio {protected string FileName = string.Empty;受保护的AVPlayer播放器;

    public bool IsPlaying { get; private set; }

    public void Init(string fileName)
    {
        //FileName = fileName;
        string second = "http://progdownload.zune.net/145/119/034/170/audio.mp3?rid=0b80911a-ba3b-42ec-b17f-c242ba087024-s4-nl-BE-music-asset-location";
        FileName = second;


        QueueFile();
    }

    public async void PlayStream(string uri)
    {
        Init(uri);
        System.Diagnostics.Debug.WriteLine("Enter in function");

        Player.Play();
        System.Diagnostics.Debug.WriteLine("Play sound");
        System.Diagnostics.Debug.WriteLine(FileName);
        IsPlaying = true;
        System.Diagnostics.Debug.WriteLine(IsPlaying);
    }

    public void Pause()
    {
        IsPlaying = false;
        Player.Pause();
    }

    public void Stop()
    {
        IsPlaying = false;
        if (Player == null) return;
        Player.Dispose();
        Player = null;
    }

    public bool HasFile
    {
        get { return Player != null; }
    }

    private void QueueFile()
    {
        if (string.IsNullOrWhiteSpace(FileName))
        {
            throw new Exception("No file specified to play");
        }

        using (var url = NSUrl.FromString(FileName))
        {

            var test = AVAsset.FromUrl(url);
            var playerItem = AVPlayerItem.FromAsset(test);

            // if Player is null, we're creating a new instance and seeking to the spot required
            // otherwise we simply resume from where we left off.
            if (Player == null)
            {
                Player = AVPlayer.FromPlayerItem(playerItem);
                if (Player == null)
                {
                    // todo: what should we do if the file doesn't exist?
                    return;
                }
            }
        }
    }}

(IAudio只实现了playstream并停止)

如果您点击网址,您的浏览器将能够播放音乐http://progdownload.zune.net/145/119/034/170/audio.mp3?rid=0b80911a-ba3b-42ec-b17f-c242ba087024-s4-nl-BE-music-asset-location

有没有人能够从互联网上播放mp3

c# ios audio xamarin.ios xamarin.forms
2个回答
1
投票

info.list文件中的答案。您需要将其添加到info.list:

<key>NSAppTransportSecurity</key><dict>
<key>NSAllowsArbitraryLoads</key>
<true/></dict>

我有同样的问题并解决了它。


0
投票

我为你的代码做了一些清理:)这就是我们得到的,包括检测播放的结束

public class AudioService 
{
    #region Members
    protected AVPlayer Player;
    #endregion

    #region Properties
    public bool IsPlaying { get; private set; }
    public bool HasFile => Player != null;
    public Action MediaEnded { get; set; }
    #endregion

    #region Public Methods
    public void PlayStream(string uri)
    {
        try
        {
            if (Player != null)
                Player.Dispose();

            Player = null;
            QueueFile(uri);

            Player.Play();
            IsPlaying = true;
        }
        catch (Exception ex)
        {
            IsPlaying = false;
            Crashes.TrackError(ex);
        }
    }

    public void Pause()
    {
        IsPlaying = false;
        Player.Pause();
    }

    public void Stop()
    {
        IsPlaying = false;
        if (Player == null)
            return;
        Player.Dispose();
        Player = null;
    }

    public void QueueFile(string fileName)
    {
        if (string.IsNullOrWhiteSpace(fileName))
        {
            return;
        }

        using (var url = NSUrl.FromString(fileName))
        {

            var asset = AVAsset.FromUrl(url);
            var playerItem = AVPlayerItem.FromAsset(asset);

            if (Player == null)
            {
                Player = AVPlayer.FromPlayerItem(playerItem);
                if (Player == null)
                {
                    return;
                }
                else
                {
                    NSNotificationCenter.DefaultCenter.AddObserver(AVPlayerItem.DidPlayToEndTimeNotification, OnPlayerDidFinishPlaying, NSObject.FromObject(playerItem));
                }
            }
        }
    }
    #endregion

    #region Private Methods
    private void OnPlayerDidFinishPlaying(NSNotification notification)
    {
        IsPlaying = false;
        MediaEnded?.Invoke();
    }
    #endregion
© www.soinside.com 2019 - 2024. All rights reserved.