Exemple #1
0
        private async void execMethod(string cmd, string obj)
        {
            if (cmd == "open" && !SpotifyLocalAPI.IsSpotifyRunning()) // Start Spotify
            {
                Console.WriteLine("Abrir Spotify");
                SpotifyLocalAPI.RunSpotify();
                SpotifyLocalAPI.RunSpotifyWebHelper();
                Connect();
                AuthConnect();
                t.Speak("O que queres ouvir?");
            }

            if (SpotifyLocalAPI.IsSpotifyRunning())
            {
                if (cmd == "pause") // Pausar
                {
                    Console.WriteLine("Pausa");
                    await _spotify.Pause();

                    t.Speak("Estarei aqui à tua espera.");
                }

                if (cmd == "play") // Retomar
                {
                    Console.WriteLine("Retomar");
                    await _spotify.Play();
                }

                if (cmd == "mute") // Silenciar
                {
                    Console.WriteLine("Silenciar");
                    _spotify.Mute();
                    t.Speak("Já podes atender.");
                }

                if (cmd == "unmute") // Volume
                {
                    Console.WriteLine("Volume");
                    _spotify.UnMute();
                }


                if (cmd == "volumeUp") // Aumentar volume
                {
                    Console.WriteLine("Aumentar volume");
                    _spotify.SetSpotifyVolume(PostureVerify.getVol() + 25);
                }

                if (cmd == "volumeDown") // Diminuir volume
                {
                    Console.WriteLine("Dimiuir volume");
                    _spotify.SetSpotifyVolume(PostureVerify.getVol() - 25);
                }

                if (cmd == "close") // Fechar Spotify
                {
                    Console.WriteLine("Fechar");
                    SpotifyLocalAPI.StopSpotify();
                    t.Speak("Obrigado pela tua preferência. Até à próxima!");
                }

                if (cmd == "createList") // Criar lista de reprodução
                {
                    String name = obj;
                    Console.WriteLine("Criar lista de reprodução");
                    FullPlaylist playlist = _spotifyWeb.CreatePlaylist(id, name);
                    if (!playlist.HasError())
                    {
                        Console.WriteLine("Playlist-URI:" + playlist.Uri);
                    }

                    t.Speak("Lista de reprodução " + name + " criada com sucesso");
                }

                if (cmd == "addList") // Adicionar à lista de reprodução
                {
                    String name = obj;
                    Console.WriteLine("Adicionar à lista de reprodução");
                    String uri = _spotify.GetStatus().Track.TrackResource.Uri;
                    List <SimplePlaylist> playlist = _spotifyWeb.GetUserPlaylists(id).Items;
                    int index = playlist.FindIndex(x => x.Name == name);
                    _spotifyWeb.AddPlaylistTrack(id, _spotifyWeb.GetUserPlaylists(id).Items[index].Id, uri);
                    t.Speak("Música adicionada com sucesso!");
                }

                if (cmd == "removeList") // Remover música da lista de reprodução
                {
                    Console.WriteLine("Remover música da lista de reprodução");
                    String uri      = _spotify.GetStatus().Track.TrackResource.Uri;
                    String playlist = _spotifyWeb.GetUserPlaylists(id).Items[0].Id;
                    _spotifyWeb.RemovePlaylistTrack(id, playlist, new DeleteTrackUri(uri));
                    t.Speak("Música removida!");
                }

                if (cmd == "identify") // Reconhecer música actual
                {
                    Console.WriteLine("Não me lembro do nome desta música");
                    String name = _spotify.GetStatus().Track.TrackResource.Name;
                    t.Speak("A música que estás a ouvir é" + name);
                }


                if (cmd == "playMusic") // Ouvir música
                {
                    SearchType search_type = SearchType.Track;
                    SearchItem item;
                    String     musica;

                    musica = obj;
                    Console.WriteLine("Ouvir " + musica + "!");
                    item = _spotifyWeb.SearchItems(musica, search_type, 1, 0, "pt");
                    await _spotify.PlayURL(item.Tracks.Items[0].Uri, "");
                }

                if (cmd == "playAlbum") // Ouvir álbum
                {
                    SearchType search_type = SearchType.Album;
                    SearchItem item;
                    String     album;

                    album = obj;
                    Console.WriteLine("Ouvir " + album + "!");
                    item = _spotifyWeb.SearchItems(album, search_type, 1, 0, "pt");
                    await _spotify.PlayURL(item.Albums.Items[0].Uri, "");
                }

                if (cmd == "playArtist") // Ouvir artista
                {
                    SearchType search_type = SearchType.Track;
                    SearchItem item;
                    String     artist;

                    artist = obj;
                    Console.WriteLine("Ouvir " + artist + "!");
                    item = _spotifyWeb.SearchItems(artist, search_type, 1, 0, "pt");
                    await _spotify.PlayURL(item.Artists.Items[0].Uri, "");
                }
            }
            else
            {
                t.Speak("Spotify não está aberto.");
            }
        }
Exemple #2
0
        private void SpeechRecognized2(object sender, SpeechRecognizedEventArgs e)
        {
            //gets recognized text
            string text   = e.Result.Text;
            string tagCmd = e.Result.Semantics["action"].Value != null ? e.Result.Semantics["action"].Value.ToString() : "";
            string tagObj = e.Result.Semantics["object"].Value != null ? e.Result.Semantics["object"].Value.ToString() : "";

            double confid = e.Result.Confidence;

            //actualizar comando a executar

            if (state != 1)
            {
                if (isCommandValid(tagCmd, tagObj))
                {
                    command = tagCmd;
                }
                else
                {
                    command = "";
                }
            }

            if (confid < 0.33)
            {
                if (state != 1)
                {
                    state = 0;
                }
                t.Speak("Não percebi, diz outra vez.");
                Console.WriteLine("Não percebi, diz outra vez.");
            }
            else if (confid >= 0.34 && confid < 0.66)
            {
                t.Speak("Tem a certeza que quer executar o comando " + text + "?");
                Console.WriteLine("Tem a certeza que quer executar o comando " + text + "?");
                state = 1;
            }
            else
            {
                if (state != 1)
                {
                    state = 2;
                }
            }

            if (state == 2)
            {
                if (PostureVerify.getPosture() && (command == "volumeUp" || command == "volumeDown"))
                {
                    execMethod(command, tagObj);
                    PostureVerify.setPosture(false);
                }

                else
                {
                    if (command != "volumeUp" && command != "volumeDown")
                    {
                        execMethod(command, tagObj);
                    }
                    PostureVerify.setPosture(false);
                }
            }

            else if (state == 1 && confid > 0.66)
            {
                if (text == "sim")
                {
                    state = 3;
                    if (PostureVerify.getPosture() && (command == "volumeUp" || command == "volumeDown"))
                    {
                        execMethod(command, tagObj);
                        PostureVerify.setPosture(false);
                    }

                    else
                    {
                        if (command != "volumeUp" && command != "volumeDown")
                        {
                            execMethod(command, tagObj);
                        }
                        PostureVerify.setPosture(false);
                    }
                }
                else if (text == "não")
                {
                    t.Speak("Então volta a repetir o comando por favor.");
                    state = 3;
                }
            }

            Console.WriteLine("Comando: " + text);
        }