如何播放下载的mp3文件?

时间:2016-01-16 17:37:10

标签: c# asp.net-mvc-4

我想播放从数据库下载的mp3文件。我可以下载mp3文件,但它似乎缺少内容,因为我无法播放或打开文件。我下载mp3文件的代码如下:

/*the database stores the file as byte array*/
public ActionResult Download(int rID)
{
    byte[] fileContents = db.Devotions.Where(d=>d.ID == rID).Select(d=>d.BytesStore).SingleOrDefault(); // gets the file
    return File(fileContents, "audio/mpeg3");//returns the file
}

如何播放下载的mp3文件?

2 个答案:

答案 0 :(得分:2)

返回0或默认字节值的原因是因为您正在使用.SingleOrDefault();。这将返回序列中的第一个值或默认值,其中默认值为0.

在获取任何数据之前,请确保您要搜索的数据存在于BytesStore中。

答案 1 :(得分:1)

需要HTML5:

<audio controls>
  <source src="files/download?rID=42" type="audio/mpeg">
Your browser does not support the audio element.
</audio>

基本上无处不在: enter image description here