c#youtube-api-live流GoogleApiException(流处于非活动状态)

时间:2018-09-28 06:30:54

标签: c# youtube-data-api youtube-livestreaming-api

我正在使用youtube-api开发直播服务。

这是我的代码

C#WPF

private void LiveStreamBtn_Click(object sender, RoutedEventArgs e)
{
    var broadcast = new LiveBroadcast
    {
        Kind = "youtube#liveBroadcast",
        Snippet = new LiveBroadcastSnippet
        {
            Title = "StreamService",
            ScheduledStartTime = DateTime.Now
        },
        Status = new LiveBroadcastStatus
        {
            PrivacyStatus = "public",
        },
        ContentDetails = new LiveBroadcastContentDetails
        {
            MonitorStream = new MonitorStreamInfo
            {
                EnableMonitorStream = true
            }
        }
    };

    var liveBroadcastInsert = youTubeService.LiveBroadcasts.Insert(broadcast, "snippet,status,contentDetails");
    var returnedBroadcast = liveBroadcastInsert.Execute();

    Console.WriteLine("\n================== Returned Broadcast ==================\n");
    Console.WriteLine(" - Id: " + returnedBroadcast.Id);
    Console.WriteLine(" - Title: " + returnedBroadcast.Snippet.Title);
    Console.WriteLine(" - Description: " + returnedBroadcast.Snippet.Description);
    Console.WriteLine(" - Published At: " + returnedBroadcast.Snippet.PublishedAt);
    Console.WriteLine(" - Scheduled Start Time: " + returnedBroadcast.Snippet.ScheduledStartTime);
    Console.WriteLine(" - Scheduled End Time: " + returnedBroadcast.Snippet.ScheduledEndTime);

    var stream = new LiveStream
    {
        Kind = "youtube#liveStream",
        Snippet = new LiveStreamSnippet
        {
            Title = "StreamService"
        },
        Status = new LiveStreamStatus
        {
            StreamStatus = "active"
        },
        Cdn = new CdnSettings
        {
            Format = "720p",
            IngestionType = "rtmp"
        }
    };

    var liveStreamInsert = youTubeService.LiveStreams.Insert(stream, "snippet,status,cdn");
    var returnedStream = liveStreamInsert.Execute();

    Console.WriteLine("\n================== Returned Stream ==================\n");
    Console.WriteLine(" - Id: " + returnedStream.Id);
    Console.WriteLine(" - Title: " + returnedStream.Snippet.Title);
    Console.WriteLine(" - Description: " + returnedStream.Snippet.Description);
    Console.WriteLine(" - Published At: " + returnedStream.Snippet.PublishedAt);
    Console.WriteLine(" - URL: " + returnedStream.Cdn.IngestionInfo.IngestionAddress);
    Console.WriteLine(" - Name: " + returnedStream.Cdn.IngestionInfo.StreamName);

    var liveBroadcastBind = youTubeService.LiveBroadcasts.Bind(returnedBroadcast.Id, "id,status,contentDetails");
    liveBroadcastBind.StreamId = returnedStream.Id;
    returnedBroadcast = liveBroadcastBind.Execute();

    Console.WriteLine("\n================== Returned Bound Broadcast ==================\n");
    Console.WriteLine(" - Broadcast Id: " + returnedBroadcast.Id);
    Console.WriteLine(" - Bound Stream Id: " + returnedBroadcast.ContentDetails.BoundStreamId);

    var liveStreamRequest = youTubeService.LiveStreams.List("id,snippet,status");

    liveStreamRequest.Id = returnedStream.Id;

    Console.WriteLine("\n================== Find Bound Broadcast, Stream ==================\n");
    Console.WriteLine("BroadCast status :: " + returnedBroadcast.Status.LifeCycleStatus);
    Console.WriteLine("Stream status :: " + returnedStream.Status.StreamStatus);

    var broadCastTrans
        = youTubeService.LiveBroadcasts.Transition(
            LiveBroadcastsResource.TransitionRequest.BroadcastStatusEnum.Testing, returnedBroadcast.Id, "status");

    Console.WriteLine("\n================== Transition Status==================\n");
    Console.WriteLine("Transition Status :: " + broadCastTrans.BroadcastStatus);

    broadCastTrans.Execute();
}

我获得了“返回广播”,“返回流”和“返回绑定广播”的完美结果。

当我的YouTube帐户被绑定时,我也有一个活动。

但是,当执行最后一行代码时,会发生错误。

  

Google.GoogleApiException:'Google.Apis.Requests.RequestError   信息流无效[403]   错误[消息[流无效]位置[-]原因[errorStreamInactive]域[youtube.liveBroadcast]]'

如何更改流状态?

0 个答案:

没有答案