Udostępnij za pośrednictwem


My.Computer.Audio.Play Method

Plays a .wav sound file.

' Usage
My.Computer.Audio.Play(location)
My.Computer.Audio.Play(location ,playMode)
My.Computer.Audio.Play(data ,playMode)
My.Computer.Audio.Play(stream ,playMode)
' Declaration
Public Sub Play( _
   ByVal location As String _
)
' -or-
Public Sub Play( _
   ByVal location As String, _
   ByVal playMode As AudioPlayMode _
)
' -or-
Public Sub Play( _
   ByVal data As Byte(), _
   ByVal playMode As AudioPlayMode _
)
' -or-
Public Sub Play( _
   ByVal stream As System.IO.Stream, _
   ByVal playMode As AudioPlayMode _
)

Parameters

  • location
    A String containing the name of the sound file

  • data
    Byte array that represents the sound file.

  • stream
    Stream that represents the sound file.

  • playMode
    AudioPlayMode Enumeration mode for playing the sound. By default, AudioPlayMode.Background.

Exceptions

The following conditions can cause an exception:

Remarks

The Play method plays the .wav sound file stored as a file at location, as a byte array in data, or as a stream in stream.

If the overload that takes only the location parameter is used, the Play method plays the sound in the background. Otherwise, the playMode parameter determines how the sound will play.

playMode

Description

AudioPlayMode.Background

Plays the sound in the background. The calling code continues to execute.

AudioPlayMode.BackgroundLoop

Plays the sound in the background until the My.Computer.Audio.Stop Method is called. The calling code continues to execute.

AudioPlayMode.WaitToComplete

Plays the sound and waits until it completes before the calling code continues.

Background playing lets the application execute other code while the sound plays. For more information, see How to: Play Looping Sounds in Visual Basic and How to: Play Sounds in Visual Basic.

Tasks

The following table lists examples of tasks involving the My.Computer.Audio.Play method.

To

See

Play a sound once

How to: Play Sounds and Wait For Completion in Visual Basic

Play a sound many times

How to: Play Looping Sounds in Visual Basic

Play a sound in the background

How to: Play Sounds in Visual Basic

Example

The My.Computer.Audio.Play method plays the specified sound in the background when PlayMode.Background is specified.

Sub PlayBackgroundSoundFile()
    My.Computer.Audio.Play("C:\Waterfall.wav", _
        AudioPlayMode.Background)
End Sub

This code example can only run within a Windows Forms application.

The file name should reference a .wav sound file on your system.

To simplify the management of your sound files, consider storing the files as application resources. They can then be accessed through the My.Resources Object.

Requirements

Namespace:Microsoft.VisualBasic.Devices

Class:Audio

Assembly: Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)

Availability by Project Type

Project type

Available

Windows Application

Yes

Class Library

Yes

Console Application

Yes

Windows Control Library

Yes

Web Control Library

No

Windows Service

Yes

Web Site

No

Permissions

The following permissions may be necessary:

Permission

Description

FileIOPermission

Controls the ability to access files and folders. Associated enumeration: Unrestricted.

SecurityPermission

Describes a set of security permissions applied to code. Associated enumeration: ControlThread.

For more information, see Code Access Security and Requesting Permissions.

See Also

Tasks

How to: Play Sounds and Wait For Completion in Visual Basic

How to: Play Looping Sounds in Visual Basic

How to: Play Sounds in Visual Basic

Reference

My.Computer.Audio Object

My.Computer.Audio.Stop Method

AudioPlayMode Enumeration

System.IO.Stream

Audio.Play