【发布时间】:2016-07-08 18:39:46
【问题描述】:
我阅读了很多关于这个问题的文档,但我没有看到任何答案。
我有一个 Xamarin 表单应用程序,可以播放来自 Groove 音乐服务的 mp3 示例音乐。
在 Android 上,一切正常。 在 Ios 上,我无法播放声音。我得到了 URL,但听不到任何声音(我可以播放本地 mp3)
我发现使用 SWIFT 的人也出现了这个问题:How to play MP3 From URL in iOS
我还在 Xamarin 论坛上找到了几个例子,但都没有声音:https://forums.xamarin.com/discussion/19883/how-do-i-get-the-avplayer-to-play-an-mp3-from-a-remote-url
这是我使用的代码:
公共类 AudioService : IAudio { 受保护的字符串 FileName = string.Empty; 受保护的 AVPlayer 播放器;
public bool IsPlaying { get; private set; }
public void Init(string fileName)
{
//FileName = fileName;
string second = "http://progdownload.zune.net/145/119/034/170/audio.mp3?rid=0b80911a-ba3b-42ec-b17f-c242ba087024-s4-nl-BE-music-asset-location";
FileName = second;
QueueFile();
}
public async void PlayStream(string uri)
{
Init(uri);
System.Diagnostics.Debug.WriteLine("Enter in function");
Player.Play();
System.Diagnostics.Debug.WriteLine("Play sound");
System.Diagnostics.Debug.WriteLine(FileName);
IsPlaying = true;
System.Diagnostics.Debug.WriteLine(IsPlaying);
}
public void Pause()
{
IsPlaying = false;
Player.Pause();
}
public void Stop()
{
IsPlaying = false;
if (Player == null) return;
Player.Dispose();
Player = null;
}
public bool HasFile
{
get { return Player != null; }
}
private void QueueFile()
{
if (string.IsNullOrWhiteSpace(FileName))
{
throw new Exception("No file specified to play");
}
using (var url = NSUrl.FromString(FileName))
{
var test = AVAsset.FromUrl(url);
var playerItem = AVPlayerItem.FromAsset(test);
// if Player is null, we're creating a new instance and seeking to the spot required
// otherwise we simply resume from where we left off.
if (Player == null)
{
Player = AVPlayer.FromPlayerItem(playerItem);
if (Player == null)
{
// todo: what should we do if the file doesn't exist?
return;
}
}
}
}}
(IAudio 只是实现了播放流和停止)
如果您点击该网址,您的浏览器将能够播放音乐 http://progdownload.zune.net/145/119/034/170/audio.mp3?rid=0b80911a-ba3b-42ec-b17f-c242ba087024-s4-nl-BE-music-asset-location
有没有人可以在网上播放mp3
【问题讨论】:
标签: c# ios audio xamarin.ios xamarin.forms