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

来源:互联网 发布:淘宝(中国)有限公司 编辑:程序博客网 时间:2024/06/02 09:39


将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/

0 0
原创粉丝点击