Example #1
0
        private async Task <MediaMetadataRetriever> GetMetadataRetriever(IMediaFile currentFile)
        {
            MediaMetadataRetriever metaRetriever = new MediaMetadataRetriever();

            switch (currentFile.Type)
            {
            case MediaFileType.AudioUrl:
                await metaRetriever.SetDataSourceAsync(currentFile.Url, _requestHeaders);

                break;

            case MediaFileType.VideoUrl:
                break;

            case MediaFileType.AudioFile:
                Java.IO.File            file        = new Java.IO.File(currentFile.Url);
                Java.IO.FileInputStream inputStream = new Java.IO.FileInputStream(file);
                await metaRetriever.SetDataSourceAsync(inputStream.FD);

                break;

            case MediaFileType.VideoFile:
                break;

            case MediaFileType.Other:
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }


            return(metaRetriever);
        }
Example #2
0
        protected async Task SetMetadata(global::Android.Net.Uri uri, IDictionary <string, string>?headers)
        {
            var retriever = new MediaMetadataRetriever();

            if (uri.Scheme != null && uri.Scheme.StartsWith(Uri.UriSchemeHttp, StringComparison.OrdinalIgnoreCase))
            {
                await retriever.SetDataSourceAsync(uri.ToString(), headers ?? new Dictionary <string, string>());
            }
            else
            {
                await retriever.SetDataSourceAsync(Context, uri);
            }

            ExtractMetadata(retriever);

            MetadataRetrieved?.Invoke(this, EventArgs.Empty);
        }
Example #3
0
        private async Task <MediaMetadataRetriever> GetMetadataRetriever(IMediaFile currentFile)
        {
            var metaRetriever = new MediaMetadataRetriever();

            if (currentFile.Type == MediaFileType.Audio)
            {
                if (currentFile.Availability == ResourceAvailability.Remote)
                {
                    await metaRetriever.SetDataSourceAsync(currentFile.Url, _requestHeaders);
                }
                else
                {
                    var file        = new Java.IO.File(currentFile.Url);
                    var inputStream = new Java.IO.FileInputStream(file);
                    await metaRetriever.SetDataSourceAsync(inputStream.FD);
                }
            }

            return(metaRetriever);
        }
Example #4
0
    private async Task PrepareAndPlayMediaPlayerAsync()
    {
        try
        {
            if (NativeVersion.IsAtLeast(21))
            {
                MediaMetadataRetriever metaRetriever = new MediaMetadataRetriever();

                await mediaPlayer.SetDataSourceAsync(ApplicationContext, AndroidNet.Uri.Parse(AudioUrl));

                await metaRetriever.SetDataSourceAsync(AudioUrl, new Dictionary <string, string>());

                var focusResult = audioManager.RequestAudioFocus(new AudioFocusRequestClass
                                                                 .Builder(AudioFocus.Gain)
                                                                 .SetOnAudioFocusChangeListener(this)
                                                                 .Build());

                if (focusResult != AudioFocusRequest.Granted)
                {
                    // Could not get audio focus
                    Console.WriteLine("Could not get audio focus");
                }

                UpdatePlaybackState(PlaybackStateCode.Buffering);
                mediaPlayer.PrepareAsync();

                AquireWifiLock();
                UpdateMediaMetadataCompat(metaRetriever);
                StartNotification();

                byte[] imageByteArray = metaRetriever.GetEmbeddedPicture();
                if (imageByteArray == null)
                {
                    Cover = await BitmapFactory.DecodeResourceAsync(Resources, Resource.Drawable.player_play);
                }
                else
                {
                    Cover = await BitmapFactory.DecodeByteArrayAsync(imageByteArray, 0, imageByteArray.Length);
                }
            }
        }
        catch (Exception ex)
        {
            UpdatePlaybackState(PlaybackStateCode.Stopped);

            mediaPlayer.Reset();
            mediaPlayer.Release();
            mediaPlayer = null;

            // Unable to start playback log error
            Console.WriteLine(ex);
        }
    }
Example #5
0
        public async Task <Song> ParseAsync(File path)
        {
            var retriever = new MediaMetadataRetriever();
            await retriever.SetDataSourceAsync(path.Path);

            var title       = retriever.ExtractMetadata(MetadataKey.Title);
            var album       = retriever.ExtractMetadata(MetadataKey.Album);
            var artist      = retriever.ExtractMetadata(MetadataKey.Artist);
            var albumArtist = retriever.ExtractMetadata(MetadataKey.Albumartist);
            var coverBitmap = GetCoverFromRetriever(retriever);

            return(new Song(title, album, artist, path, albumArtist, coverBitmap));
        }
        public virtual async Task <IMediaItem> CreateMediaItem(IMediaItem mediaItem)
        {
            try
            {
                var metaRetriever = new MediaMetadataRetriever();
                await metaRetriever.SetDataSourceAsync(mediaItem.MediaUri, RequestHeaders);

                mediaItem = await ExtractMediaInfo(metaRetriever, mediaItem);
            }
            catch (Exception)
            {
            }
            return(mediaItem);
        }
        public virtual async Task <IMediaItem> CreateMediaItem(FileInfo file)
        {
            IMediaItem mediaItem = new MediaItem(file.FullName);

            try
            {
                var metaRetriever = new MediaMetadataRetriever();

                var javaFile    = new Java.IO.File(file.FullName);
                var inputStream = new Java.IO.FileInputStream(javaFile);
                await metaRetriever.SetDataSourceAsync(inputStream.FD);

                mediaItem = await ExtractMediaInfo(metaRetriever, mediaItem);
            }
            catch (Exception)
            {
            }
            return(mediaItem);
        }
        /// <summary>
        /// Intializes the player.
        /// </summary>
        public async Task Play()
        {
            if (mediaPlayer != null && MediaPlayerState == PlaybackStateCompat.StatePaused)
            {
                //We are simply paused so just start again
                mediaPlayer.Start();
                UpdatePlaybackState(PlaybackStateCompat.StatePlaying);
                StartNotification();

                //Update the metadata now that we are playing
                UpdateMediaMetadataCompat();
                return;
            }

            if (mediaPlayer == null)
            {
                InitializePlayer();
            }

            if (mediaSessionCompat == null)
            {
                InitMediaSession();
            }

            if (mediaPlayer.IsPlaying)
            {
                UpdatePlaybackState(PlaybackStateCompat.StatePlaying);
                return;
            }

            try {
                MediaMetadataRetriever metaRetriever = new MediaMetadataRetriever();

                await mediaPlayer.SetDataSourceAsync(ApplicationContext, Android.Net.Uri.Parse(audioUrl));

                await metaRetriever.SetDataSourceAsync(audioUrl, new Dictionary <string, string>());

                var focusResult = audioManager.RequestAudioFocus(this, Stream.Music, AudioFocus.Gain);
                if (focusResult != AudioFocusRequest.Granted)
                {
                    //could not get audio focus
                    Console.WriteLine("Could not get audio focus");
                }

                UpdatePlaybackState(PlaybackStateCompat.StateBuffering);
                mediaPlayer.PrepareAsync();

                AquireWifiLock();
                UpdateMediaMetadataCompat(metaRetriever);
                StartNotification();

                byte[] imageByteArray = metaRetriever.GetEmbeddedPicture();
                if (imageByteArray == null)
                {
                    Cover = await BitmapFactory.DecodeResourceAsync(Resources, Resource.Drawable.album_art);
                }
                else
                {
                    Cover = await BitmapFactory.DecodeByteArrayAsync(imageByteArray, 0, imageByteArray.Length);
                }
            } catch (Exception ex) {
                UpdatePlaybackState(PlaybackStateCompat.StateStopped);

                mediaPlayer.Reset();
                mediaPlayer.Release();
                mediaPlayer = null;

                //unable to start playback log error
                Console.WriteLine(ex);
            }
        }