Programing

C #, .NET에서 사운드를 재생하는 방법

lottogame 2020. 8. 12. 22:08
반응형

C #, .NET에서 사운드를 재생하는 방법


C # /. NET으로 작성된 Windows 응용 프로그램이 있습니다.

버튼을 클릭 할 때 특정 사운드를 재생하려면 어떻게해야합니까?


다음을 사용할 수 있습니다.

System.Media.SoundPlayer player = new System.Media.SoundPlayer(@"c:\mywavfile.wav");
player.Play();

예를 들어 SystemSound 를 사용할 수 있습니다 System.Media.SystemSounds.Asterisk.Play();.


Windows Forms의 경우 한 가지 방법은 SoundPlayer

private void Button_Click(object sender, EventArgs e)
{
    using (var soundPlayer = new SoundPlayer(@"c:\Windows\Media\chimes.wav")) {
        soundPlayer.Play(); // can also use soundPlayer.PlaySync()
    }
}

MSDN 페이지

이것은 WPF에서도 작동하지만 MSDN 페이지 사용과 같은 다른 옵션이 있습니다 MediaPlayer .


추가 정보.

이것은 Windows 환경에 매끄럽게 맞추기를 원하는 응용 프로그램에 대한 약간 높은 수준의 대답입니다. 특정 사운드 재생에 대한 기술적 세부 사항은 다른 답변에서 제공되었습니다. 그 외에도 항상 다음 두 가지 사항에 유의하십시오.

  1. 일반적인 시나리오에서 다섯 표준 시스템 사운드를 사용 , 즉,

    • 별표 -현재 이벤트를 강조하고 싶을 때 재생

    • 질문 - 질문으로 재생 (시스템 메시지 상자 창에서이 질문 재생)

    • 느낌표 - 느낌표 아이콘으로 재생 (시스템 메시지 상자 창에서이 아이콘 재생)

    • 경고음 (기본 시스템 사운드)

    • 치명적 정지 ( "Hand")-오류로 재생 (시스템 메시지 상자 창에서이 항목 재생)
       

    수업 방법은 System.Media.SystemSounds당신을 위해 그것들을 재생할 것입니다.
     

  2. 사운드 제어판 에서 사용자가 사용자 정의 할 수있는 기타 사운드 구현

    • 이렇게하면 사용자가 애플리케이션에서 사운드를 쉽게 변경하거나 제거 할 수 있으며이를 위해 사용자 인터페이스를 작성할 필요가 없습니다. 이미 존재합니다.
    • 각 사용자 프로필은 고유 한 방식으로 이러한 사운드를 재정의 할 수 있습니다.
    • 어떻게:

코드 벨로우는 mp3 파일 및 메모리 내 웨이브 파일도 재생할 수 있습니다.

player.FileName = "123.mp3";
player.Play();

에서 http://alvas.net/alvas.audio,samples.aspx#sample6 또는

Player pl = new Player();
byte[] arr = File.ReadAllBytes(@"in.wav");
pl.Play(arr);

에서 http://alvas.net/alvas.audio,samples.aspx#sample7


C #을 사용하여 Windows 형식의 오디오 파일을 재생하려면 다음과 같이 간단한 예제를 확인하십시오.

1. Visual Studio (VS-2008 / 2010 / 2012)-> 파일 메뉴-> 새 프로젝트를 클릭합니다.

2. New Project에서-> Windows Forms Application-> Give Name을 클릭 한 다음 OK를 클릭합니다.

A new "Windows Forms" project will opens.

3.Drag-and-Drop a Button control from the Toolbox to the Windows Form.

4.Double-click the button to create automatically the default Click event handler, and add the following code.

This code displays the File Open dialog box and passes the results to a method named "playSound" that you will create in the next step.

 OpenFileDialog dialog = new OpenFileDialog();
 dialog.Filter = "Audio Files (.wav)|*.wav";


if(dialog.ShowDialog() == DialogResult.OK)
{
  string path = dialog.FileName;
  playSound(path);
}

5.Add the following method code under the button1_Click event hander.

 private void playSound(string path)
 {
   System.Media.SoundPlayer player = new System.Media.SoundPlayer();
   player.SoundLocation = path;
   player.Load();
   player.Play();
 }

6.Now let's run the application just by Pressing the F5 to run the code.

7.Click the button and select an audio file. After the file loads, the sound will play.

I hope this is useful example to beginners...


I think you must firstly add a .wav file to Resources. For example you have sound file named Sound.wav. After you added the Sound.wav file to Resources, you can use this code:

System.Media.SoundPlayer player = new System.Media.SoundPlayer(Properties.Resources.Sound);
player.Play();

This is another way to play sound.

참고URL : https://stackoverflow.com/questions/3502311/how-to-play-a-sound-in-c-net

반응형