簡體   English   中英

Xamarin.iOS音頻流不起作用,未觸發事件

[英]Xamarin.iOS audio streaming not working, events not being triggered

因此,我的問題是我正在嘗試使用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);
    }
}

順便說一句,我主要是從此截屏視頻中復制了代碼。

您的代碼看起來正確...

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源產生了Connection拒絕 ,這是一個無提示的失敗,並且沒有任何日志記錄,並且當然沒有任何委托/事件被調用...預測試您的流式Web服務是活躍,嘗試使用curlwget等來測試流...

3)我看到了您正在播放的MP3,所以應該沒問題 ,但是請記住,格式有數百種變體,iOS不會全部播放/播放它們,因此請嘗試通過不同的工具集編碼的不同mp3,以便再次檢查。 。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM