如何从Facebook流式传输视频

时间:2017-06-02 00:54:51

标签: android facebook facebook-graph-api

我想用facebook中的精选视频构建Android应用。该应用程序与我的服务器通信,该服务器以JSON格式向应用程序提供视频ID。像这样

    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Input;

    namespace TreeViewWithCheckBoxes
    {
        public partial class Window1 : Window
        {
            public Window1()
            {
                InitializeComponent();

                FooViewModel root = this.tree.Items[0] as FooViewModel;

                base.CommandBindings.Add(
                new CommandBinding(
                    ApplicationCommands.Undo,
                    (sender, e) => // Execute
                    {                        
                        e.Handled = true;
                        root.IsChecked = false;
                        this.tree.Focus();
                    },
                    (sender, e) => // CanExecute
                    {
                        e.Handled = true;
                        e.CanExecute = (root.IsChecked != false);
                    }));

                this.tree.Focus();
            }

            private void TreeViewItemDragEnter(object sender, DragEventArgs e)
            {
                e.Effects = DragDropEffects.None;
                if (e.Data.GetDataPresent(typeof(FooViewModel)))
                {
                    var folderViewModel = e.Data.GetData(typeof(FooViewModel))
                       as FooViewModel;
                    var treeViewItem =
                       VisualTreeHelperUtils.FindParent<TreeViewItem>
                      ((DependencyObject)e.OriginalSource);

                    if (treeViewItem == null)
                    {
                        return;
                    }
                    var dropTarget = treeViewItem.Header as FooViewModel;

                    if (dropTarget == null || folderViewModel == null)
                    {
                        return;
                    }

                    if (dropTarget.Parent == folderViewModel.Parent)
                        e.Effects = e.AllowedEffects;
                    else
                    {
                        e.Effects = DragDropEffects.None;
                    }
                }
            }

            private void TreeViewItemDrop(object sender, DragEventArgs e)
            {
                if (e.Data.GetDataPresent(typeof(FooViewModel)))
                {
                    var folderViewModel = e.Data.GetData(typeof(FooViewModel))
                        as FooViewModel;
                    var treeViewItem =
                        VisualTreeHelperUtils.FindParent<TreeViewItem>
                        ((DependencyObject)e.OriginalSource);

                    var dropTarget = treeViewItem.Header as FooViewModel;

                    if (dropTarget == null || folderViewModel == null)
                        return;

                    if (dropTarget.Parent == folderViewModel.Parent)
                    {
                        var dropType = dropTarget.Name;
                        var dragType = folderViewModel.Name;

                        var parent = dropTarget.Parent;

                    if (parent != null)
                    {
                        var dropLocation = -1;
                        var dragLocation = -1;
                        for (int index = 0; index < parent.Children.Count; ++index)
                        {
                            if (parent.Children[index].Name == dropType) dropLocation = index;
                            if (parent.Children[index].Name == dragType) dragLocation = index;
                            if (dropLocation != -1 && dragLocation != -1)
                                break;
                        }

                        if (dropLocation != -1 && dragLocation != -1)
                        {
                            parent.Children[dropLocation] = folderViewModel;
                            parent.Children[dragLocation] = dropTarget;
                        }
                    }
                }
            }
        }

        private void TreeViewItemMouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                var mousePos = e.GetPosition(null);
                var diff = StartPoint - mousePos;

                if (Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance
                    || Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance)
                {
                    var treeView = sender as TreeView;
                    var treeViewItem =
                        VisualTreeHelperUtils.FindParent<TreeViewItem>((DependencyObject)e.OriginalSource);

                    if (treeView == null || treeViewItem == null)
                        return;

                    var folderViewModel = treeView.SelectedItem as FooViewModel;
                    if (folderViewModel == null)
                        return;

                    var dragData = new DataObject(folderViewModel);
                    DragDrop.DoDragDrop(treeViewItem, dragData, DragDropEffects.Move);
                }
            }
        }

        private void TreeViewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            StartPoint = e.GetPosition(null);
        }

        public Point StartPoint { get; set; }

        private void TreeViewItemDragOver(object sender, DragEventArgs e)
        {
            var container = sender as FrameworkElement;

            if (container == null)
            {
                return;
            }

            var scrollViewer = VisualTreeHelperUtils.GetFirstVisualChild<ScrollViewer>(container);

            if (scrollViewer == null)
            {
                return;
            }

            double tolerance = 30;
            double verticalPos = e.GetPosition(container).Y;
            double offset = 20;

            if (verticalPos < tolerance) // Top of visible list? 
            {
                scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset - offset); //Scroll up. 
            }
            else if (verticalPos > container.ActualHeight - tolerance) //Bottom of visible list? 
            {
                scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset + offset); //Scroll down.     
            }

            e.Effects = DragDropEffects.None;

            var source = e.OriginalSource as DependencyObject;

            if (source == null)
            {
                return;
            }
            if (e.Data.GetDataPresent(typeof(FooViewModel)))
            {
                var folderViewModel = e.Data.GetData(typeof(FooViewModel))
                    as FooViewModel;
                var treeViewItem =
                    VisualTreeHelperUtils.FindParent<TreeViewItem>((DependencyObject)e.OriginalSource);

                if (treeViewItem == null)
                {
                    return;
                }
                var dropTarget = treeViewItem.Header as FooViewModel;

                if (dropTarget == null || folderViewModel == null)
                {
                    return;
                }

                if (dropTarget.Parent == folderViewModel.Parent)
                    e.Effects = e.AllowedEffects;
                else
                {
                    e.Effects = DragDropEffects.None;
                }
            }
        }
    }
}

虽然这种只传递视频ID的方法适用于Youtube Player Api:YouTube Android Player API

我在互联网上找不到足够的资源,这将为facebook视频做同样的事情。我想用他们的api播放一个fb视频。

我已经尝试了他们的Graph API

这是我作为回应得到的:

<Window 
  x:Class="TreeViewWithCheckBoxes.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:local="clr-namespace:TreeViewWithCheckBoxes"
  xmlns:dw="clr-namespace:DrWPF.Windows.Controls"
  FontSize="13"
  Title="TreeView with CheckBoxes"
  Width="300" Height="300" 
  WindowStartupLocation="CenterScreen"  
  >
  <Window.Resources>
    <ResourceDictionary>
      <!-- Load this specific theme because the Aero theme for CheckBox has issues. -->
      <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="/PresentationFramework.Royale;V3.0.0.0;31bf3856ad364e35;component\themes\royale.normalcolor.xaml" />
      </ResourceDictionary.MergedDictionaries>

      <Style x:Key="TreeViewItemStyle" TargetType="TreeViewItem">
        <Setter Property="IsExpanded" Value="True" />
        <Setter Property="IsSelected" Value="{Binding IsInitiallySelected, Mode=OneTime}" />
        <Setter Property="KeyboardNavigation.AcceptsReturn" Value="True" />
        <Setter Property="dw:VirtualToggleButton.IsVirtualToggleButton" Value="True" />
        <Setter Property="dw:VirtualToggleButton.IsChecked" Value="{Binding IsChecked}" />        
      </Style>

      <HierarchicalDataTemplate 
        x:Key="CheckBoxItemTemplate"
        ItemsSource="{Binding Children, Mode=OneTime}"
        >
        <StackPanel Orientation="Horizontal">
          <!-- These elements are bound to a FooViewModel object. -->
          <CheckBox
            Focusable="False" 
            IsChecked="{Binding IsChecked}" 
            VerticalAlignment="Center"
            />
          <ContentPresenter 
            Content="{Binding Name, Mode=OneTime}" 
            Margin="2,0"
            />
        </StackPanel>
      </HierarchicalDataTemplate>
    </ResourceDictionary>
  </Window.Resources>

  <Window.DataContext>
    <ObjectDataProvider 
      MethodName="CreateFoos"
      ObjectType="{x:Type local:FooViewModel}" />
  </Window.DataContext>

    <TabControl>
        <TabItem Header="Original">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="3*"/>
                    <RowDefinition Height=".2*"/>
                    <RowDefinition Height="2*"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
              <Button 
                Grid.Row="3"
                Command="Undo"
                Content="Generate Report" 
                HorizontalAlignment="Center"
                Margin="0,2"
                Padding="8,0"
                />
              <TreeView 
                  Grid.Row="0"
                x:Name="tree"
                ItemContainerStyle="{StaticResource TreeViewItemStyle}"
                ItemsSource="{Binding Mode=OneTime}"
                ItemTemplate="{StaticResource CheckBoxItemTemplate}"
                  AllowDrop="True"
                PreviewMouseLeftButtonDown="TreeViewMouseLeftButtonDown"
                PreviewMouseMove="TreeViewItemMouseMove"
                DragEnter="TreeViewItemDragEnter"
                DragOver="TreeViewItemDragOver"
                Drop="TreeViewItemDrop"
                />
                <ScrollViewer Grid.Row="2" IsEnabled="{Binding ElementName=tree, Path=SelectedItem.IsChecked}">
                    <TextBlock Text="Oh Hai"></TextBlock>
                    <ScrollViewer.Style>
                        <Style TargetType="ScrollViewer">
                            <Style.Triggers>
                                <Trigger Property="IsEnabled" Value="False">
                                    <Setter Property="Opacity" Value="0.5"></Setter>
                                    <Setter Property="VerticalScrollBarVisibility" Value="Hidden"></Setter>
                                </Trigger>
                                <Trigger Property="IsEnabled" Value="True">
                                    <Setter Property="Opacity" Value="1"></Setter>
                                    <Setter Property="VerticalScrollBarVisibility" Value="Auto"></Setter>
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                    </ScrollViewer.Style>
                </ScrollViewer>
            </Grid>
        </TabItem>
        <TabItem Header="Modified">
            <DockPanel>
                <Button 
                DockPanel.Dock="Bottom" 
                Command="Undo"
                Content="Uncheck All" 
                HorizontalAlignment="Center"
                Margin="0,2"
                Padding="8,0"
                />
                <TreeView 
                x:Name="modified"
                ItemContainerStyle="{StaticResource TreeViewItemStyle}"
                ItemsSource="{Binding Mode=OneTime}"
                ItemTemplate="{StaticResource CheckBoxItemTemplate}"
                />
            </DockPanel>
        </TabItem>
    </TabControl>

</Window>

但我在这里找不到流媒体信息。

似乎有嵌入网络: Embedded Video & Live Video Player

但我找不到与Android相同的内容!任何指针都表示赞赏。

1 个答案:

答案 0 :(得分:0)

您应该可以请求来源字段

$ oksocial https://graph.facebook.com/1973801359519107?fields=source | jq .
{
  "source": "https://scontent.xx.fbcdn.net/v/t42.1790-29/18846178_1977083652514776_2979079068437184512_n.mp4?efg=eyJybHIiOjE1MDAsInJsYSI6MjU3OSwidmVuY29kZV90YWciOiJzZCJ9&rl=1500&vabr=348&oh=d0e43bafa8d9740c205c494f0fde0b5d&oe=5932C4FA",
  "updated_time": "2017-06-01T21:43:02+0000",
  "id": "1973801359519107"
}

https://developers.facebook.com/docs/graph-api/reference/video