Asp.Net 音頻檔案上傳和播放代碼

來源:互聯網
上載者:User

一、音頻檔案上傳和播放
在網上找到一個名叫AspNetPager的第三方控制項,將AspNetPager.dll檔案引用到項目的Bin中。在網頁中可直接調用。引用命名空間 using Telerik.WebControls;
>>上傳 複製代碼 代碼如下:protected void Button1_Click(object sender, EventArgs e)
{
foreach (UploadedFile file in RadUploadContext.Current.UploadedFiles)
{
if (file.FileName != null && file.FileName != "")
{
string strTime = memberid.ToString() + "-" + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Millisecond.ToString();
string namefile = file.FileName;//擷取檔案原來的名字
string namelast = namefile.Substring(namefile.LastIndexOf("."));//擷取檔案的尾碼名,LastIndexOf()-返回括弧內字元的所在位置
string FilePath = System.AppDomain.CurrentDomain.BaseDirectory + @"SaveFilePath/MBmusic/";//檔案上傳的路徑
System.IO.FileInfo fi = new System.IO.FileInfo(file.FileName);
if (fi.Extension.ToLower() == ".mp3" || fi.Extension.ToLower() == ".wma" || fi.Extension.ToLower() == ".rm" || fi.Extension.ToLower() == ".wav")
{
string name = strTime + namelast;//檔案最後儲存的名字
mv = "http://www.cnblogs.com/SaveFilePath/MBmusic/" + name;
Filesizes = int.Parse(file.ContentLength.ToString());
string path = FilePath + name;
if (File.Exists(path) == false)
{
file.SaveAs(FilePath + name);
mf.MF_Path = mv;
//mf.MF_Path = mv;
}
else if (File.Exists(path) == true)
{
name = strTime + "(" + tt + ")" + namelast;
file.SaveAs(FilePath + name);
mv = "http://www.cnblogs.com/SaveFilePath/MBmusic/" + name;
mf.MF_Path = mv;
//mf.MF_Path = mv;
}
if (RemainByteSize >= Filesizes)
{
try
{
//將資料插入資料庫
if (addnum > 0 && udpsize > 0)
{
//Response.Write("<script>alert('上傳成功!!')</script>");
//Bind();
}
else
{
Response.Write("<script>alert('上傳失敗!!')</script>");
return;
}
num++;
}
catch { }
}
else
{
Response.Write("<script>alert('空間不足,請申請空間!!')</script>");
return;
}
}
else
{
Response.Write("<script>alert('上傳檔案格式錯誤! 檔案格式為mp3,wma,wav.rm的視頻!')</script>");
return;
}
}
else
{
Response.Write("<script>alert('請選擇上傳的檔案')</script>");
return;
}
}
}

>>播放
1、在播放頁面添加控制項Literal1 控制項
2、在頁面後台添加調用音頻播放器代碼 複製代碼 代碼如下:public static string GetFlashText(string url)
{
string str = @"<object id='MediaPlayer' classid='clsid:6BF52A52-394A-11D3-B153-00C04F79FAA6' height='62' width='406'>
<param name='rate' value='1' />
<param name='balance' value='0' />
<param name='currentPosition' value='0' />
<param name='defaultFrame' value='' />
<param name='playCount' value='1' />
<param name='autoStart' value='-1' />
<param name='currentMarker' value='0' />
<param name='invokeURLs' value='-1' />
<param name='baseURL' value='' />
<param name='volume' value='2' />
<param name='mute' value='0' />
<param name='uiMode' value='full' />
<param name='stretchToFit' value='0' />
<param name='windowlessVideo' value='0' />
<param name='enabled' value='-1' />
<param name='enableContextMenu' value='0' />
<param name='fullScreen' value='0' />
<param name='SAMIStyle' value='' />
<param name='SAMILang' value='' />
<param name='SAMIFilename' value='' />
<param name='captioningID' value='' />
<param name='enableErrorDialogs' value='0' />
<param name='_cx' value='10742' />
<param name='_cy' value='1799' />
<param name='URL' value='"+url+"'/></object>";
return str;
}

3、調用GetFlashText()方法
複製代碼 代碼如下:public void BindMusic()
{
if (mfid != 0)
{
DataSet dst = mf.Search(mfid);
if (dst != null && dst.Tables[0].Rows.Count > 0)
{
string url = dst.Tables[0].Rows[0]["MF_Path"].ToString();
string[] strs = url.Split(new char[] { '/' });
string mvname = strs[strs.Length - 1];
string url1 = "SaveFilePath/MBmv/" + mvname;
Literal1.Text = GetFlashText(url);//Literal1調用方法播放音頻
Label1.Text = dst.Tables[0].Rows[0]["MF_FileName"].ToString();
//txt_FileName.Text = dst.Tables[0].Rows[0]["MF_FileName"].ToString();
//txt_introduction.Text = dst.Tables[0].Rows[0]["MF_Introduction"].ToString();
}
}
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.