所以,我的问题是我尝试使用AudioFileStream和OutputAudioQueue类在Xamarin.iOS上传输音频。我已经为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);
}
}
顺便说一句,我大部分都是从this screencast复制代码。
答案 0 :(得分:0)
您的代码看起来正确......
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的未来是{{ 1}}只有这样开始准备...; - )
2)如果提供给NSUrlSession的URL源产生连接被拒绝,那就是静默失败,并且没有关于它的记录,当然也没有调用任何委托/事件。预先测试您的流媒体网络服务是否有效,尝试使用https://
,curl
等来测试流...
3)我看到你的流媒体MP3,所以你应该没问题,但是记住格式有数百种变化,而iOS不会流式传输/播放它们所以请尝试不同的mp3编码通过一个不同的工具集只是为了仔细检查......