所以,我的问题是,我试图用Xamarin.iOS和AudioFileStream和OutputAudioQueue类来流音频。我为PacketDecoded和PropertyFound事件添加了处理程序,但它们没有被触发。怎么了?我的密码在下面。
class AudioStreamer : IAudioStreamer // this is my dependency service interface
{
bool outputStarted;
AudioFileStream afs;
OutputAudioQueue oaq;
public void StartStreaming(string url)
{
afs = new AudioFileStream(AudioFileType.MP3);
// event handlers, these are never triggered
afs.PacketDecoded += OnPacketDecoded;
afs.PropertyFound += OnPropertyFound;
GetAudio(url);
}
void GetAudio(string url)
{
// HTTP
NSUrlSession session = NSUrlSession.FromConfiguration(
NSUrlSessionConfiguration.DefaultSessionConfiguration,
new SessionDelegate(afs),
NSOperationQueue.MainQueue);
var dataTask = session.CreateDataTask(new NSUrl(url));
dataTask.Resume();
}
// event handler - never executed
void OnPropertyFound(object sender, PropertyFoundEventArgs e)
{
if(e.Property == AudioFileStreamProperty.ReadyToProducePackets)
{
oaq = new OutputAudioQueue(afs.StreamBasicDescription);
oaq.BufferCompleted += OnBufferCompleted;
}
}
// another event handler never executed
void OnPacketDecoded(object sender, PacketReceivedEventArgs e)
{
IntPtr outBuffer;
oaq.AllocateBuffer(e.Bytes, out outBuffer);
AudioQueue.FillAudioData(outBuffer, 0, e.InputData, 0, e.Bytes);
oaq.EnqueueBuffer(outBuffer, e.Bytes, e.PacketDescriptions);
// start playing if not already
if(!outputStarted)
{
var status = oaq.Start();
if (status != AudioQueueStatus.Ok)
System.Diagnostics.Debug.WriteLine("Could not start audio queue");
outputStarted = true;
}
}
void OnBufferCompleted(object sender, BufferCompletedEventArgs e)
{
oaq.FreeBuffer(e.IntPtrBuffer);
}
}
// instantiated in GetAudio()
class SessionDelegate : NSUrlSessionDataDelegate
{
readonly AudioFileStream afs;
public SessionDelegate(AudioFileStream afs)
{
this.afs = afs;
}
// this is, too, never executed
public override void DidReceiveData(NSUrlSession session, NSUrlSessionDataTask dataTask, NSData data)
{
afs.ParseBytes((int)data.Length, data.Bytes, false);
}
}顺便说一句,我主要是从这个屏幕复制代码。
发布于 2016-05-26 21:19:02
你的代码看起来是正确的..。
1)我在你的消息来源中看到了"http://“评论”。您的流是否来自http://源而不添加ATS异常?如果是这样的话,dataTask.Resume();是“无声代码失败”,因此不会调用任何委托/事件,,但是 iOS会记录它。
请检查输出日志中的如下内容:
StreamingAudio[13602:591658] App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file.使用https://源代码,但是如果您必须完全关闭ATS以进行测试:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key><true/>
</dict>注意:搜索iOS ATS以获取更多信息或详细信息,以便只允许来自您将要流的服务器的非安全http://,但是iOS的未来只有https://才能开始准备.;-)
2)如果提供给NSUrlSession的URL源产生拒绝连接的,这是一个无声的失败,而且没有记录它,当然没有一个委托/事件被调用.预先测试您的流媒体web服务是否处于活动状态,尝试使用curl、wget等。去测试溪流..。
3)我看到了您的流MP3s,所以您应该没事,但是请记住,格式有数百种不同的变化,而且iOS没有全部流/播放它们,所以尝试通过不同的工具集编码的不同的mp3,只为了重复检查.
https://stackoverflow.com/questions/37470136
复制相似问题