Resolving WPF implementation of the sequential playback of audio files in a circular sequence _ practical Tips

Source: Internet
Author: User

To do a WPF-based audio file looping order playback first, understand what classes under WPF are used to control audio.
There are two main audio-controlled classes under WPF, and here are the comparisons:
1.SoundPlayer
2.MediaPlayer
Derived MediaElement

I. SoundPlayer class
1 based on the. NET FRAMEWORK 2.0;
2. Can play WAV audio files;
3. Can play only one file, while playing multiple files after the playback of a file will terminate the previous playback of the file;
4. Can not control the volume;
Two. MediaPlayer class
1. Based on WPF;
2. Support a variety of audio files;
3. Can play multiple sounds at the same time;
4. Can adjust the volume of audio control;
5. Support to set mute and left and right speaker;
6. Can control the audio playback speed and obtain the playback progress and control progress;

MediaElement similar MediaPlayer functions, the labels available as WPF pages are derived from MediaPlayer;
The development idea of circular sequential playback of audio files in WPF:
First, a new class inherits MediaElement;
This class contains playback logic features:
1. Read all audio files in the specified folder;
2. Put the read file path into the list;
3. Sequentially read the file name in the list;
4. Play audio files;
5. Read the next filename until the end of the list;
6. Play audio files to the end of the list the transformation list head continues to play;
Loading this class in the XAML interface;
The play List of this class is executed in the Window load event;

The following is a list of code that is played in the looping order of the audio files in WPF:

Copy Code code 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"
"MainWindow" height= "title=" width= "525" loaded= "window_loaded" >
<StackPanel>
<md:mediamanager x:name= "Media" ></md:MediaManager>
</StackPanel>
</Window>

Copy Code code as follows:

WPF Interface CS Code
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 Code code 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.appsettings["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.