声音未在我的应用程序中播放 [英] Sound not playing in my application

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

问题描述

我正在我的应用程序中运行1个后台进程...它会不断检查是否有某些输入...找到正确的输入后..它将播放一些wav文件。

I am running 1 background process in my application...it continuously checks for some input...When proper input is entered of found..It will play Some wav file..

我在资源中添加了1个wav填充,命名为 ding.wav。

I have added 1 wav filled named as "ding.wav" into Resources..

我在应用程序中编写了以下代码...
我正在使用System.Media命名空间。并使用.Net 4.0

and i have written following code in my application... I am using System.Media namespace. and using .Net 4.0

SoundPlayer player = new SoundPlayer();
player.Stream = Properties.Resources.ding;
player.Play();

但是声音没有播放...

but sound is not playing...

你能告诉我我做错了吗?

Can you tell me what i am doing wrong..!!

推荐答案

尝试一下:

SoundPlayer player = new SoundPlayer(Properties.Resources.ding);
player.Play();

您还可以尝试以下操作:

You can also try this:

using System;
using System.Runtime.InteropServices;
using System.Resources;
using System.IO;
namespace Win32
{
  public class Winmm
  {
    public const UInt32 SND_ASYNC = 1;
    public const UInt32 SND_MEMORY = 4;

    [DllImport("Winmm.dll")]
    public static extern bool PlaySound(byte[] data, IntPtr hMod, UInt32 dwFlags);
    public Winmm() { }
    public static void PlayWavResource(string wav)
    {
      // get the namespace 
      string strNameSpace= 
        System.Reflection.Assembly.GetExecutingAssembly().GetName().Name.ToString();

      // get the resource into a stream
      Stream str = 
        System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(      strNameSpace +"."+ wav );
      if ( str == null ) return;
     // bring stream into a byte array
     byte[] bStr = new Byte[str.Length];
     str.Read(bStr, 0, (int)str.Length);
     // play the resource
     PlaySound(bStr, IntPtr.Zero, SND_ASYNC | SND_MEMORY);
    }
  }
}

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

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