Xamarin IOS:无法从互联网播放 mp3
Posted
技术标签:
【中文标题】Xamarin IOS:无法从互联网播放 mp3【英文标题】:Xamarin IOS: unable to play mp3 from internet 【发布时间】: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
【问题讨论】:
【参考方案1】:info.list 文件中的答案。您需要将此添加到您的 info.list:
<key>NSAppTransportSecurity</key><dict>
<key>NSAllowsArbitraryLoads</key>
<true/></dict>
我遇到了同样的问题并解决了。
【讨论】:
想知道它对我有用,谢谢@Thando Toto先生【参考方案2】:我已经为您的代码做了一些清理工作 :) 这就是我们得到的,包括检测播放结束
public class AudioService
#region Members
protected AVPlayer Player;
#endregion
#region Properties
public bool IsPlaying get; private set;
public bool HasFile => Player != null;
public Action MediaEnded get; set;
#endregion
#region Public Methods
public void PlayStream(string uri)
try
if (Player != null)
Player.Dispose();
Player = null;
QueueFile(uri);
Player.Play();
IsPlaying = true;
catch (Exception ex)
IsPlaying = false;
Crashes.TrackError(ex);
public void Pause()
IsPlaying = false;
Player.Pause();
public void Stop()
IsPlaying = false;
if (Player == null)
return;
Player.Dispose();
Player = null;
public void QueueFile(string fileName)
if (string.IsNullOrWhiteSpace(fileName))
return;
using (var url = NSUrl.FromString(fileName))
var asset = AVAsset.FromUrl(url);
var playerItem = AVPlayerItem.FromAsset(asset);
if (Player == null)
Player = AVPlayer.FromPlayerItem(playerItem);
if (Player == null)
return;
else
NSNotificationCenter.DefaultCenter.AddObserver(AVPlayerItem.DidPlayToEndTimeNotification, OnPlayerDidFinishPlaying, NSObject.FromObject(playerItem));
#endregion
#region Private Methods
private void OnPlayerDidFinishPlaying(NSNotification notification)
IsPlaying = false;
MediaEnded?.Invoke();
#endregion
【讨论】:
以上是关于Xamarin IOS:无法从互联网播放 mp3的主要内容,如果未能解决你的问题,请参考以下文章
来自服务器的 MP3 文件在 iOS7 中无法使用 AVAudioPlayer 播放
xamarin ios html5 video.js 无法播放
Ionic 媒体插件无法在 iOS 上播放本地存储的 mp3 文件