如何在C#中播放声音?

编程入门 行业动态 更新时间:2024-10-19 14:28:34
本文介绍了如何在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();

推荐答案

选中此项,msdn.microsoft/en-us/library/4y171b18.aspx [ ^ ]& msdn.microsoft/en-us/library/ms143770.aspx [ ^ ] 传递给stream参数的Stream应该是包含.wav文件的Stream. Stream的Read方法返回的数据应为.wav文件中的数据. Check this, msdn.microsoft/en-us/library/4y171b18.aspx[^] & msdn.microsoft/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#中播放声音?

本文发布于:2023-11-14 15:15:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1587758.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:声音   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!