在C#应用程序中播放音乐 [英] Playing music in C# application

查看:79
本文介绍了在C#应用程序中播放音乐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个程序,可以在单击按钮时播放歌曲。该程序运行良好..除了从我的硬盘读取歌曲。当我将歌曲作为资源添加到我的项目并更改路径时,我得到一个错误。



自从我开始使用PHP在Web开发公司工作以来,我已经离开了C#一段时间了。所以我需要有人让我快速了解我做错了什么。



这是我的工作代码:(代码从硬盘读取歌曲)

I have created a program that plays a song when you click a button. The Program works great.. Except the song is read from my hard drive. When I add the song as a resource on my project and change the path then I get an error.

I''ve been away from C# for a while since I started working at a web development company using PHP. So I need someone to just give me a quick catch up on what I''m doing wrong.

This is my working code:(Code reading song from hard drive)

private void btnPlay_Click(object sender, EventArgs e)
        {
            wmpPlay.URL = @"c:\song.mp3";
        }





这是收到错误的那个:



This is the one that receives the error:

private void btnPlay_Click(object sender, EventArgs e)
        {
            wmpPlay.URL = @"../Resources/song.mp3";
        }





提前致谢,

Chris



Thanks in advance,
Chris

推荐答案

将你的歌曲粘贴到你的应用程序bin / Debug文件夹中



并在播放按钮点击事件上写下这段代码



Paste your song inside your application bin/Debug folder

and write this code on play button click event

wmpPlay.URL = Application.StartupPath + @"/song.mp3";





谢谢&尊重

Sham



Thanks & regard
Sham


获取你做这样的资源



to get the resource you do something like this

System.Reflection.Assembly objAssembly = System.Reflection.Assembly.GetExecutingAssembly();
objStream = objAssembly.GetManifestResourceStream("My.Namespace.song.mp3");





大概是song.mp3已经正确添加并在编译时标记为嵌入式资源



注意命名空间!!



从StackOverflow,我发现: -





Presumably song.mp3 has been added correctly and flagged as an Embedded Resource on compilation

Note the Namespace !!

From StackOverflow, I found this :-

string[] myResources = objAssembly.GetManifestResourceNames();
foreach(string aresource in myResources) {
   Console.WriteLine(aresource);
}





打印资源名称



我已经看到了一些建议从那里开始 - 要么将资源从objStream写入临时文件并从磁盘播放,要么使用?''WaveOut''类直接播放它(这就是我要做的do)



''g''



to print the names of the resources

Ive seen a couple of suggestions where to go from there - either writing the resource from objStream to a temporary file and playing it from disk, or using ?''WaveOut'' classes to play it directly (which is what I''d do)

''g''


这篇关于在C#应用程序中播放音乐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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