Xamarin Android C# 播放音频流(在线广播)

编程入门 行业动态 更新时间:2024-10-28 20:24:42
本文介绍了Xamarin Android C# 播放音频流(在线广播)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个在线广播的 URL,我想播放它.所以,我是这样做的:读取流并将其写入字节数组并使用 audioTrack 类播放.代码如下:

I have a URL of online-radio and I'd like to play it. So, I've done it this way: reading stream and writing it to byte array and playing it with audioTrack class. Here's the code:

private static async Task Play() { using (WebClient wcDownload = new WebClient()) { // Create a request to the file we are downloading WebRequest webRequest = (HttpWebRequest)WebRequest.Create("media.vmariel.ru:8000/puls"); // Set default authentication for retrieving the file webRequest.Credentials = CredentialCache.DefaultCredentials; // Retrieve the response from the server WebResponse webResponse = (HttpWebResponse)webRequest.GetResponseAsync().Result; // Ask the server for the file size and store it Int64 fileSize = webResponse.ContentLength; // Open the URL for download System.IO.Stream strResponse = wcDownload.OpenRead(new System.Uri("media.vmariel.ru:8000/puls")); // It will store the current number of bytes we retrieved from the server int bytesSize = 0; // A buffer for storing and writing the data retrieved from the server byte[] downBuffer = new byte[131072]; // Loop through the buffer until the buffer is empty AudioTrack audioTrack = new AudioTrack( // Stream type Android.Media.Stream.Music, // Frequency 48000, // Mono or stereo ChannelConfiguration.Stereo, // Audio encoding Android.Media.Encoding.Pcm16bit, // Length of the audio clip. downBuffer.Length, // Mode. Stream or static. AudioTrackMode.Stream); audioTrack.Play(); while ((bytesSize = strResponse.Read(downBuffer, 0, downBuffer.Length)) > 0) { await audioTrack.WriteAsync(downBuffer, 0, downBuffer.Length); } } }

但这样我只能听到噪音,听不到音乐.

but this way I can hear only noise, no music.

推荐答案

您可以使用 MediaPlayer 类通过提供 URL 来流式传输音频.

You can use the MediaPlayer class to stream audio by providing a URL.

MediaPlayer player = new MediaPlayer(); player.SetAudioStreamType (Stream.Music); player.SetDataSource ("media.vmariel.ru:8000/puls"); player.Prepare(); player.Start();

更多推荐

Xamarin Android C# 播放音频流(在线广播)

本文发布于:2023-07-11 12:39:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1094097.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:在线   音频   Xamarin   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!