2010-12-13 102 views
3

我正在用Silverlight 4(C#)创建一个简单的媒体播放器。现在,我使用MediaElement的,但它给我一个错误如何使用MediaElement播放本地MP3文件

错误4001:.... System.Collections.ListDictionaryInternal

现在,当我想设置的源这个错误发生MediaElement的。所以当用户点击“播放”时,它会设置第一首歌曲的来源。

Song s = afspeelijst.ElementAt(currentPlayingIndex); 
media.Source = new System.Uri(s.FilePath); 
media.Position = TimeSpan.Zero; 
media.Play(); 

下一个代码也不起作用,但它会消除错误。但还是没声音:■

media.SetSource(new FileStream(s.FilePath, FileMode.Open)); 
+0

s.FilePath指向哪里? SL不允许应用程序访问本地文件系统(孤立存储除外)。文件是应用程序包的一部分吗? – AlexEzh 2010-12-13 06:32:09

+0

没有路径是例如c:\ music \ song.mp3并且它被转换为file:/// c:\ music \ song.mp3。该应用程序需要安装,因此它可以访问本地文件系统。 – 2010-12-13 09:21:37

回答

1

你可能想看看这个代码。它的工作对我蛮好:)

mediaElement1.LoadedBehavior = MediaState.Manual; 
mediaElement1.Source = new Uri(@"D:/ExamplePath/myVideoFile.avi"); 
//@ means that the string behind is a path so/won't be 
//treated like a special character 
mediaElement1.Play(); 
2

在Silverlight 4中,您可以访问特定用户文件夹中的本地文件(我的文档,我的音乐,我的图片和我的视频),或在这些子文件夹。 (在启用提升特权的浏览器之外)。

修改上面的代码会将其更改为下面的类似内容。

string path = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "test/song.mp3"); 


      media.SetSource(new FileStream(path, FileMode.Open)); 
      media.Position = TimeSpan.Zero; 
      media.Play(); 

我希望这有助于

+0

我试过了,没有工作。我发现,当你调用SetSource()时,它会下载文件。所以在下载代理达到100%时,我点击了Play()并且工作正常。我只是用HTTP测试了我的解决方案,不是本地的。 – 2010-12-15 18:46:07

+0

你在什么操作系统上? Windows 7处理它的方式与它所显示的不同。库/文档可能与C:\ Users \ Account_name \ Documents混淆......我试图在我的机器上运行。您可能可以使其在本地文件系统上运行。 – scartag 2010-12-15 18:49:36

0

说不允许

文件操作。访问路径“'被拒绝。

当您尝试访问Documents文件夹或其他文件localy时。我使用的是Silverlight 4,我可以在网上播放一些http文件。但不能播放像ftp://user:[email protected]/myVoicefile.snd这样的ftp文件,当我看到给定的错误

.Net的验证方法返回不支持的路径。

所以在silverlight应用程序中使用内部html页面。或者如果您的声音文件有限,则可以在Web应用程序的/ ClientBin中复制这些文件。