Listen to a playlist with MediaElement
Wouldn’t the app be much nicer if you could listen to more songs? Well that’s ideal because today we’re integrating a music playlist!
Playlist creation
To do this, let’s define the list of songs available in the application as follows:
Filename:MusicPlayerViewModel.cs
|
|
There’s nothing extraordinary here, apart from the usefulness of our Model!
The number of songs is limited to 5 for the purposes of this course, but imagine if we allowed the user to explore titles from all over the world!
The logic we’re going to implement consists in moving from one track to another in our playlist
. It’s a bit like a pointer sliding over the cells of an array of MusicTrack
elements to define the next piece of music to be played.
To do this, we’ll enhance the logic around the CurrentTrack
property introduced in the previous chapter with the following code:
Filename:MusicPlayerViewModel.cs
|
|
We’ve added a new property called currentTrackPosition
, which corresponds in some way to our pointer mentioned above. It will define the position of the song to be played in the playlist
, thanks to this new attribute: NotifyPropertyChangedFor().
In concrete terms, each time the value of the currentTrackPosition
property changes, this attribute will trigger a notification to the View informing that the CurrentTrack
property has just been updated.
Indeed, the CurrentTrack
property has been adapted to return a music track from the playlist
at the requested position (playlist[CurrentTrackPosition]
).
CurrentTrack
from the MusicPlayerViewModel constructor! So you can delete this bit of code.Have you noticed the initial value of the currentTrackPosition
property? It’s equal to 0, which corresponds to the first position in the playlist
array, which is none other than the song “Baila” played from the chapter n°10.
But let’s move on quickly, because we want to hear other tracks!
Binding components
Spoiler alert: we’re now going to tackle the final dummy components of the app!
If you remember the chapter on displaying playback controls, we still have 3 ImageButton components to implement:
SkipNextButton
to skip to the next song,SkipPreviousButton
to skip to the previous song,et
RepeatOnceButton
to replay the music track (once only).
Moving on to the next song is easy. Start by adding the following command to the ViewModel:
Filename:MusicPlayerViewModel.cs
|
|
Then associate the new command with the SkipNextButton
from the View :
Filename:MusicPlayerView.cs
|
|
The behavior defined in the GoToNextTrack() method consists of simply moving the pointer’s position one step forward on the array. Of course, when last item of the array is reached, we return to the beginning of the playlist
.
Let’s try it out!
I’ll explain how. For now, add the following command to the ViewModel:
Filename:MusicPlayerViewModel.cs
|
|
The behavior defined in the GoToPreviousTrack() method consists of simply moving the pointer’s position one step backward on the array. Of course, when the first cell of the array is reached, we switch to the last song in the playlist.
But did you notice the if
condition? You can only return to the previous song if the current song has been playing for less than 2 seconds.
TimeTracker.Value
as a parameter to the command to be associated with the SkipPreviousButton
.To do this, apply the following changes to the View:
Filename:MusicPlayerView.cs
|
|
We still use BindCommand(), but we use two additional parameters for the purposes of our GoToPreviousTrack() method:
The information to be passed as a parameter (
parameterPath
) is theTimeTracker.Value
corresponding to the elapsed playback time,And the source of this property (
parameterSource
) is, of course, theTimeTracker
.
Finally, if you need to restart the song, you can use the OnPropertyChanged() method. This will trigger a notification to the View informing that the CurrentTrack
property has just been updated (even though its value hasn’t changed at all!).
And that’s it! Let’s see what it looks like:
Well, that’s it! Now we just need to implement a button to repeat the song currently playing.
Replay music once again
This is the home stretch of the series, so hang in there!
To enable a song to be repeated, we must first add the following property to the ViewModel:
Filename:MusicPlayerViewModel.cs
|
|
This is a classic boolean whose state will be modified via its dedicated command :
Filename:MusicPlayerViewModel.cs
|
|
The idea is that each time the user clicks, we invert the state of the boolean to enable or disable repetition. All that remains is to associate this command with the RepeatOnceButton
from the View:
Filename:MusicPlayerView.cs
|
|
As you may have noticed, I’ve defined a color for the outline of the RepeatOnceButton
with the BorderColor
property. This is to improve the visual rendering when the repeat is activated!
To do this, we’ll use the MustRepeatCurrentTrackOnce
property. Modify the InitMediaControlPanel() method to apply the following changes:
Filename:MusicPlayerView.cs
|
|
With this code, we play with the thickness of the borders (BorderWidth
) of the RepeatOnceButton
, depending on whether the repeat option is activated or not. Indeed, by default, the border thickness is negative!
In other words, we won’t see any borders unless we change the BorderWidth
value ourselves.
This is why, in the convert, we define a positive value to show a colored contour when MustRepeatCurrentTrackOnce
is equal to True
.
MustRepeatCurrentTrackOnce
parameter to determine whether to restart the song when it’s over, or move on to the next song.And this logic will be defined in the ViewModel with this new public method:
Filename:MusicPlayerViewModel.cs
|
|
Depending on whether MustRepeatCurrentTrackOnce
is set to True
or False
, we either use again the OnPropertyChanged() technique we saw earlier, or move on to the next song with the GoToNextTrack() method.
By the way, we only want to repeat the song once! So don’t forget to disable repetition by setting the MustRepeatCurrentTrackOnce
property to False
.
Finally, to detect when the song ends, we’ll rely on the MediaEnded event exposed by the MediaElement, as follows:
Filename:MusicPlayerView.cs
|
|
And it’s when the event is triggered that you’ll ask to repeat the current song or move on to the next one:
Filename:MusicPlayerView.cs
|
|
To do this, we use the MusicPlayerView’s BindingContext
to call the AssessRepeatOrSkip() method we’ve just defined in the MusicPlayerViewModel.
It’s demo time! Recompile the project and check that the current song restarts after clicking on the repeat button:
Then let the song finish to check that you are moving on to the next one:
Conclusion
Well then, do you like the result?
You can now enjoy the app and listen to a few songs in a row, at the desired volume, listen to a few passages again, and even download the tracks! 😎
We’ll remedy that in the next post, and it’ll be the last in this series!!!
More articles in the series: