Resolve WPF to implement loop and sequential playback of audio files

Source: Internet
Author: User

To play audio files cyclically and sequentially Based on WPF, you must first understand the categories used to control audio in WPF.
There are two main audio control classes in WPF. Here we will make a comparison:
1. SoundPlayer
2. MediaPlayer
Derivative MediaElement

1. SoundPlayer class
1. Based on. net framework 2.0;
2. You can play WAV audio files;
3. Only one file can be played. After multiple files are simultaneously played, the playback operation of one file terminates the previous file;
4. Volume cannot be controlled;
Ii. MediaPlayer
1. Based on WPF;
2. Multiple audio files are supported;
3. Multiple sounds can be played simultaneously;
4. You can adjust the volume to control the audio;
5. Supports mute and left and right speakers;
6. You can control the audio playback speed, obtain the playback progress, and control the progress;

MediaElement is similar to MediaPlayer. A tag available as a WPF page is derived from MediaPlayer;
The development idea of loop playback of audio files in WPF:
First, create a class that inherits MediaElement;
This class includes the playback logic function:
1. Read all audio files in the specified folder;
2. Put the paths of the read files into the List;
3. Read the names of objects in the list in sequence;
4. Play the audio file;
5. After playing the video, read the next file name until the end of the list;
6. After the audio file is played to the end of the list, the list header is converted to continue playing;
Load this class on the XAML interface;
In the Window Load event, execute the playlist of this class;

Below is the code for loop playback of audio files in WPF:

Copy codeThe Code is as follows: WPF interface code
<Window x: Class = "MediaApplication. MainWindow"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Xmlns: md = "clr-namespace: MediaApplication"
Title = "MainWindow" Height = "350" Width = "525" Loaded = "Window_Loaded">
<StackPanel>
<Md: MediaManager x: Name = "media"> </md: MediaManager>
</StackPanel>
</Window>

Copy codeThe Code is as follows:CS code on the WPF Interface
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Windows;
Using System. Windows. Controls;
Using System. Windows. Data;
Using System. Windows. Documents;
Using System. Windows. Input;
Using System. Windows. Media;
Using System. Windows. Media. Imaging;
Using System. Windows. Navigation;
Using System. IO;
Using System. Collections. ObjectModel;
Using System. Configuration;

Namespace MediaApplication {
/// <Summary>
/// Interaction logic for MainWindow. xaml
/// </Summary>
Public partial class MainWindow: Window {
Public MainWindow (){
InitializeComponent ();
}

Private void Window_Loaded (object sender, RoutedEventArgs e ){
This. media. PlayList ();
}

}
}

Copy codeThe Code is as follows:MediaManager class
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Windows. Controls;
Using System. IO;
Using System. Configuration;
Using System. Windows;
Using System. Collections. ObjectModel;
Namespace MediaApplication {
Public class MediaManager: MediaElement {
Public MediaManager (){
Try {
GetAllDirList (new DirectoryInfo (ConfigurationManager. receivettings ["dir"]. ToString ()));
} Catch {
}
}
Public void PlayList (){
If (files. Count> 0)
{
This. UnloadedBehavior = MediaState. Manual;
This. LoadedBehavior = MediaState. Manual;
This. MediaEnded + = new RoutedEventHandler (media_MediaEnded );
This. Source = new Uri (files [index], UriKind. RelativeOrAbsolute );
This. Play ();
}
}
Private void GetAllDirList (DirectoryInfo directory ){
Foreach (string filter in filters)
{
Foreach (FileInfo file in directory. GetFiles (filter )){
Files. Add (file. FullName );
}
}
Foreach (DirectoryInfo subDirectory in directory. GetDirectories ()){
GetAllDirList (subDirectory );
}
}
Private void media_MediaEnded (object sender, RoutedEventArgs e ){
This. Source = new Uri (files [++ index % files. Count], UriKind. RelativeOrAbsolute );
This. Play ();
}
Private ObservableCollection <string> files = new ObservableCollection <string> ();
Private int index = 0;
Private string [] filters = new string [] {"*. wav", "*. mp3 "};
}
}

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.