英文:
Unable to stream audio and play it with Plugin.Maui.Audio
问题
我正在创建一个可以在Android和Windows上正常播放本地文件的Maui .Net 7应用程序,使用Plugin.Maui.Audio。对于本地文件,以下代码是正确的。
var player = AudioManager.Current.CreatePlayer(await FileSystem.OpenAppPackageFileAsync("audio_file_name.mp3"));
player.Play();
问题是我想播放托管在Web服务器或某个远程位置的音频文件。我已经通过将一些测试文件上传到个人网络托管帐户并直接链接到文件,以及设置Azure Blob存储并尝试从那里播放音频来进行测试。无论我尝试什么,都无法播放音频文件。没有任何反应,也没有错误。我尝试了以下两种方法来在应用程序中播放托管在远程位置的文件。即使我按播放并等待几分钟才开始播放,仍然没有发生任何事情。
#1. 直接从远程源流式传输:
var player = AudioManager.Current.CreatePlayer("https://link/to/file.mp3");
player.Play();
#2. 首先下载文件,然后再播放它:
public async Task DownloadFile(string url, string filePath)
{
var client = new HttpClient();
var response = await client.GetAsync(url);
var stream = await response.Content.ReadAsStreamAsync();
var fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None);
await stream.CopyToAsync(fileStream);
}
var localFilePath = Path.Combine(FileSystem.CacheDirectory, "file.mp3");
await DownloadFile("https://link/to/file.mp3", localFilePath);
var player = AudioManager.Current.CreatePlayer(localFilePath);
player.Play();
查看Plugin.Maui.Audio,它应该能够播放流,而不仅仅是本地文件。我不确定我在这里做错了什么。有没有任何建议可以使这个Maui应用程序正确运行?
英文:
I'm creating a Maui .Net 7 application that can play audio files and works well for local files on Android and Windows using Plugin.Maui.Audio. For local files, the below code works correctly.
var player = AudioManager.Current.CreatePlayer(await FileSystem.OpenAppPackageFileAsync("audio_file_name.mp3"));
player.Play();
The problem is that I want to play audio files that will be hosted on a web server or some remote location. I've tested it by uploading a few test files to a personal web hosting account and linking directly to the file, finding a test audio file hosted elsewhere https://www.learningcontainer.com/wp-content/uploads/2020/02/Kalimba.mp3
, And also setup Azure Blob storage and tried playing audio from there. Still, no matter what I try, the audio file does not play. Nothing happens. No errors. I tried the below 2 ways of having a remotely hosted file play in the app. Even if I press play and wait minutes for it to start playing, nothing happens.
#1. Streaming directly from the remote source:
var player = AudioManager.Current.CreatePlayer("https://link/to/file.mp3");
player.Play();
#2 Downloading the file first and then playing it:
public async Task DownloadFile(string url, string filePath)
{
var client = new HttpClient();
var response = await client.GetAsync(url);
var stream = await response.Content.ReadAsStreamAsync();
var fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None);
await stream.CopyToAsync(fileStream);
}
var localFilePath = Path.Combine(FileSystem.CacheDirectory, "file.mp3");
await DownloadFile("https://link/to/file.mp3", localFilePath);
var player = AudioManager.Current.CreatePlayer(localFilePath);
player.Play();
Looking into Plaugin.Maui.Audio, It's supposed to be able to play a Stream as well and not just local files. I'm not sure what I'm doing wrong here. Any suggestions for having this work correctly for a Maui application?
答案1
得分: 1
> 应该可以播放流,而不仅仅是本地文件。
在 IAudioManager 的源代码中提供了以下方法。
IAudioPlayer CreatePlayer(Stream audioStream)
{
return new AudioPlayer(audioStream);
}
但正如 jdweng 所说,应用程序应该使用文件扩展名和 ASCII 标头来测试文件类型,我不确定 HTTP 响应是否能被识别为流。
实际上,你可以使用 MediaElement 来通过 HTTP 和 HTTPS URI 方案播放音频。以下是代码示例:
<toolkit:MediaElement Source="https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4"
ShouldShowPlaybackControls="True" />
这个 MediaElement 属于 .NET MAUI Community Toolkit。
英文:
> It's supposed to be able to play a Stream as well and not just local
> files.
In the source code of the IAudioManager it provide the method.
IAudioPlayer CreatePlayer(Stream audioStream)
{
return new AudioPlayer(audioStream);
}
but just as jdweng said that Applications are suppose to test the file type using both extension and ASCII Header
I am not sure that the https response can be recognized as a stream.
Acturaly, you can use the MediaElement to play the audio by using the HTTP and HTTPS URI schemes. Here is the code sample:
<toolkit:MediaElement Source="https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4"
ShouldShowPlaybackControls="True" />
This MediaElement belongs to the .NET MAUI Community Toolkit.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论