使用 MediaPlayer (WPF) 读取音频文件以进行流式传输
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13348897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Read audio file to stream with MediaPlayer (WPF)
提问by Developer
I use MediaPlayerto play audio files
我MediaPlayer用来播放音频文件
var mediaPlayer = new MediaPlayer();
mediaPlayer.Open(new Uri(s));
mediaPlayer.Play();
Sometimes I need to delete files that MediaPlayeris still playing.
I guess I have somehow read file to stream to be get free access to it in order to delete it. I mean i sthere way to read file to stream or I have to create some temp. file to play and in this case I can delete the original one or there are other options?
有时我需要删除MediaPlayer仍在播放的文件。我想我以某种方式读取文件以进行流式传输以免费访问它以将其删除。我的意思是我有办法读取文件进行流式传输,或者我必须创建一些临时文件。要播放的文件,在这种情况下我可以删除原始文件还是有其他选择?
How to implement it?
如何实施?
Thank you!
谢谢!
回答by Bill Tarbell
Actually, when you call MediaPlayer.Open it reads in the file via a stream automatically. The problem is that the stream is still open when you try to delete the file.
实际上,当您调用 MediaPlayer.Open 时,它会自动通过流读取文件。问题是当您尝试删除文件时,流仍处于打开状态。
MediaPlayer has a .Closemethod on it. Calling this will close the stream that's reading in the file.
MediaPlayer 有一个.Close方法。调用此方法将关闭正在读取文件的流。
here's the documentation on the MediaPlayer class so you can see what other methods are available to use: http://msdn.microsoft.com/en-us/library/system.windows.media.mediaplayer.aspx
这是有关 MediaPlayer 类的文档,因此您可以查看可以使用哪些其他方法:http: //msdn.microsoft.com/en-us/library/system.windows.media.mediaplayer.aspx
EDIT: If you don't mind that the player stops playback when you call Close then you can just Close & then delete your file. If you do need playback to continue then you'll need a different approach.
编辑:如果您不介意调用 Close 时播放器停止播放,那么您可以关闭并删除您的文件。如果您确实需要继续播放,那么您将需要一种不同的方法。
If you're using Silverlight then you can just load the stream directly into a MediaElement:
如果您使用 Silverlight,那么您可以直接将流加载到 MediaElement 中:
var bytes = File.ReadAllBytes(@"c:\yourfile.ext");
var mStream = new MemoryStream(bytes);
mediaElement1.SetSource(mStream);
Sadly, WPF does not have the same stream support. I attempted to get a Uri for the MemoryStream by writing the stream into the resource pack. Though, i couldn't get it to playback correctly in my testing. I'll include my source that i had just in case you want to fiddle with it and maybe get it to work:
遗憾的是,WPF 没有相同的流支持。我试图通过将流写入资源包来获取 MemoryStream 的 Uri。但是,我无法在测试中正确播放它。我将包括我的来源,以防万一你想摆弄它并让它工作:
var bytes = File.ReadAllBytes(@"C:\Bill\TestWaveFiles043.wav");
MemoryStream packStream = new MemoryStream()
Package pack = Package.Open(packStream, FileMode.Create, FileAccess.ReadWrite);
Uri packUri = new Uri("bla:");
PackageStore.AddPackage(packUri, pack);
Uri packPartUri = new Uri("/MemoryResource", UriKind.Relative);
PackagePart packPart = pack.CreatePart(packPartUri, "Media/MemoryResource");
packPart.GetStream().Write(bytes, 0, bytes.Length);
var inMemoryUri = PackUriHelper.Create(packUri, packPart.Uri);
mediaElement1.LoadedBehavior = MediaState.Manual;
mediaElement1.Source = inMemoryUri;
mediaElement1.Play();
Another option is to simply make a copy of the file before you open it. that way you can always delete the original. Though, you could also just "mark" the file to be deleted. When the user is done playing the the file then you could close & delete it.
另一种选择是在打开文件之前简单地复制该文件。这样你就可以随时删除原件。不过,您也可以“标记”要删除的文件。当用户播放完文件后,您可以关闭并删除它。
One additional option is to use a 3rd party library called BoxedApp. It seemingly will allow you to have a "Virtual File" that contains a memory stream. You could then get a Uri that points to this virtual file and load it into the media player. Look at this answer by user1108125 to see how to use this BoxedApp library (which i've never used). https://stackoverflow.com/a/8587166/1721136
另一种选择是使用名为 BoxedApp 的 3rd 方库。它似乎允许您拥有一个包含内存流的“虚拟文件”。然后,您可以获得指向该虚拟文件的 Uri 并将其加载到媒体播放器中。查看 user1108125 的这个答案,了解如何使用这个 BoxedApp 库(我从未使用过)。https://stackoverflow.com/a/8587166/1721136

