public static async Task <bool> DownloadSong(string videoId, string fileName) { fileName = AndroidSongsManager.Instance.CorrectSongNameForSave(fileName); HttpResponseMessage songStream = await YoutubeApiClient.downloadStream(videoId); if (songStream.StatusCode == HttpStatusCode.OK) { return(await AndroidSongsManager.Instance.SaveSong(FileManager.PATH, fileName, videoId, await songStream.Content.ReadAsStreamAsync())); } return(false); }
async Task UpdateVideos(string searchQuery) { ProgressDialog progress = new ProgressDialog(this); progress.SetMessage("loading..."); try { if (searchQuery == string.Empty) { if (YoutubeApiClient.MostPopularSongs == null) { progress.Show(); await YoutubeApiClient.Search(); } List <SearchResult> originalResults = YoutubeApiClient.MostPopularSongs; videos = Common.GetSearchResultsItems(originalResults.ToArray()); } else { progress.Show(); List <SearchResult> originalResults = await YoutubeApiClient.Search(searchQuery); videos = Common.GetSearchResultsItems(originalResults.ToArray()); } if (videos != null) { images = await LoadImages(videos.ToArray()); await LoadListView(); } else { Toast.MakeText(this, "Didn't find results", ToastLength.Long).Show(); } } catch (Exception ex) { GoogleAnalyticsService.Instance.TrackAppException(ex.Message, false); Toast.MakeText(this, "Could not connect to Youtube", ToastLength.Long).Show(); } finally { progress.Dismiss(); } }
protected override void OnResume() { base.OnResume(); Task.Run(async() => { try { await YoutubeApiClient.Search(); } catch (Exception ex) { GoogleAnalyticsService.Instance.TrackAppException(ex.Message, true); RunOnUiThread(new Runnable( () => Toast.MakeText(Application.Context, "Could not connect, please check your internet connection", ToastLength.Long).Show())); }; }); if (ContextCompat.CheckSelfPermission(this, Manifest.Permission.WriteExternalStorage) == Permission.Granted && ContextCompat.CheckSelfPermission(this, Manifest.Permission.ReadExternalStorage) == Permission.Granted) { StartActivity(new Intent(Application.Context, typeof(SongsPlayer))); } }
protected async override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); SetContentView(Resource.Layout.activity_search_songs); GoogleAnalyticsService.Instance.Initialize(this); GoogleAnalyticsService.Instance.TrackAppPage("Search Song"); myVideosListView = FindViewById <ListView>(Resource.Id.songsListView); ImageButton searchButton = FindViewById <ImageButton>(Resource.Id.searchBtn); searchString = FindViewById <AutoCompleteTextView>(Resource.Id.searchEditText); clearBtn = FindViewById <ImageButton>(Resource.Id.clearBtn); searchString.Text = string.Empty; searchString.Background.SetTint(ContextCompat.GetColor(this, Resource.Color.darkassets)); DownloadWatcher.onDownloaded += async(sender, e) => await LoadListView(); DownloadWatcher.onDownloadFailed += async(sender, e) => await LoadListView(); searchButton.Click += async delegate { HideKeyboard(searchButton.Context); await UpdateVideos(searchString.Text); }; searchString.KeyPress += async(object sender, View.KeyEventArgs e) => { e.Handled = false; if (e.Event.Action == KeyEventActions.Down && e.KeyCode == Keycode.Enter) { HideKeyboard(searchString.Context); await UpdateVideos(searchString.Text); e.Handled = true; } }; searchString.TextChanged += async(sender, e) => { if (searchString.Text.Length > 0) { clearBtn.Visibility = ViewStates.Visible; if (checkInternetConnection()) { IEnumerable <string> songs = await YoutubeApiClient.SearchTitles(searchString.Text); ArrayAdapter autoCompleteAdapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleDropDownItem1Line, songs.ToList()); searchString.Adapter = autoCompleteAdapter; } } else { clearBtn.Visibility = ViewStates.Gone; } }; clearBtn.Click += (s, e) => { searchString.Text = string.Empty; }; myVideosListView.ItemClick += (sender, e) => { selectedVideo = videos[e.Position]; Intent intent = new Intent(this, typeof(DownloadSong)); StartActivity(intent); }; await UpdateVideos(string.Empty); }