无法使用 Plugin.Maui.Audio 流式传输音频并播放。

huangapple go评论137阅读模式
英文:

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:

&lt;toolkit:MediaElement Source=&quot;https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4&quot;
              ShouldShowPlaybackControls=&quot;True&quot; /&gt;

This MediaElement belongs to the .NET MAUI Community Toolkit.

huangapple
  • 本文由 发表于 2023年2月26日 20:10:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75571876.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定