ようこそ。睡眠不足なプログラマのチラ裏です。

C#でFLVファイルをお手軽再生

ニコニコ動画などから得られるFLVファイルですが、
C#でお手軽に再生するには、ActiveX Control(AxShockwaveFlash)に頼るといいっぽい。
アスペクト比が指定できなかったり、再生できないFLVも結構あるようですが、まぁお手軽ということで。


※FLVファイルを取得したい方 → ニコ動APIとLINQ to XMLでごにょごにょしよう


参考:http://activex.moonvalley.com/flashControl.HTM



以下、雑なサンプルソース

using System;
using System.Windows.Forms;
using System.Xml;
using AxShockwaveFlashObjects;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        private System.ComponentModel.IContainer components = null;
        private AxShockwaveFlashObjects.AxShockwaveFlash axShockwaveFlash1;

        #region コンストラクタ
        public Form1()
        {
            InitializeComponent();

            InitAxShockwaveFlash();
        }
        #endregion

        #region イベント
        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                //swfファイルならなんでもいい
                axShockwaveFlash1.LoadMovie(0, Application.StartupPath + "\\test.swf");
                axShockwaveFlash1.FlashCall += new AxShockwaveFlashObjects._IShockwaveFlashEvents_FlashCallEventHandler(axShockwaveFlash1_FlashCall);
            }
            catch
            {
                MessageBox.Show("Flashがインストールされていないようですが・・(^ω^;)");
            }
        }

        private void axShockwaveFlash1_FlashCall(object sender, _IShockwaveFlashEvents_FlashCallEvent e)
        {
            var document = new XmlDocument();
            document.LoadXml(e.request);

            XmlNodeList list = document.GetElementsByTagName("arguments");
            //ResizePlayer(Convert.ToInt32(list[0].FirstChild.InnerText), Convert.ToInt32(list[0].ChildNodes[1].InnerText));

            var width = int.Parse(list[0].ChildNodes[0].InnerText);
            var height = int.Parse(list[0].ChildNodes[1].InnerText);

            this.ClientSize = new System.Drawing.Size(width, height);
            axShockwaveFlash1.ClientSize = this.axShockwaveFlash1.Size;
        }

        /// <summary>
        /// DragDrop
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_DragDrop(object sender, DragEventArgs e)
        {
            string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
            LoadFLV(files[0]);
        }

        /// <summary>
        /// DragEnter
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop, false))
            {
                e.Effect = DragDropEffects.All;
            }
        }
        #endregion

        #region メソッド

        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
            this.axShockwaveFlash1 = new AxShockwaveFlashObjects.AxShockwaveFlash();
            ((System.ComponentModel.ISupportInitialize)(this.axShockwaveFlash1)).BeginInit();
            this.SuspendLayout();
            // 
            // axShockwaveFlash1
            // 
            this.axShockwaveFlash1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.axShockwaveFlash1.Enabled = true;
            this.axShockwaveFlash1.Location = new System.Drawing.Point(0, 0);
            this.axShockwaveFlash1.Name = "axShockwaveFlash1";
            this.axShockwaveFlash1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axShockwaveFlash1.OcxState")));
            this.axShockwaveFlash1.Size = new System.Drawing.Size(394, 312);
            this.axShockwaveFlash1.TabIndex = 0;

            // 
            // Form1
            // 
            this.AllowDrop = true;
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(394, 312);
            this.Controls.Add(this.axShockwaveFlash1);
            this.MaximizeBox = false;
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.DragDrop += new System.Windows.Forms.DragEventHandler(this.Form1_DragDrop);
            this.DragEnter += new System.Windows.Forms.DragEventHandler(this.Form1_DragEnter);
            ((System.ComponentModel.ISupportInitialize)(this.axShockwaveFlash1)).EndInit();
            this.ResumeLayout(false);
        }

        /// <summary>
        /// AxShockwaveFlashの設定
        /// </summary>
        private void InitAxShockwaveFlash()
        {
            this.axShockwaveFlash1.AllowFullScreen = "false";
            this.axShockwaveFlash1.BGColor = "000000";
            this.axShockwaveFlash1.AllowNetworking = "all";

            this.axShockwaveFlash1.CtlScale = "NoScale";
            //this.axShockwaveFlash1.CtlScale = "NoBorder ";
            //this.axShockwaveFlash1.CtlScale = "ExactFit";
            //this.axShockwaveFlash1.CtlScale = "ShowAll";

            this.axShockwaveFlash1.DeviceFont = false;
            this.axShockwaveFlash1.EmbedMovie = true;

            this.axShockwaveFlash1.FrameNum = -1;
            this.axShockwaveFlash1.Loop = true;
            this.axShockwaveFlash1.Playing = true;
            this.axShockwaveFlash1.Profile = true;
            this.axShockwaveFlash1.Quality2 = "High";
            this.axShockwaveFlash1.SAlign = "LT";
            this.axShockwaveFlash1.WMode = "Window";
            this.axShockwaveFlash1.Dock = DockStyle.Fill;
        }

        /// <summary>
        /// FLVファイルのロード
        /// </summary>
        /// <param name="videoPath"></param>
        private void LoadFLV(string videoPath)
        {
            axShockwaveFlash1.CallFunction("<invoke name=\"loadAndPlayVideo\" returntype=\"xml\"><arguments><string>" + videoPath + "</string></arguments></invoke>");
        }
        #endregion

        #region Dispose
        /// <summary>
        /// 使用中のリソースをすべてクリーンアップします。
        /// </summary>
        /// <param name="disposing">マネージ リソースが破棄される場合 true、破棄されない場合は false です。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
        #endregion
    }
}