Parsing WPF to achieve audio file loop sequence playback solution

  • 2020-06-07 04:22:43
  • OfStack

The first thing to do with WPF-based audio file loop sequence playback is to understand what classes are available under WPF to control the audio.
There are mainly two audio control classes under WPF. Here is a comparison:
1. SoundPlayer
2. MediaPlayer
The derived MediaElement

1. SoundPlayer class
1. NET FRAMEWORK 2.0;
2. WAV audio files can be played;
3. Only one file can be played, and the operation of playing one file after playing multiple files at the same time will terminate the file played before;
4. Can't control the volume;
2. MediaPlayer class
1. Based on WPF;
2. Supports multiple audio files;
3. Can play multiple sounds at the same time;
4. The volume can be adjusted to control the audio;
5. Mute and left and right speakers are supported;
6. Can control the audio playback speed and get the playback progress and control the progress;

MediaElement is similar to MediaPlayer in function and is a derivative of MediaPlayer as the tag available for WPF pages.
Development idea of audio file loop sequence playback under WPF:
First, create a new class to inherit from MediaElement.
This class contains the playback logic:
1. Read all audio files in the specified folder;
2. Put the read file path into the list;
3. Read the file names in the list in order;
4. Play audio files;
5. Read the next file name until the end of the list;
6. Turn the audio file to the end of the list and continue to play;
Load this class in the XAML interface;
The Window Load event executes the playlist of this class;

The code of audio file loop sequence playback under WPF is posted below:


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>


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();
         }

 
     }
 }


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" };
    }
}


Related articles: