MP3 Player

I’m looking for some sample code to play MP3 files via http and from the local file system. Are there any JS or Uno examples available?

Thanks!

Hi André, Great question, we do need some of these. The audio api (and examples) got delayed internally in favour of working on our objc & java interop story. Now that that feature is approaching release we will be diving back into making some nice libraries for this.

Thanks for your patience on this, be sure that we will shout about it when it is in :slight_smile:

p.s. I’m marking this as resolved as there isnt much extra to follow up here, not because it’s actually done yet.

Hi!

We do not have an abstraction for proper mp3 playback yet. But this should not be that hard to do :slight_smile: Ill put together some code to show you a way to approach this

using Uno;
using Uno.Collections;
using Uno.Compiler.ExportTargetInterop;

namespace MP3
{
    public class Player
    {
        readonly PlayerImpl _impl;

        public Player(string uri)
        {
            _impl = PlayerImpl.Create(uri);
        }

        public void Play() { _impl.Play(); }
        public void Pause() { _impl.Pause(); }

        public double Duration
        {
            get { return _impl.Duration; }
        }

        public double Position
        {
            get { return _impl.Position; }
            set { _impl.Position = value; }
        }
    }

    abstract class PlayerImpl
    {
        public abstract void Play();
        public abstract void Pause();
        public abstract double Duration { get; }
        public abstract double Position { get; set; }

        public static PlayerImpl Create(string uri)
        {
            if defined(iOS) return new iOS.Player(uri);
            if defined(Android) return new Android.Player(uri);
            throw new Exception("Platform not supported");
        }
    }

    namespace iOS
    {
        using iOS.Foundation;
        using iOS.AVFoundation;

        extern(iOS) class Player : PlayerImpl
        {
            public override void Play() { _audioPlayer.play(); }
            public override void Pause() { _audioPlayer.pause(); }

            public override double Duration
            {
                get { return 0.0; }
            }

            public override double Position
            {
                get { return 0.0; }
                set { }
            }

            readonly AVAudioPlayer _audioPlayer;

            public Player(string uri)
            {
                _audioPlayer = new AVAudioPlayer();
                var url = new NSURL();
                url.initWithString(uri);
                NSError error;
                _audioPlayer.initWithContentsOfURLError(url, out error);
            }
        }
    }

    namespace Android
    {
        using Android.java.lang;
        using Android.android.media;

        extern(Android) class Player : PlayerImpl
        {
            public override void Play() { _mediaPlayer.start(); }
            public override void Pause() { _mediaPlayer.pause(); }

            public override double Duration
            {
                get { return _mediaPlayer.getDuration() / 1000.0; }
            }

            public override double Position
            {
                get { return _mediaPlayer.getCurrentPosition() / 1000.0; }
                set { _mediaPlayer.seekTo((int)value * 1000); }
            }

            readonly MediaPlayer _mediaPlayer;

            public Player(string uri)
            {
                _mediaPlayer = new MediaPlayer();
                _mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                _mediaPlayer.setDataSource(uri);
                _mediaPlayer.prepare();
            }
        }
    }
}

Feel free to follow this up if you have any further questions for this issue :slight_smile: