Hello,
Welcome to our Microsoft Q&A platform!
but I still can't figure out how to use the
await youtube.Videos.Streams.DownloadAsync(streamInfo, $"video.{streamInfo.Container}");
I open the DownloadAsync
method, I found we can add path to it like following code.
string folder = FileSystem.AppDataDirectory;
string path =Path.Combine(folder, "MAudio.mp4");
await youtube.Videos.Streams.DownloadAsync(streamInfo, path, null);
I change background code.
public AboutPage()
{
InitializeComponent();
btnDownload.Clicked += BtnDownload_Clicked;
}
private async void BtnDownload_Clicked(object sender, EventArgs e)
{
var youtube = new YoutubeClient();
var streamManifest = await youtube.Videos.Streams.GetManifestAsync(iptLink.Text);
if (btnVideo.IsChecked)
{
var streamInfo = streamManifest.GetMuxed().WithHighestVideoQuality();
if (streamInfo != null)
{
var stream = await youtube.Videos.Streams.GetAsync(streamInfo);
string folder = FileSystem.AppDataDirectory;
string path =Path.Combine(folder, "MAudio.mp4");
await youtube.Videos.Streams.DownloadAsync(streamInfo, path, null);
}
}
else if (btnAudio.IsChecked)
{
var streamInfo = streamManifest.GetAudioOnly().WithHighestBitrate();
if (streamInfo != null)
{
var stream = await youtube.Videos.Streams.GetAsync(streamInfo);
string folder = FileSystem.AppDataDirectory;
string path = Path.Combine(folder, "MAudio.mp3");
await youtube.Videos.Streams.DownloadAsync(streamInfo, path, null);
}
}
}
I set it path with Xamarin.Essentials: File System Helpers
https://learn.microsoft.com/en-us/xamarin/essentials/file-system-helpers?context=xamarin%2Fandroid&tabs=android
It store files in the internal storage. In the Android, it store /data/data/com.companyname.shellnavi/files/
================
Update====================
If you want to see the download file in your emulator and target framework is blew Android 10(Android 11 or later, Google do not allow store file in the external folder), you can store it in the external folder, add following permission and add android:requestLegacyExternalStorage="true"
to the application
tag.
<application android:label="ShellNavi.Android" android:theme="@style/MainTheme" android:requestLegacyExternalStorage="true"></application>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Then Add following code to request run-time permission. For testing, I add following code to OnCreate
method of MainActivity
normally
if (ContextCompat.CheckSelfPermission(this, Manifest.Permission.WriteExternalStorage) != (int)Permission.Granted)
{
RequestPermissions(new string[] { Manifest.Permission.ReadExternalStorage, Manifest.Permission.WriteExternalStorage }, 0);
}
If you want to store the file in download folder. You need to use dependenceService to the path of download folder.
Create an interface.
public interface IgetFile1
{
string getExternalFile();
}
Achieve it in android project .
[assembly: Dependency(typeof(getFileService))]
namespace ShellNavi.Droid
{
class getFileService : IgetFile1
{
public string getExternalFile()
{
string path= Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads).AbsolutePath;
return path;
}
}
}
Use it when you want to download .
private async void BtnDownload_Clicked(object sender, EventArgs e)
{
var youtube = new YoutubeClient();
var streamManifest = await youtube.Videos.Streams.GetManifestAsync(iptLink.Text);
if (btnVideo.IsChecked)
{
var streamInfo = streamManifest.GetMuxed().WithHighestVideoQuality();
if (streamInfo != null)
{
var stream = await youtube.Videos.Streams.GetAsync(streamInfo);
string folder= DependencyService.Get<IgetFile1>().getExternalFile();
// string folder = FileSystem.AppDataDirectory;
string path =Path.Combine(folder, "MAudio.mp4");
await youtube.Videos.Streams.DownloadAsync(streamInfo, path, null);
}
}
else if (btnAudio.IsChecked)
{
var streamInfo = streamManifest.GetAudioOnly().WithHighestBitrate();
if (streamInfo != null)
{
var stream = await youtube.Videos.Streams.GetAsync(streamInfo);
string folder = DependencyService.Get<IgetFile1>().getExternalFile();
// string folder = FileSystem.AppDataDirectory;
string path = Path.Combine(folder, "MAudio.mp3");
await youtube.Videos.Streams.DownloadAsync(streamInfo, path, null);
}
}
}
Best Regards,
Leon Lu
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.