[012] embed audio or video in C #

Source: Internet
Author: User

First, add the Windows Media Player control:

 

In. NET winform, there is no hosted music player. APIs can only play in WAV format. For MP3 audio files, MediaPlayer is required.

Usage:

  1. Right-click the toolbox, select "Choose Items", switch to the COM page, find Windows Media Player, select, and OK.
  2. In toolbox, drag and drop the MediaPlayer control you just added to Winform.

Bytes ---------------------------------------------------------------------------------------------------------

1. AxWindowsMediaPlayer attributes:

  • URL: multimedia address, which can be directly assigned a value of the string type!
  • CtlControls:
    • Play (): Play
    • Pause (): Pause
    • Stop (): Stop

 

Attribute/method name: Description:

 

[Basic attributes]

  • URL: String; specifies the media location, local or network address
  • UiMode: String; player interface mode, which can be Full, Mini, None, Invisible
  • PlayState: integer; playback status, 1 = stopped, 2 = paused, 3 = played, 6 = buffering, 9 = connected, 10 = ready
  • EnableContextMenu: Boolean; enable/disable context menu
  • FullScreen: boolean; Whether to display in full screen

// Basic player control

  • Ctlcontrols. play; play
  • Ctlcontrols. pause; pause
  • Ctlcontrols. stop; stop
  • Ctlcontrols. currentPosition: double; current progress
  • Ctlcontrols. currentPositionString: string; current progress, string format. For example"
  • Ctlcontrols. fastForward; Fast Forward
  • Ctlcontrols. fastReverse; fast return
  • Ctlcontrols. next; next
  • Ctlcontrols. previous; previous line

[Settings] wmp. settings // basic player settings

  • Settings. volume: integer; volume: 0-100
  • Settings. autoStart: Boolean; Whether to play automatically
  • Settings. mute: Boolean; mute
  • Settings. playCount: integer; number of playbacks

[CurrentMedia] wmp. currentMedia // current media attribute

  • CurrentMedia. duration: double; total media Length
  • CurrentMedia. durationString: string; total media length, in string format. For example"
  • CurrentMedia. getItemInfo (const string); get current media information "Title" = media Title, "Author" = artist, "Copyright" = Copyright information, "Description" = media content Description, "Duration" = Duration (seconds), "FileSize" = file size, "FileType" = file type, "sourceURL" = original address
  • CurrentMedia. setItemInfo (const string); set media information by attribute name
  • CurrentMedia. name: string; same as currentMedia. getItemInfo ("Title ")

[CurrentPlaylist] wmp. currentPlaylist // attributes of the current playlist

  • CurrentPlaylist. count: integer; number of media contained in the current playlist
  • CurrentPlaylist. Item [integer]; gets or sets the media information of the specified project. Its subattributes are the same as that of wmp. currentMedia.
AxWindowsMediaPlayer1.currentMedia. sourceURL; // obtain the path of the media file being played.
AxWindowsMediaPlayer1.currentMedia. name; // gets the name of the media file being played.
AxWindowsMediaPlayer1.Ctlcontrols. Play
AxWindowsMediaPlayer1.Ctlcontrols. Stop
AxWindowsMediaPlayer1.Ctlcontrols. Pause
AxWindowsMediaPlayer1.Ctlcontrols. PlayCount file playback times
AxWindowsMediaPlayer1.Ctlcontrols. AutoRewind whether to play cyclically
AxWindowsMediaPlayer1.Ctlcontrols. Balance audio channel
AxWindowsMediaPlayer1.Ctlcontrols. Volume
AxWindowsMediaPlayer1.Ctlcontrols. Mute
AxWindowsMediaPlayer1.Ctlcontrols. EnableContextMenu whether to allow right-click the control. A shortcut menu is displayed.
AxWindowsMediaPlayer1.Ctlcontrols. AnimationAtStart whether to play the animation before playing
AxWindowsMediaPlayer1.Ctlcontrols. ShowControls: whether to display the control Toolbar
AxWindowsMediaPlayer1.Ctlcontrols. ShowAudioControls: whether the sound control button is displayed
AxWindowsMediaPlayer1.Ctlcontrols. ShowDisplay: whether the data file information is displayed
Whether the Goto column is displayed in axWindowsMediaPlayer1.Ctlcontrols. ShowGotoBar
AxWindowsMediaPlayer1.Ctlcontrols. ShowPositionControls: whether to display the position adjustment button
AxWindowsMediaPlayer1.Ctlcontrols. ShowStatusBar whether the status bar is displayed
AxWindowsMediaPlayer1.Ctlcontrols. ShowTracker: whether the progress bar is displayed
AxWindowsMediaPlayer1.Ctlcontrols. FastForward fast forward
AxWindowsMediaPlayer1.Ctlcontrols. FastReverse quick return
AxWindowsMediaPlayer1.Ctlcontrols. Rate fast forward/Return Rate
AxWindowsMediaPlayer1.AllowChangeDisplaySize: whether the playback image size can be set freely
AxWindowsMediaPlayer1.DisplaySize: sets the playback image size.
  • 1-MpDefaultSize original size
  • 2-MpHalfSize: half of the original size
  • 3-mpdoublesize: twice the original size
  • 4-MpFullScreen full screen
  • 5-mponesixteenthscreen screen size: 1/16
  • 6-MpOneFourthScreen: 1/4 of screen size
  • 7-mponehalfscreen: 1/2 of screen size

AxWindowsMediaPlayer1.ClickToPlay whether to enable Media Player in the playback window
After a video is played, you can read the width and height of the source video as follows, and then set it to the original size.

        private void ResizeOriginal()
{
int intWidth = axWindowsMediaPlayer1.currentMedia.imageSourceWidth;
int intHeight = axWindowsMediaPlayer1.currentMedia.imageSourceHeight;
axWindowsMediaPlayer1.Width = intWidth + 2;
axWindowsMediaPlayer1.Height = intHeight + 2;
}

Open a media file and play it back:

Dim filePath As String
With Me. OpenFileDialog1
. Title = "open a voice file"
. CheckPathExists = True
. CheckFileExists = True
. Multiselect = False
. Filter = "mp3 file (*. mp3) | *. mp3 | all files (*. *) | *.*"
If. ShowDialog = DialogResult. Cancel Then
Exit Sub
End If
FilePath =. FileName
End
Me. Text = "PC repeater-file" & filePath
AxWindowsMediaPlayer1.URL = filePath
Try
Me. AxWindowsMediaPlayer1.Ctlcontrols. play ()
Catch ex As Exception
MsgBox ("sorry, you cannot play audio files in this format", MsgBoxStyle. OKOnly, "PC repeater ")
Exit Sub
End Try

Note:

In AxWindowsMediaPlayer1.URL, the URL indicates the file Name to be played. The original Name attribute is canceled.
AxWindowsMediaPlayer1.Ctlcontrols. play () playback, other attributes such as Pause and Stop.
AxWindowsMediaPlayer1.settings. balance indicates the audio channel settings for media playback, 0 indicates the balance, and-1 and 1 indicates the Left and Right audio channels.
AxWindowsMediaPlayer1.currentMedia. duration indicates the duration of the file to be played. You can use it to obtain the file length.
AxWindowsMediaPlayer1.Ctlcontrols. currentPosition indicates the current playback position of the file being played. This attribute can be used to set forward and backward for the media file. For example
AxWindowsMediaPlayer1.Ctlcontrols. currentPosition + 1 indicates the first time unit forward.
AxWindowsMediaPlayer1.settings. rate: Specifies the playback rate. Generally, the unit of kbps is displayed after the value is multiplied by 16.

Note: In the above program, if you add:
Msgbox (AxWindowsMediaPlayer1.currentMedia. duration. ToString)
The display result may be 0. Therefore, the playback duration of the file may not be obtained at this time, which is prone to errors. Therefore, you can add a timer control when using it:

Private Sub timereffectick (ByVal sender As Object, ByVal e As System. EventArgs) Handles Timer1.Tick
EndPoint = AxWindowsMediaPlayer1.currentMedia. duration
If EndPoint = 0 Then Exit Sub ', it may take some time to open the media file. It is waiting for the opening of the media file.
Msgbox (AxWindowsMediaPlayer1.currentMedia. duration. ToString)
End Sub

In this case, msgbox displays the playback length of the file.
2. Ctlcontrols attributes
The Ctlcontrols attribute is an important attribute of AxWindowsMediaPlayer, which has many common members.
(1) method play
Used to play multimedia files. The format is:
Form name. Control name. Ctlcontrols. play ()
For example, AxWindowsMediaPlayer1.Ctlcontrols. play () '. The default form name is Me.
(2) method pause
Used to pause a video. The format is as follows:
Form name. Control name. Ctlcontrols. pause ()
For example, AxWindowsMediaPlayer1.Ctlcontrols. pause ()
(3) method stop
The format of a multimedia file used to stop playing is as follows:
Form name. Control name. Ctlcontrols. stop ()
For example, AxWindowsMediaPlayer1.Ctlcontrols. stop ()
(4) fastforward Method
This interface is used to fast forward a video. The format is as follows:
Form name. Control name. Ctlcontrols. fastforward ()
For example, AxWindowsMediaPlayer1.Ctlcontrols. forward ()
(5) fastreverse
Form name. Control name. Ctlcontrols. fastreverse ()
For example, AxWindowsMediaPlayer1.Ctlcontrols. fastreverse ()
6. Attribute CurrentPosition
Used to obtain the current playback progress of a multimedia file. The value is of the numerical type and the format is as follows:
Form name. Control name. Ctlcontrols. currentPosition
D1 = AxWindowsMediaPlayer1.Ctlcontrols. currentPosition
D1 is an integer variable.
7. Attribute Duration
Used to obtain the total playback Time of the current multimedia file. The value is of the numerical type. The format is as follows:
Form name. Control name. currentMedia. duration
For example, d2 = AxWindowsMediaPlayer1.currentMedia. duration
D2 is an integer variable.

 

Source: http://wenku.baidu.com/view/1b89a61a964bcf84b9d57bc3.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.