Alan loves listening to music with you
private async Task PlayMusic()
{
StorageFile song = await Package.Current.InstalledLocation.GetFileAsync(@"Music\" + songs_list[song_idx]);
// Switch from default grid to media player grid
if(state == AssistantState.LISTENING_DEFAULT)
{
await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
main_page.SwitchToMediaPlayerGrid();
});
}
await ChangeMediaPlayerVolume(0.35 * 100);
media_player.AutoPlay = true;
media_player.SetFileSource(song);
state = AssistantState.BACKGROUND_MEDIA_PLAYER;
}
private async Task MediaPlayerTasksList(string command)
{
if(command == "close")
{
media_player.Pause();
song_idx = 0;
await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
main_page.SwitchToDefaultGrid();
});
state = AssistantState.BACKGROUND_DEFAULT;
}
else if(command == "stop")
{
if(media_player.CurrentState == MediaPlayerState.Paused)
await GUIOutput("The music has already been stopped.", true);
else
media_player.Pause();
state = AssistantState.BACKGROUND_MEDIA_PLAYER;
}
else if(command == "play")
{
if(media_player.CurrentState == MediaPlayerState.Playing)
await GUIOutput("The music is already playing.", true);
else
media_player.Play();
state = AssistantState.BACKGROUND_MEDIA_PLAYER;
}
else if(command == "volume up")
{
if(media_player_volume <= 0.8)
{
media_player_volume += 0.2;
ChangeMediaPlayerVolume(media_player_volume * 100);
}
state = AssistantState.BACKGROUND_MEDIA_PLAYER;
}
else if(command == "volume down")
{
if(media_player_volume >= 0.2)
{
media_player_volume -= 0.2;
ChangeMediaPlayerVolume(media_player_volume * 100);
}
state = AssistantState.BACKGROUND_MEDIA_PLAYER;
}
else if(command == "next song")
{
media_player.Pause();
if(song_idx + 1 >= songs_list.Count)
song_idx = 0;
else
song_idx++;
await PlayMusic();
}
else if(command == "previous song")
{
media_player.Pause();
if(song_idx - 1 < 0)
song_idx = songs_list.Count - 1;
else
song_idx--;
await PlayMusic();
}
else
{
await GUIOutput("I'm sorry, I couldn't understand.", true);
state = AssistantState.BACKGROUND_MEDIA_PLAYER;
}
}
private async Task CreateSongsList()
{
StorageFolder installed_location = Package.Current.InstalledLocation;
StorageFolder music_folder = await installed_location.GetFolderAsync(@"Music\");
IReadOnlyList<StorageFile> files = await music_folder.GetFilesAsync();
foreach(StorageFile file in files)
if(file.Name.Contains(".wma") || file.Name.Contains(".mp3"))
songs_list.Add(file.Name);
song_idx = 0;
}
private KeyValuePair<string, string> GetSongInfos(int idx)
{
string title, artist, temp;
int i = songs_list[idx].LastIndexOf('.');
temp = songs_list[idx].Remove(i);
i = temp.LastIndexOf('-');
if(i > 0)
{
title = temp.Remove(i);
artist = temp.Remove(0, i + 1);
}
else
{
title = temp;
artist = "";
}
return new KeyValuePair<string, string>(title, artist);
}
private void MediaPlayerMediaEnded(MediaPlayer sender, object args) { NextSong(); }
// Slider volume control
public void SetMediaPlayerVolume(double value) { media_player.Volume = value / 100.0; }
// Media player Play button control
public void MediaPlayerPlay()
{
if(media_player.CurrentState != MediaPlayerState.Playing)
media_player.Play();
}
// Media player Close button control
public async Task MediaPlayerClose()
{
media_player.Pause();
song_idx = 0;
await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
main_page.SwitchToDefaultGrid();
});
state = AssistantState.BACKGROUND_DEFAULT;
}
// Media player Stop button control
public void MediaPlayerStop()
{
if(media_player.CurrentState == MediaPlayerState.Playing)
media_player.Pause();
}
// Media player Next button control
public async Task NextSong()
{
media_player.Pause();
if(song_idx + 1 >= songs_list.Count)
song_idx = 0;
else
song_idx++;
await PlayMusic();
}
// Media player Previous button control
public async Task PreviousSong()
{
media_player.Pause();
if(song_idx - 1 < 0)
song_idx = songs_list.Count - 1;
else
song_idx--;
await PlayMusic();
}
private void MediaPlayerCurrentStateChanged(MediaPlayer sender, object args)
{
if(media_player.CurrentState == MediaPlayerState.Playing)
{
KeyValuePair<string, string> song_infos = GetSongInfos(song_idx);
ChangeMediaPlayerText(song_infos.Key, song_infos.Value);
}
else if(media_player.CurrentState == MediaPlayerState.Paused)
ChangeMediaPlayerText("Paused", "");
else if(media_player.CurrentState == MediaPlayerState.Buffering)
ChangeMediaPlayerText("Loading media", "");
}
As soon as the application is started, the function
CreateSongList
is called and creates a list of string containing all the filenames
of the audio files in the application's music folder. When the command "Play some music"
is given to the assistant, the function PlayMusic
is called: this
function opens the first file in the song list, changes the layout of the UI and then
starts the reproduction of the song. When reproducing a song the assistant is in the state
BACKGROUND_MEDIA_PLAYER
and responds to a different set of commands, both
physical (buttons and slider) and vocal:
- Play: starts the reproduction of the current song.
- Stop: pauses the reproduction of the current song; we used the word "stop" instead of "pause" because the latter was very difficult to comprehend correctly for the speech recognizer.
- Close: closes the media player and returns to default functionalities.
- Volume up: increases the volume of the current song.
- Volume down: decreases the volume of the current song.
- Next song: starts the reproduction of the next song in the list (if bottom of the list is reached, it selects the first one).
- Previous song: starts the reproduction of the previous song in the list (if top of the list is reached, it selects the last one).