Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin IOS: unable to play mp3 from internet

I read a lot of documentation regarding this problem, but I don't seen any answer.

I have a Xamarin form app that play mp3 sample music from the Groove music service.

On Android, everyhting workd fine. On Ios, I'm not able to play the sound. I get the URL and it's not possible to ear anything (I was able to play a local mp3)

I see this problem occured also for people using SWIFT : How to play MP3 From URL in iOS

I also found several exemple on the Xamarin forum but no sound neither: https://forums.xamarin.com/discussion/19883/how-do-i-get-the-avplayer-to-play-an-mp3-from-a-remote-url

Here is the code I use:

public class AudioService : IAudio { protected string FileName = string.Empty; protected AVPlayer Player;

    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 just implements playstream and stop)

If you click on the url, your browser will be able to play music http://progdownload.zune.net/145/119/034/170/audio.mp3?rid=0b80911a-ba3b-42ec-b17f-c242ba087024-s4-nl-BE-music-asset-location

Does any one was able to play mp3 from internet

like image 903
Refre Avatar asked Dec 09 '25 05:12

Refre


1 Answers

The answer in the info.list file. You need to add this to your info.list:

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

I had the same issue and resolved it.

like image 53
Thando Toto Avatar answered Dec 10 '25 19:12

Thando Toto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!