Exemplo n.º 1
0
        public void PlaySong(string name)
        {
            var resId = Resources.GetIdentifier(name, "raw", PackageName);

            if (player != null && player.IsPlaying)
            {
                player.Stop();
            }

            player             = MediaPlayer.Create(this, resId);
            player.Completion += delegate
            {
                player = null; //Free the memory
            };
            player.Start();
        }
Exemplo n.º 2
0
        private void PlayPauseMusic(object sender, EventArgs eventArgs)
        {
            //If the mediaPlayer previously played before.
            if (mediaPlayer != null)
            {
                //If a current music is playing
                if (mediaPlayer.IsPlaying)
                {
                    //If the currently playing song does not match the displayed song.
                    if (currentPlayingSong != displayedSong)
                    {
                        //plays the new song.
                        playBtn.SetImageDrawable(GetDrawable(Resource.Drawable.pause));
                        PreparePlayer(true);
                    }
                    else
                    {
                        //Otherwise, pause the music.
                        mediaPlayer.Pause();
                        playBtn.SetImageDrawable(GetDrawable(Resource.Drawable.play));
                    }
                }
                else
                {
                    //else plays the music.
                    playBtn.SetImageDrawable(GetDrawable(Resource.Drawable.pause));

                    //If the music was previously in a paused state.
                    if (mediaPlayer.CurrentPosition != 0 && currentPlayingSong == displayedSong)
                    {
                        mediaPlayer.Start();
                        headerTitle.Text = "Now playing~ " + Utils.Truncate(currentPlayingSong.GetSongTitle(), 5) + "..";
                    }
                    else
                    {
                        PreparePlayer(true);
                    }
                }
            }
            else
            {
                //Called mainly once (When app launches and first time playing.)
                PreparePlayer(true);
                playBtn.SetImageDrawable(GetDrawable(Resource.Drawable.pause));
            }
        }
Exemplo n.º 3
0
        private void PreparePlayer(bool playMusic = false)
        {
            try
            {
                //Clears the current seekbar thread first if its running.
                if (musicThread.IsAlive)
                {
                    ThreadHandler(1);
                }

                //Flushs the mediaplayer.
                if (mediaPlayer != null)
                {
                    //If the player is playing.
                    if (mediaPlayer.IsPlaying)
                    {
                        //Stops the player first.
                        mediaPlayer.Stop();
                    }
                    mediaPlayer.Reset();
                    mediaPlayer.Release();
                }
                //Set the mediaplayer source.
                mediaPlayer = new MediaPlayer();
                mediaPlayer.SetDataSource(displayedSong.GetFileDirectory().Path);
                mediaPlayer.Prepare();

                if (playMusic)
                {
                    mediaPlayer.Start();
                    currentPlayingSong = displayedSong;
                    Utils.ShowToast(this, "Now playing: " + displayedSong.GetSongTitle());
                    headerTitle.Text        = "Now playing~ " + Utils.Truncate(currentPlayingSong.GetSongTitle(), 5) + "..";
                    mediaPlayer.Completion += MediaPlayerOnCompletion;
                    ThreadHandler(0);
                }
            } catch (Exception p) {
                //For debug purpose.
                Utils.ShowToast(this, "ERROR: " + p.Message, false, ToastLength.Long);
            }
        }
Exemplo n.º 4
0
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.Album);
            // Create your application here

            var startPause      = FindViewById <Button>(Resource.Id.startpause);
            var nextSong        = FindViewById <Button>(Resource.Id.nextSong);
            var previousSong    = FindViewById <Button>(Resource.Id.previousSong);
            var songProgressBar = FindViewById <SeekBar>(Resource.Id.songProgressBar);

            startPause.Click += delegate
            {
                if (Player.IsPlaying)
                {
                    Player.Pause();
                }
                else
                {
                    Player.Start();
                }
            };
            nextSong.Click += delegate
            {
                NextSong(songProgressBar);
            };
            previousSong.Click += delegate
            {
                var nextSongIndex = _album.Songs.IndexOf(_currentSong) - 1;

                if (nextSongIndex < 0)
                {
                    Player.Reset();
                    _currentSong = null;
                }
                else
                {
                    _currentSong = _album.Songs[nextSongIndex];
                    Player.Reset();
                    var uri = Uri.Parse(_currentSong.SongPath);
                    Player.SetAudioStreamType(Stream.Music);
                    Player.SetDataSource(ApplicationContext, uri);
                    Player.Prepare();
                    Player.Start();
                    songProgressBar.Max      = Player.Duration;
                    songProgressBar.Progress = 0;
                }
            };


            Title = Intent.GetStringExtra("AlbumName") ?? "";
            if (Title != "")
            {
                _album = ApplicationData.Albums.SingleOrDefault(a => a.Name == Title);
            }

            if (_album == null)
            {
                return;
            }

            var layout = FindViewById <LinearLayout>(Resource.Id.linearSongsLayout);

            for (int i = 0; i < _album.Songs.Count; i++)
            {
                var button = new Button(this)
                {
                    Text = $"{i+1} {_album.Songs[i].Title}",
                    Id   = i
                };
                button.Click += (sender, args) =>
                {
                    _currentSong = _album.Songs[button.Id];

                    if (_currentSong != null)
                    {
                        Player.Reset();
                        var uri = Uri.Parse(_currentSong.SongPath);
                        Player.SetAudioStreamType(Stream.Music);
                        Player.SetDataSource(ApplicationContext, uri);
                        Player.Prepare();
                        Player.Start();
                        songProgressBar.Max      = Player.Duration;
                        songProgressBar.Progress = 0;
                    }
                };

                layout.AddView(button);
            }


            var progressText = FindViewById <TextView>(Resource.Id.progressText);

            songProgressBar.StopTrackingTouch += delegate
            {
                progressText.Text = $"{songProgressBar.Progress}";
                Player.SeekTo(songProgressBar.Progress);
            };

            SetCountDown();

            Player.Completion += delegate
            {
                NextSong(songProgressBar);
            };
        }