MetroStyleApp入門 vol6.HTTP通信その2

木曜日 , 5, 4月 2012 Leave a comment

(本記事はWindows8Consumer PreviewとVisualStudio11β環境で動作を確認しています)

 

 前回に引き続いて、動画再生のサンプルです。

 外部URLからデータを取ってくるところまで前回紹介しましたので、今回は少し進めて動画のリストを取得してクリックした動画を再生できるところまで実装してみたいと思います。

 

 

 

サーバーから返される値

 

 サーバからはXMLで動画のデータが返ることにします。

<values>
    <value>
        <name>Big Buck Bunny</name>
        <description>SmoothStreamingの動画です</description>
        <url>http://video3.smoothhd.com.edgesuite.net/ondemand/Big%20Buck%20Bunny%20Adaptive.ism/Manifest</url>
        <image></image>
    </value>
    <value>
        <name>FlexUG北陸001</name>
        <description>生まれて初めての勉強会登壇動画です</description>
        <url>http://coelacanth.heteml.jp/10.wmv</url>
        <image></image>
    </value>
    <value>
        <name>テストBlender</name>
        <description>Blenderテスト動画です</description>
        <url>http://coelacanth.heteml.jp/test.wmv</url>
        <image></image>
    </value>
</values>

 上記のような動画形式が返ることにします。

 

 メトロスタイルアプリ側ではこれを取得してListBoxに表示、タップした動画を再生することにします。

 とりあえずエラー処理は考えません(キリッ)。

 

 動画を取得する部分は以下、

                    this.videoView_Canvas.Children.Add(this.player);

                    // URL一覧を取得する
                    String uri_str = "http://coelacanth.heteml.jp/smoothlist/url/setUrl";
                    System.Uri uri = new Uri(uri_str);

                    HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(uri);
                    Request.Method = "POST";
                    HttpWebResponse Response = (HttpWebResponse)await Request.GetResponseAsync();
                    StreamReader ResponseDataStream = new StreamReader(Response.GetResponseStream());
                    var result = ResponseDataStream.ReadToEnd();

 

 ここまでは前回とほぼ同様。

 今回はXMLをListBoxにバインドします。

 続きのコードは以下、

 

                    System.Xml.Linq.XElement doc = System.Xml.Linq.XElement.Parse(result);

                    var list = from c in doc.Descendants("value")
                               select new MovieItem
                               {
                                   name = c.Element("name").Value,
                                   description = c.Element("description").Value,
                                   url = c.Element("url").Value
                               };

                    this.movieList_ListBox.ItemsSource = list;

 

MovieItemクラスは以下のように定義してあります。

    class MovieItem
    {
        public string name { get; set; }
        public string description { get; set; }
        public string url { get; set; }
    }

 

これでListBoxにアイテムが表示されるようになりました。

(XAMLコードはサンプルをダウンロード可能にしますのでそこで参照してください)

 

 

続いてリストをタップしたときのイベントを記述します。

        private void movieList_ListBox_Tapped(object sender, TappedRoutedEventArgs e)
        {
            this.player.Stop();

            this.player.Source = new Uri((this.movieList_ListBox.SelectedItem as MovieItem).url, UriKind.Absolute);
            this.player.Width = 800;
            this.player.Height = 600;
            this.player.Play();
        }

 

PlayerはSmoothStreaming対応のMediaPlayerクラスです。

利用方法は「Windows8:Smooth Streaming Client SDK (Beta)」を参考にしてください。

 

デザインも、エラー処理もない作りこみの浅い状態ですが、一応動画プレイヤーが動きました。

 

サンプルをダウンロードする(VisualStudio11beta版)

 

 

 

 


Please give us your valuable comment

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください