如何在C#中播放声音? [英] How to play sound in C#?

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

问题描述

如何在winforms中播放声音.我正在使用以下代码,但无法播放.

How to play sound in winforms. I am using below code but its not play.

Stream audioStream = new MemoryStream(Properties.Resources.TestSound.ReadByte());
            SoundPlayer player = new SoundPlayer(audioStream);
            player.Play();

推荐答案

选中此项,http://msdn.microsoft.com/en-us/library/4y171b18.aspx [ http://msdn.microsoft.com/en-us/library/ms143770.aspx [ ^ ]

传递给stream参数的Stream应该是包含.wav文件的Stream. Stream的Read方法返回的数据应为.wav文件中的数据.
Check this, http://msdn.microsoft.com/en-us/library/4y171b18.aspx[^] & http://msdn.microsoft.com/en-us/library/ms143770.aspx[^]

The Stream passed to the stream parameter should be a Stream containing a .wav file. The data returned by the Read method of the Stream should be the data within a .wav file.


如果要同时播放2个视频,则需要输入3个代码部分:

第1部分:

If you want to play 2 videos at the same time you need to enter 3 parts of code:

Part 1:

using System.Runtime.InteropServices; //with this code you are able to import dll files



第2部分:

下:



Part 2:

under:

public Form1()
        {
            InitializeComponent();
        }




输入代码:




enter code:

[DllImport("winmm.dll")]
        private static extern long mciSendString(
            string command,
            StringBuilder returnValue,
            int returnLength,
            IntPtr winHandle);//imports a dll file and creates a new command




第3部分:




Part 3:

string filename1 = "";//location of first file
mciSendString("close mf1", null, 0, IntPtr.Zero);
mciSendString("open \"" + filename1 + "\" type mpegvideo alias mf1", null, 0, IntPtr.Zero);
mciSendString("play mf1", null, 0, IntPtr.Zero)




这对我有用;如果您想了解有关此代码的更多信息,请搜索MCI(媒体控制接口);一些额外的命令:
暂停MF1
简历MF1
停止MF1
寻求mf1到x//x是某个数字
尝试让他们看看他们做什么




this worked for me;if you want to know more about this code search for MCI(Media Control Interface);Some extra commands:
pause mf1
resume mf1
stop mf1
seek mf1 to x // x is some number
try them to see what they do


我在下面的代码中使用了.将声音文件放入资源"中.
I used below code. Put sound file in Resources.
SoundPlayer player = new SoundPlayer(Properties.Resources.sound1);
                    player.Play();


例如,sound1是媒体文件名.


sound1 is media file name for example.


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

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