将Video文件保存到数据库,然后再从数据库显示到axWindowsMediaPlayer1上



将Video文件保存到数据库,然后再从数据库显示到axWindowsMediaPlayer1上

具体思路如下:
1.将Video文件转化为二进制数据,将二进制数据,文件名保存到数据库对应的表中
2.为了将Video显示在Winform的axWindowsMediaPlayer1中,可以从数据库中读取出数据,根据保存的Video文件的二进制数据,将二进制数据转化为临时文件保存在临时目录中,然后把临时文件的路径绑定到axWindowsMediaPlayer1显示播放。

具体的代码如下:

   private void SaveToBase_Click(object sender, EventArgs e)
        {
            string filePath = GetFile();
            string[] list = new string[] { ".mp3", ".avi", ".mp4", ".wmv" };
            if (!list.Contains(Path.GetExtension(filePath)))
            {
                MessageBox.Show("Please select proper file.");
            }
            else
            {
                //读取filepath的文件
                FileStream fs = new FileStream(filePath,FileMode.Open,FileAccess.Read);
                byte[] videoByteArr = new byte[fs.Length];
                //将二进制内容存储在videoByteArr
                fs.Read(videoByteArr,0,Convert.ToInt32(fs.Length));
                fs.Close();
                //将videoByteArr保存到数据库
                if (SaveToDataBase(Path.GetFileName(filePath), videoByteArr))
                {
                    cmbPlayList.Items.Add(Path.GetFileName(filePath));
                    MessageBox.Show("Save OK!!!");
                }
                else
                {
                    MessageBox.Show("Error!!!"); 
                }
            }
        }

        private void OpenFromBase_Click(object sender, EventArgs e)
        {               
            string fileName = cmbPlayList.SelectedItem.ToString();
            string filePath = Path.GetTempPath() + "\\" + fileName;
            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }
            SqlConnection connection = new SqlConnection(ConnectionString);
            string qurey = "select content from TestTable where filename = " + "'" + fileName + "'";
            SqlDataAdapter adapter = new SqlDataAdapter(qurey, connection);
            DataSet ds = new DataSet();
            adapter.Fill(ds, "MyPlay");
            if (ds.Tables[0].Rows.Count == 0)
            {
                MessageBox.Show("No data found");
            }
            else
            {
                FileStream fs = new FileStream(filePath,FileMode.Create);
                byte[] videoByteArr = (byte[])ds.Tables[0].Rows[0]["content"];
                fs.Write(videoByteArr,0,videoByteArr.Length);
                fs.Close();
                fs = null;
            }
            axWindowsMediaPlayer1.URL = filePath; 
            axWindowsMediaPlayer1.settings.autoStart = true;
        }
        public bool SaveToDataBase(string fileName, byte[] data)
        {
            try
            {
                var ds = new DataSet();
                SqlCommand cmd = new SqlCommand("insert into TestTable values('" + Guid.NewGuid() + "','" + fileName + "',@content)");
                SqlParameter param = cmd.Parameters.Add("@content", SqlDbType.VarBinary);
                param.Value = data;
                cmd.Connection = new SqlConnection(ConnectionString);
                cmd.CommandTimeout = 0;
                cmd.Connection.Open();
                cmd.ExecuteNonQuery();
                return true;
            }
            catch (Exception e)
            {
                throw;
            }
            return false;
        }


其中的内容参照了下面的文章
# Playing Audio and Video File in Windows Form Application
http://www.c-sharpcorner.com/uploadfile/26b237/playing-audio-and-video-file-in-windows-form-application/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值