339.2.1 explain the concept of media control

33

Upload: others

Post on 19-Jan-2022

3 views

Category:

Documents


0 download

TRANSCRIPT

339.2.1 Explain the concept of media control interface

339.2.2 identify playable media types

339.2.3 Apply basic multimedia functions to select, open and play devices

The Multimedia control allows you to manage Media Control Interface (MCI) devices. These devices include: sound boards, MIDI sequencers, CD-ROM drives, audio players, videodisc players, and videotape recorders and players.

Devices are considered to be of two types: simple and compound.

Compound devices, however, require a data file for playback.

Which buttons you use, and the functions provided to you by the Multimedia control, depend on the hardware and software configurations of a particular machine.

Driver support for many multimedia devices (audio and video files, for instance) is provided for in the Windows 95 (or later) and Windows NT operating systems.

The following table lists some of the devices supported by the Multimedia control and the string required by the Device Type property to use the device. Those listing an accompanying file type are compound devices.

Device type String File Type Description

CD audio CDAudio CD audio player

Digital Audio Tape DAT Digital audio tape player

Digital video DigitalVideo Digital video in a window (not GDI-based)

Other Other Undefined MCI device

Overlay Overlay Overlay device

Scanner Scanner Image scanner

Sequencer Sequencer .mid Musical Instrument Digital Interface (MIDI) sequencer

Vcr VCR Video cassette recorder or player

AVI AVIVideo .avi Audio Visual Interleaved video.

Videodisc Videodisc Videodisc player

Wave audio Waveaudio .wav Wave device that plays digitized waveform files.

The Multimedia control uses a set of high-level, device-independent commands, known as Media Control Interface commands, that control various multimedia devices. Many of these commands correspond directly to a button on the Multimedia control.

The Multimedia control is essentially a Visual Basic interface to this command set.

The following table lists the MCI commands used by the Multimedia control, along with their Win32 equivalents:

Command MCI Command Description Open MCI_OPEN Opens a MCI device. Close MCI_CLOSE Closes a MCI device. Play MCI_PLAY Plays a MCI device. Pause MCI_PAUSE or

MCI_RESUME Pauses playing or recording.

Stop MCI_STOP Stops a MCI device. Back MCI_STEP Steps backward through available

tracks.

Command MCI Command Description Back MCI_STEP Steps backward through available tracks. Step MCI_STEP Steps forward through available tracks. Prev MCI_SEEK Goes to the beginning of the current track

using the Seek command. If executed within three seconds of the previous Prev command, goes to the beginning of the previous track or to the beginning of the first track if at the first track.

Next MCI_SEEK Goes to the beginning of the next track (if at last track, goes to the beginning of the last track) using the Seek command.

Seek MCI_SEEK Seeks track forward or backward. Record MCI_RECORD Records MCI device input. Eject MCI_SET Ejects Audio CD from CD drive. Save MCI_SAVE Saves an open file.

The Multimedia Control can be either visible or invisible at run time by setting the Enabled and Visible properties.

In most cases, the default functionality of the individual buttons is sufficient to manage MCI devices. However, the Multimedia control contains run-time properties which allow you to augment or redefine the button commands.

After placing the Multimedia control on a form, whether it is set to be visible or not, the first step is accessing the MCI device. To do this, you set a number of run-time properties. For example:

'Set initial property values of the media device MMControl1.Notify = False MMControl1.Wait = True MMControl1.Shareable = False MMControl1.DeviceType = "CDAudio"

The Notify property, if set to True, generates a Done event when next command is completed.

The Done event provides useful feedback indicating the success or failure of the command. The Wait property determines whether or not the Multimedia control waits for the next command to complete before returning control to the application.

The Shareable property either restricts or allows use of the media device by other applications or processes. The Device Type property is used to specify the type of MCI device.

To properly manage multimedia and system resources, close those MCI devices that are open before exiting your application. You can place the following statement in the Form_Unload procedure to close an open MCI device when the form containing the Multimedia control is unloaded.

Private Sub Form_Unload (Cancel as Integer) Form1.MMControl1.Command = "Close"

End Sub

To begin, start a new project in Visual Basic. Add an instance of the Multimedia control to the form. In the form's Form_Load event procedure add the following code:

'Set initial property values of the media device Form1.MMControl1.Notify = False Form1.MMControl1.Wait = True Form1.MMControl1.Shareable = False Form1.MMControl1.DeviceType = "CDAudio" 'Open the media device Form1.MMControl1.Command = "Open"

The Multimedia control recognizes what actions are relevant for the current state of a device. Therefore, the appropriate buttons are automatically enabled when supported MCI devices are opened with the Multimedia control. In this example, when the Play button is pressed, the Stop and Pause buttons are enabled. If the CD is removed from the CD drive while the application is running, all the buttons become disabled.

Figure 1: A simple audio CD player

Launch Visual Basic, choose File > New Project and create a new Windows Application. Size your form to a largish size and add one label, two buttons and a listbox. Set the text property for each of these objects as follows:

The Windows Media Player control isn’t in the list of tools so you need to add it to the toolbox before you can use it in your project.

Form1 Audio Player Label1 Select a file to play Button1 Load and play the file Button2 Close

To do this, in the toolbox right click the Components collection and choose Choose Items. When the Choose Toolbox Items dialog opens click the COM Components tab, enable the checkbox for Windows Media Player and click Ok. The Windows Media Player control now appears in your Components collection so you can click to select it and drag the player object onto your form.

Use the COM Components dialog to add the Windows Media Player control to your toolbox to use in your project.

To add the code to your project, double click the form and add this code inside the Form1_load event:

The code is used to populate the list box with the files is placed in the form_load event and the player is managed with a button.

My.Computer.FileSystem.CurrentDirectory = "C:\windows\media" ListBox1.DataSource = My.Computer.FileSystem.GetFiles(My.Computer. FileSystem.CurrentDirectory, FileIO.SearchOption.SearchTopLevelOnly, "*.wav")

This code appears inside the Button1_Click event:

And this code appears inside the Button2_click even End

If ListBox1.SelectedItem <> "" Then Me.AxWindowsMediaPlayer1.URL = ListBox1.SelectedItem.ToString

Else MsgBox(;Please select a file", MsgBoxStyle.OkOnly, "Select a file")

End If

Save your project and test it. When you click the Start Debugging button you'll see the Listbox is automatically populated with the WAV files from your C:\windows\media folder.

Select any one of these files and click the Load and Play File button and the file will be loaded into the Windows Media Player and played automatically. You can continue and select and play any of the files in the list. When you are done click Close to finish.

One change which would improve this application is for file names alone (and not the file and folder name) to appear in the Listbox. This requires a minor rewrite to the Form1_Load event and the Button1_click event.

This is the new Form1_load event code:

My.Computer.FileSystem.CurrentDirectory = "C:\windows\media" For Each File As String

In My.Computer.FileSystem.GetFiles (My.Computer.FileSystem.CurrentDirectory, FileIO.SearchOption.SearchTopLevelOnly, "*.wav") ListBox1.Items.Add(My.Computer.FileSystem.GetFileInfo(File).Name)

Next

And the new Button1_click event code:

The Form1_load event code locates all the WAV files in the c:\windows\media folder but unlike before, it processes each of them to extract only the filename – discarding the path.

If ListBox1.SelectedItem <> "" Then Me.AxWindowsMediaPlayer1.URL = My.Computer.FileSystem.CurrentDirectory + "\" + ListBox1.SelectedItem.ToString

Else MsgBox("Please select a file", MsgBoxStyle.OkOnly, "Select a file")

End If

These filenames are then used by the Listbox1.items.add method to populate the listbox with the filenames. When you run the program you will see filenames alone in the Listbox.

There are two features of Visual Basic that make this application simple to create.

One is the WindowsMediaPlayer control and the other is MyNamespace.

The WindowsMediaPlayer control gives you all the features of the Windows Media Player accessible on a form using a single control and My Namespace lets us access and get the contents of the My Music directory with a single line of code.

Here are the text properties you should set for each control:

To find the WindowsMediaPlayer control, right click the Components entry in the Toolbox, choose Choose Items > COM Components tab and locate the WindowsMediaPlayer control. Enable its checkbox and click Ok. This adds the WindowsMediaPlayer control into the Components collection. Drag and drop the player control onto your form.

Form1 Audio Player Label1 Select a file to play Button1 Load and play the file Button2 Close

Figure 2: Basic form that controls the music player

Use the My.Computer.FileSystem.SpecialDirectories.MyMusic property to find the music files stored in the My Music folder and in its subfolders.

This statement should appear in the Form1 Declarations section:

Public musicNames(1000) As String

This code should be added to the Form1_Load event:

Dim arraycount As Integer = 0 My.Computer.FileSystem.CurrentDirectory = My.Computer.FileSystem.SpecialDirectories.MyMusic For Each File As String In My.Computer.FileSystem.GetFiles(My.Computer.FileSystem.CurrentDirectory, FileIO.SearchOption.SearchAllSubDirectories, "*.wma")

ListBox1.Items.Add(My.Computer.FileSystem.GetFileInfo(File).Name) musicNames(arraycount) =My.Computer.FileSystem.GetFileInfo(File).FullName arraycount = arraycount + 1

Next

The statement declaring an array of 1,000 items will be used to hold the music file names.

If you have a lot of music files, increase the array size to, for example, 10000.

This array is used to store both the filenames and the associated file paths of all the WMA files retrieved.

The filenames only of these files are displayed in the Listbox. To avoid confusion, the array has the same ordering as the listbox so each filename that appears in the Listbox has an associated entry in the musicNames array.

The array is required because although we're are only displaying the filename in the Listbox, both the filename and the path must be passed to the WindowsMediaPlayer control in order to play the file.

In this Form1_Load event, the Listbox is populated with the filenames and, at the same time, the array is filled with filenames and paths.

This code should be added to the Button1_Click event:

This is the code for the Button2_click event:

If ListBox1.SelectedItem <> "" Then Me.AxWindowsMediaPlayer1.URL = musicNames(ListBox1.SelectedIndex)

Else MsgBox("Please select a file", MsgBoxStyle.OkOnly, "Select a file")

End If

End

Figure 3: Audio Player Output