Sdílet prostřednictvím


Console.Beep Metoda

Definice

Přehraje zvuk pípnutí přes reproduktor konzoly.

Přetížení

Beep()

Přehraje zvuk pípnutí přes reproduktor konzoly.

Beep(Int32, Int32)

Přehraje zvuk pípnutí zadané frekvence a doby trvání prostřednictvím reproduktoru konzoly.

Beep()

Zdroj:
Console.cs
Zdroj:
Console.cs
Zdroj:
Console.cs

Přehraje zvuk pípnutí přes reproduktor konzoly.

public:
 static void Beep();
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public static void Beep ();
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
[System.Runtime.Versioning.UnsupportedOSPlatform("android")]
[System.Runtime.Versioning.UnsupportedOSPlatform("ios")]
[System.Runtime.Versioning.UnsupportedOSPlatform("tvos")]
public static void Beep ();
public static void Beep ();
[<System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
static member Beep : unit -> unit
[<System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
[<System.Runtime.Versioning.UnsupportedOSPlatform("android")>]
[<System.Runtime.Versioning.UnsupportedOSPlatform("ios")>]
[<System.Runtime.Versioning.UnsupportedOSPlatform("tvos")>]
static member Beep : unit -> unit
static member Beep : unit -> unit
Public Shared Sub Beep ()
Atributy

Výjimky

Tato metoda byla spuštěna na serveru, například SQL Server, který neumožňuje přístup k uživatelskému rozhraní.

Příklady

Následující příklad ukazuje metodu Beep . V příkladu se jako argument příkazového řádku přijme číslo od 1 do 9 a přehraje pípnutí tohoto počtu.

// This example demonstrates the Console.Beep() method.
using namespace System;
int main()
{
   array<String^>^args = Environment::GetCommandLineArgs();
   int x = 0;
   
   //
   if ( (args->Length == 2) && (Int32::TryParse( args[ 1 ],  x ) == true) && ((x >= 1) && (x <= 9)) )
   {
      for ( int i = 1; i <= x; i++ )
      {
         Console::WriteLine( "Beep number {0}.", i );
         Console::Beep();

      }
   }
   else
      Console::WriteLine( "Usage: Enter the number of times (between 1 and 9) to beep." );
}

/*
This example produces the following results:

>beep
Usage: Enter the number of times (between 1 and 9) to beep

>beep 9
Beep number 1.
Beep number 2.
Beep number 3.
Beep number 4.
Beep number 5.
Beep number 6.
Beep number 7.
Beep number 8.
Beep number 9.

*/
// This example demonstrates the Console.Beep() method.
using System;

class Sample
{
    public static void Main(String[] args)
    {
    int x = 0;
//
    if ((args.Length == 1) &&
        (Int32.TryParse(args[0], out x) == true) &&
        ((x >= 1) && (x <= 9)))
        {
        for (int i = 1; i <= x; i++)
            {
            Console.WriteLine("Beep number {0}.", i);
            Console.Beep();
            }
        }
    else
        {
            Console.WriteLine("Usage: Enter the number of times (between 1 and 9) to beep.");
        }
    }
}
/*
This example produces the following results:

>beep
Usage: Enter the number of times (between 1 and 9) to beep

>beep 9
Beep number 1.
Beep number 2.
Beep number 3.
Beep number 4.
Beep number 5.
Beep number 6.
Beep number 7.
Beep number 8.
Beep number 9.

*/
// This example demonstrates the Console.Beep() method.
open System

[<EntryPoint>]
let main args =
    if args.Length = 1 then
        match Int32.TryParse args[0] with
        | true, x when x >= 1 && x <= 9 ->
            for i = 1 to x do
                Console.WriteLine $"Beep number {i}."
                Console.Beep()
        | _ ->
            Console.WriteLine "Usage: Enter the number of times (between 1 and 9) to beep." 
    else
        Console.WriteLine "Usage: Enter the number of times (between 1 and 9) to beep."

    0

// This example produces the following results:

// >beep
// Usage: Enter the number of times (between 1 and 9) to beep

// >beep 9
// Beep number 1.
// Beep number 2.
// Beep number 3.
// Beep number 4.
// Beep number 5.
// Beep number 6.
// Beep number 7.
// Beep number 8.
// Beep number 9.
' This example demonstrates the Console.Beep() method.
Class Sample
   Public Shared Sub Main(args() As String)
      Dim x As Integer = 0
      Dim i As Integer
      '
      If      (args.Length = 1) _
      AndAlso (Int32.TryParse(args(0), x) = True) _
      AndAlso ((x >= 1) AndAlso (x <= 9)) Then
         For i = 1 To x
            Console.WriteLine("Beep number {0}.", i)
            Console.Beep()
         Next i
      Else
         Console.WriteLine("Usage: Enter the number of times (between 1 and 9) to beep.")
      End If
   End Sub
End Class
'
'This example produces the following results:
'
'>beep
'Usage: Enter the number of times (between 1 and 9) to beep
'
'>beep 9
'Beep number 1.
'Beep number 2.
'Beep number 3.
'Beep number 4.
'Beep number 5.
'Beep number 6.
'Beep number 7.
'Beep number 8.
'Beep number 9.
'

Poznámky

Ve výchozím nastavení se pípnutí přehrává s frekvencí 800 hertzů po dobu 200 milisekund.

Beep zabalí volání funkce Windows Beep. To, jestli Beep ve verzích Windows před Windows 7 vydá zvuk, závisí na přítomnosti programovatelného intervalového čipu 8254. Počínaje Windows 7 závisí na výchozím zvukovém zařízení.

Platí pro

Beep(Int32, Int32)

Zdroj:
Console.cs
Zdroj:
Console.cs
Zdroj:
Console.cs

Přehraje zvuk pípnutí zadané frekvence a doby trvání prostřednictvím reproduktoru konzoly.

public:
 static void Beep(int frequency, int duration);
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
public static void Beep (int frequency, int duration);
public static void Beep (int frequency, int duration);
[<System.Runtime.Versioning.SupportedOSPlatform("windows")>]
static member Beep : int * int -> unit
static member Beep : int * int -> unit
Public Shared Sub Beep (frequency As Integer, duration As Integer)

Parametry

frequency
Int32

Frekvence pípání, v rozmezí od 37 do 32767 hertz.

duration
Int32

Doba trvání pípnutí měřená v milisekundách.

Atributy

Výjimky

frequency je menší než 37 nebo více než 32767 hertzů.

-nebo-

duration je menší než nebo rovno nule.

Tato metoda byla spuštěna na serveru, například SQL Server, který neumožňuje přístup ke konzole.

Aktuální operační systém není Windows.

Příklady

Tento příklad demonstruje metodu Beep přehráním prvních několika poznámek skladby prostřednictvím reproduktoru konzoly.

// This example demonstrates the Console.Beep(Int32, Int32) method
using namespace System;
using namespace System::Threading;
ref class Sample
{
protected:

   // Define the frequencies of notes in an octave, as well as 
   // silence (rest).
   enum class Tone
   {
      REST = 0,
      GbelowC = 196,
      A = 220,
      Asharp = 233,
      B = 247,
      C = 262,
      Csharp = 277,
      D = 294,
      Dsharp = 311,
      E = 330,
      F = 349,
      Fsharp = 370,
      G = 392,
      Gsharp = 415
   };


   // Define the duration of a note in units of milliseconds.
   enum class Duration
   {
      WHOLE = 1600,
      HALF = Duration::WHOLE / 2,
      QUARTER = Duration::HALF / 2,
      EIGHTH = Duration::QUARTER / 2,
      SIXTEENTH = Duration::EIGHTH / 2
   };


public:

   // Define a note as a frequency (tone) and the amount of 
   // time (duration) the note plays.
   value struct Note
   {
   public:
      Tone toneVal;
      Duration durVal;

      // Define a constructor to create a specific note.
      Note( Tone frequency, Duration time )
      {
         toneVal = frequency;
         durVal = time;
      }


      property Tone NoteTone 
      {

         // Define properties to return the note's tone and duration.
         Tone get()
         {
            return toneVal;
         }

      }

      property Duration NoteDuration 
      {
         Duration get()
         {
            return durVal;
         }

      }

   };


protected:

   // Play the notes in a song.
   static void Play( array<Note>^ tune )
   {
      System::Collections::IEnumerator^ myEnum = tune->GetEnumerator();
      while ( myEnum->MoveNext() )
      {
         Note n =  *safe_cast<Note ^>(myEnum->Current);
         if ( n.NoteTone == Tone::REST )
                  Thread::Sleep( (int)n.NoteDuration );
         else
                  Console::Beep( (int)n.NoteTone, (int)n.NoteDuration );
      }
   }


public:
   static void Main()
   {
      
      // Declare the first few notes of the song, "Mary Had A Little Lamb".
      array<Note>^ Mary = {Note( Tone::B, Duration::QUARTER ),Note( Tone::A, Duration::QUARTER ),Note( Tone::GbelowC, Duration::QUARTER ),Note( Tone::A, Duration::QUARTER ),Note( Tone::B, Duration::QUARTER ),Note( Tone::B, Duration::QUARTER ),Note( Tone::B, Duration::HALF ),Note( Tone::A, Duration::QUARTER ),Note( Tone::A, Duration::QUARTER ),Note( Tone::A, Duration::HALF ),Note( Tone::B, Duration::QUARTER ),Note( Tone::D, Duration::QUARTER ),Note( Tone::D, Duration::HALF )};
      
      // Play the song
      Play( Mary );
   }

};

int main()
{
   Sample::Main();
}

/*
This example produces the following results:

This example plays the first few notes of "Mary Had A Little Lamb" 
through the console speaker.
*/
// This example demonstrates the Console.Beep(Int32, Int32) method
using System;
using System.Threading;

class Sample
{
    public static void Main()
    {
// Declare the first few notes of the song, "Mary Had A Little Lamb".
    Note[] Mary =
        {
        new Note(Tone.B, Duration.QUARTER),
        new Note(Tone.A, Duration.QUARTER),
        new Note(Tone.GbelowC, Duration.QUARTER),
        new Note(Tone.A, Duration.QUARTER),
        new Note(Tone.B, Duration.QUARTER),
        new Note(Tone.B, Duration.QUARTER),
        new Note(Tone.B, Duration.HALF),
        new Note(Tone.A, Duration.QUARTER),
        new Note(Tone.A, Duration.QUARTER),
        new Note(Tone.A, Duration.HALF),
        new Note(Tone.B, Duration.QUARTER),
        new Note(Tone.D, Duration.QUARTER),
        new Note(Tone.D, Duration.HALF)
        };
// Play the song
    Play(Mary);
    }

// Play the notes in a song.
    protected static void Play(Note[] tune)
    {
    foreach (Note n in tune)
        {
        if (n.NoteTone == Tone.REST)
            Thread.Sleep((int)n.NoteDuration);
        else
            Console.Beep((int)n.NoteTone, (int)n.NoteDuration);
        }
    }

// Define the frequencies of notes in an octave, as well as
// silence (rest).
    protected enum Tone
    {
    REST   = 0,
    GbelowC = 196,
    A      = 220,
    Asharp = 233,
    B      = 247,
    C      = 262,
    Csharp = 277,
    D      = 294,
    Dsharp = 311,
    E      = 330,
    F      = 349,
    Fsharp = 370,
    G      = 392,
    Gsharp = 415,
    }

// Define the duration of a note in units of milliseconds.
    protected enum Duration
    {
    WHOLE     = 1600,
    HALF      = WHOLE/2,
    QUARTER   = HALF/2,
    EIGHTH    = QUARTER/2,
    SIXTEENTH = EIGHTH/2,
    }

// Define a note as a frequency (tone) and the amount of
// time (duration) the note plays.
    protected struct Note
    {
    Tone     toneVal;
    Duration durVal;

// Define a constructor to create a specific note.
    public Note(Tone frequency, Duration time)
        {
        toneVal = frequency;
        durVal  = time;
        }

// Define properties to return the note's tone and duration.
    public Tone NoteTone { get{ return toneVal; } }
    public Duration NoteDuration { get{ return durVal; } }
    }
}
/*
This example produces the following results:

This example plays the first few notes of "Mary Had A Little Lamb"
through the console speaker.
*/
// This example demonstrates the Console.Beep(Int32, Int32) method
open System
open System.Threading

// Define the frequencies of notes in an octave, as well as
// silence (rest).
type Tone =
    | REST    = 0
    | GbelowC = 196
    | A       = 220
    | Asharp  = 233
    | B       = 247
    | C       = 262
    | Csharp  = 277
    | D       = 294
    | Dsharp  = 311
    | E       = 330
    | F       = 349
    | Fsharp  = 370
    | G       = 392
    | Gsharp  = 415

// Define the duration of a note in units of milliseconds.
type Duration =
    | WHOLE     = 1600
    | HALF      = 800
    | QUARTER   = 400
    | EIGHTH    = 200
    | SIXTEENTH = 100

// Define a note as a frequency (tone) and the amount of
// time (duration) the note plays.
[<Struct>]
type Note =
    { Tone: Tone
      Duration: Duration }

// Play the notes in a song.
let play tune =
    for n in tune do
        if n.Tone = Tone.REST then
            Thread.Sleep(int n.Duration)
        else
            Console.Beep(int n.Tone, int n.Duration)

// Declare the first few notes of the song, "Mary Had A Little Lamb".
let mary =
    [ { Tone = Tone.B; Duration = Duration.QUARTER }
      { Tone = Tone.A; Duration = Duration.QUARTER }
      { Tone = Tone.GbelowC; Duration = Duration.QUARTER }
      { Tone = Tone.A; Duration = Duration.QUARTER }
      { Tone = Tone.B; Duration = Duration.QUARTER }
      { Tone = Tone.B; Duration = Duration.QUARTER }
      { Tone = Tone.B; Duration = Duration.HALF }
      { Tone = Tone.A; Duration = Duration.QUARTER }
      { Tone = Tone.A; Duration = Duration.QUARTER }
      { Tone = Tone.A; Duration = Duration.HALF }
      { Tone = Tone.B; Duration = Duration.QUARTER }
      { Tone = Tone.D; Duration = Duration.QUARTER }
      { Tone = Tone.D; Duration = Duration.HALF } ]

// Play the song
play mary

// This example plays the first few notes of "Mary Had A Little Lamb"
// through the console speaker.
' This example demonstrates the Console.Beep(Int32, Int32) method
Imports System.Threading

Class Sample
   Public Shared Sub Main()
      ' Declare the first few notes of the song, "Mary Had A Little Lamb".
      Dim Mary As Note() =  { _
            New Note(Tone.B, Duration.QUARTER), _
            New Note(Tone.A, Duration.QUARTER), _
            New Note(Tone.GbelowC, Duration.QUARTER), _
            New Note(Tone.A, Duration.QUARTER), _
            New Note(Tone.B, Duration.QUARTER), _
            New Note(Tone.B, Duration.QUARTER), _
            New Note(Tone.B, Duration.HALF), _
            New Note(Tone.A, Duration.QUARTER), _
            New Note(Tone.A, Duration.QUARTER), _
            New Note(Tone.A, Duration.HALF), _
            New Note(Tone.B, Duration.QUARTER), _
            New Note(Tone.D, Duration.QUARTER), _
            New Note(Tone.D, Duration.HALF)}
      ' Play the song
      Play(Mary)
   End Sub
   
   ' Play the notes in a song.
   Protected Shared Sub Play(tune() As Note)
      Dim n As Note
      For Each n In  tune
         If n.NoteTone = Tone.REST Then
            Thread.Sleep(CInt(n.NoteDuration))
         Else
            Console.Beep(CInt(n.NoteTone), CInt(n.NoteDuration))
         End If
      Next n
   End Sub
   ' Define the frequencies of notes in an octave, as well as 
   ' silence (rest).
   
   Protected Enum Tone
      REST = 0
      GbelowC = 196
      A = 220
      Asharp = 233
      B = 247
      C = 262
      Csharp = 277
      D = 294
      Dsharp = 311
      E = 330
      F = 349
      Fsharp = 370
      G = 392
      Gsharp = 415
   End Enum 'Tone
   
   ' Define the duration of a note in units of milliseconds.
   
   Protected Enum Duration
      WHOLE = 1600
      HALF = WHOLE / 2
      QUARTER = HALF / 2
      EIGHTH = QUARTER / 2
      SIXTEENTH = EIGHTH / 2
   End Enum 'Duration
   
   ' Define a note as a frequency (tone) and the amount of 
   ' time (duration) the note plays.
   Protected Structure Note
      Private toneVal As Tone
      Private durVal As Duration
      
      ' Define a constructor to create a specific note.
      Public Sub New(frequency As Tone, time As Duration)
         toneVal = frequency
         durVal = time
      End Sub
      
      ' Define properties to return the note's tone and duration.
      Public ReadOnly Property NoteTone() As Tone
         Get
            Return toneVal
         End Get
      End Property
      
      Public ReadOnly Property NoteDuration() As Duration
         Get
            Return durVal
         End Get
      End Property
   End Structure 'Note '
'This example produces the following results:
'
'This example plays the first few notes of "Mary Had A Little Lamb" 
'through the console speaker.
'
End Class

Poznámky

Beep zabalí volání funkce Windows Beep. To, jestli Beep ve verzích Windows před Windows 7 vydá zvuk, závisí na přítomnosti programovatelného intervalového čipu 8254. Počínaje Windows 7 závisí na výchozím zvukovém zařízení.

Platí pro