从资源中调用mp3文件. [英] Calling mp3 files from the resources.?

查看:96
本文介绍了从资源中调用mp3文件.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<br />
<br />
QUESTION HAS BEEN SOLVED. I WILL POST THE SOLUTION SHORTLY. <br />




添加Windows Media Player库(wmp.dll)后,我可以在c#应用程序中播放mp3.




After adding windows media player library (wmp.dll), I''m able to play mp3''s in my c# application.

var player = new WMPLib.WindowsMediaPlayer();



当前语法:



Current syntax:

player.URL = @"K:\Sounds\8.mp3";



我已经将mp3和图像添加到了参考资料中.
现在可以通过以下方式调用图像:



I''ve added the mp3''s and images to the resources in the references.
Now images can be called this way:

pictureBox[0].Image = WindowsFormsApplication1.Properties.Resources.cleff;



但是该方法似乎无法在资源中找到mp3.



But that method doesn''t seem to find the mp3''s in the resources.

<code>player.URL = WindowsFormsApplication1.Properties.Resources. - mp3''s missing -;
//Therefore this way it doesn''t work.
</code> 



有人知道如何在要播放的资源中调用mp3文件吗?谢谢.



Anyone knows how to call the mp3 files in the resources to be played? thank you.

推荐答案

方法#1 .如果确实需要一种从资源流传输视频的方法,则需要使用所需的资源创建资源流.这会给您一个想法: http://social.msdn .microsoft.com/Forums/en/csharpgeneral/thread/98f149e9-6975-4d67-a2c7-7e5c93c8cfc2 [ http://msdn.microsoft.com/zh-cn/library/system.io.binaryreader.aspx [System.Uri:Uri(SerializationInfo, StreamingContext)的构造函数.有关更多信息,请参见 http://msdn.microsoft.com/en-us/library/system .uri.aspx [^ ].

这种方法将需要一些努力.

方法2 .同时,我也同意阿尔宾的观点.使用外部文件会更好.您的问题是使用了错误的路径名.您的媒体文件是只读的(否则,您不会将它们放在资源中).您应该将它们与程序集可执行文件放在一起.找出入口程序集在其中具有主模块可执行文件的位置,然后将媒体文件放在此处或放置在相对位置.这是这样的:

Approach #1. If you really need a way to stream a video from the resource, you need to create a resource stream using the resource you need. This will give you an idea: http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/98f149e9-6975-4d67-a2c7-7e5c93c8cfc2[^], http://msdn.microsoft.com/en-us/library/system.io.binaryreader.aspx[^].

Feeding the stream read from resource as the input of your Media Player control is difficult. The only input is URI. I could not find that a special Microsoft URL schema "res://" supported. As a general approach, you need to develop a special URI instead. You will need to use the constructor for the System.Uri: Uri(SerializationInfo, StreamingContext). For more information, see http://msdn.microsoft.com/en-us/library/system.uri.aspx[^].

This approach will take some effort.

Approach #2. At the same time, I agree with Albin. You will be much better off using external files. You problem is using wrong path names. Your media files are read-only (otherwise you would not put them in your resources). You should put them with your assembly executable files. Find out where your entry assembly has a main module executable and put your media files there or in some relative position. This is how:

string exePath = System.IO.Path.GetDirectoryName(
    System.Reflection.Assembly.GetEntryAssembly().Location);



方法3-中级.根据请求将媒体放入资源中,以二进制流的形式读取资源并写入临时文件;将文件作为URL传递到媒体播放器.同样,在这里您需要一些使用路径的文化.使用System.IO.Path.GetTempFileName.

我会建议方法2,并与Albin达成协议.

—SA



Approach #3 — intermediate. Put media in resources, on request, read the resource as a binary stream and write into temporary file; pass the file as URL to the Media Player. Again, here you need some culture of working with paths. Use System.IO.Path.GetTempFileName.

I would recommend Approach #2, in agreement with Albin.

—SA


仅作为帮助1)我同意不要将文件分开放在资源中; 2)如果您正在寻找一种播放MP3的好方法,并且其他使用WMP的文件,请参阅我的文章:媒体播放器类 [
Just as a help 1) I would agree keep the files seperate not in resources and 2) If your looking for a good way to play MP3 and other files using WMP please see my article: Media Player Class[^]


这篇关于从资源中调用mp3文件.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆