SoFunction
Updated on 2025-03-03

Solution to parse WPF to realize the sequential playback of audio files

To play audio files based on WPF in a sequential order, you must first understand which classes under WPF are used to control audio.
There are two main audio control classes under WPF. Here is a comparison:


Derived MediaElement

1.SoundPlayer class
1.Based on .NET FRAMEWORK 2.0;
2. Can play WAV audio files;
3. Only one file can be played. Playing multiple files at the same time will terminate the previous playback of the file;
4. Cannot 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. You can adjust the volume to control the audio;
5. Support setting of mute and left and right speakers;
6. It can control the audio playback speed, obtain the playback progress and control the progress;

MediaElement is similar to MediaPlayer's functions. The tags available as WPF pages are derived from MediaPlayer;
Development ideas for audio file cyclic playback under WPF:
First create a new class to inherit MediaElement;
This class contains playback logic functions:
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 sequence;
4. Play audio files;
5. After playing, read the next file name until the end of the list;
6. When playing the audio file to the end of the list, convert the list to continue playing;
Load this class in the XAML interface;
The playlist of this class is executed in the Window Load event;

The following is a code for playing audio files in a loop sequence under WPF:

Copy the codeThe code is as follows:

WPF interface code
 <Window x:Class=""
         xmlns="/winfx/2006/xaml/presentation"
         xmlns:x="/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 the codeThe code is as follows:

WPF interface CS code
 using System;
 using ;
 using ;
 using ;
 using ;
 using ;
 using ;
 using ;
 using ;
 using ;
 using ;
 using ;
 using ;
 using ;
 using ;

 namespace MediaApplication {
     /// <summary>
     /// Interaction logic for
     /// </summary>
     public partial class MainWindow : Window {
         public MainWindow() {
             InitializeComponent();
         }

         private void Window_Loaded(object sender, RoutedEventArgs e) {
             ();
         }

 
     }
 }

Copy the codeThe code is as follows:

MediaManager class
using System;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
namespace MediaApplication {
    public class MediaManager : MediaElement {
        public MediaManager() {
            try {
                GetAllDirList(new DirectoryInfo(["dir"].ToString()));
            } catch {
            }
        }
        public void PlayList() {
            if( > 0)
            {
                = ;
                = ;
                += new RoutedEventHandler(media_MediaEnded);
                = new Uri( files[index], );
                ();
            } 
        }
        private void GetAllDirList(DirectoryInfo directory) {           
            foreach(string filter in filters)
            {
                foreach (FileInfo file in (filter)) {
                    ();
                }
            }
            foreach (DirectoryInfo subDirectory in ()) {
                GetAllDirList(subDirectory);
            }
        }
        private void media_MediaEnded(object sender, RoutedEventArgs e) {
            = new Uri( files[++index % ], );
            ();
        }
        private ObservableCollection<string> files = new ObservableCollection<string>();
        private int index = 0;
        private string[] filters = new string[] { "*.wav", "*.mp3" };
    }
}