我想在Silverlight中使用WCF,C#語言播放音頻文件,我沒有在我的應用程序中得到任何錯誤,雖然mp3不會播放。音頻文件沒有在silverlight中播放
這裏是我到目前爲止有:
stream fileStream;
private void DownloadMusic_Click(object sender, RoutedEventArgs e)//this is the button that plays the music
{
FileServiceClient FSC = new FileServiceClient();
FSC.DownloadTestCompleted += new EventHandler<DownloadTestCompletedEventArgs>(FSC_DownloadTestCompleted);
FSC.DownloadTestAsync(FileSearch.Text);
}
void FSC_DownloadTestCompleted(object sender, DownloadTestCompletedEventArgs e)
{
FileObject fileObject;
if (e.Error == null)
{
if (e.Result != null)
{
fileObject = e.Result;
fileStream = new MemoryStream(fileObject.FileStream);
mediaElement1.SetSource(fileStream);
mediaElement1.AutoPlay = false;
mediaElement1.Play();
ResultBlock.Text = "Song is playing . . .";
}
else
{
ResultBlock.Text = "Song could not be found";
}
}
}
服務:
public FileObject DownloadTest(string fileName)
{
FileStream fileStream = null;
BinaryReader reader = null;
string filePath = "";
byte[] fileBytes;
try
{
filePath = Path.Combine(HttpContext.Current.Server.MapPath("."), "Pictures", fileName);
if (File.Exists(filePath))
{
fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
reader = new BinaryReader(fileStream);
fileBytes = reader.ReadBytes((int)fileStream.Length);
return new FileObject() { FileName = fileName, FileStream = fileBytes };
}
return null;
}
catch (Exception)
{
return null;
}
}
接口:
namespace WCF_Silverlight_UploadFile.Web
{
[ServiceContract]
public interface IFileService
{
[OperationContract]
bool UploadTest(FileObject file);
[OperationContract]
FileObject DownloadTest(string fileName);
}
[DataContract]
public class FileObject
{
[DataMember]
public string FileName { get; set; }
[DataMember]
public byte[] FileStream { get; set; }
[DataMember]
public string FileType { get; set; }
}
}
所有幫助將不勝感激。 謝謝
即使使用mediaElement1.SetSource(fileStream),它仍然不播放mp3。我現在要看看seek()。 – 2012-02-07 09:58:50
已經更新了我的回答,你也嘗試過嗎? – 2012-02-07 10:05:23
讓我知道它什麼時候適合你。 – 2012-02-07 10:14:20