Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize cyclic Sequential playback of Audio Files by WPF

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you "WPF how to achieve audio file loop sequence playback", the content is easy to understand, clear, hope to help you solve doubts, the following let the editor lead you to study and learn "WPF how to achieve audio file loop sequence playback" this article.

To do cyclic sequential playback of audio files based on WPF, it is necessary to know which classes are used to control audio under WPF.

There are two main audio control classes under WPF. Here's a comparison:

1.SoundPlayer

2.MediaPlayer

Derived MediaElement

I. SoundPlayer class

1. Based on .NET FRAMEWORK 2.0

two。 Can play WAV audio files;

3. Only one file can be played, and the playback operation of one file after playing multiple files at the same time will terminate the previous file.

4. Unable to control the volume

II. MediaPlayer class

1. Based on WPF;

two。 Support for multiple audio files

3. Multiple sounds can be played at the same time

4. You can adjust the volume to control the audio.

5. Mute and left and right speakers are supported.

6. Can control the audio playback speed and obtain the playback progress and control the progress;

The function of the MediaElement class is similar to that of MediaPlayer, and the tags available as WPF pages are a derivative of MediaPlayer.

The development idea of cyclic sequential playback of audio files under WPF:

First create a new class that inherits MediaElement

This class contains the playback logic function:

1. Read all audio files in the specified folder

two。 Put the read file path into the list

3. Read the file names in the list sequentially

4. Play audio files

5. After playback, read the next file name until the end of the list.

6. When the audio file is played to the end of the list, the list header is converted to continue to play.

Load this class in the XAML interface

The playlist that executes this class in the Window Load event

The code for cyclic sequential playback of audio files under WPF is posted below:

The copy code is as follows:

WPF interface code

The copy code is 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 {

/ / /

/ Interaction logic for MainWindow.xaml

/ / /

Public partial class MainWindow: Window {

Public MainWindow () {

InitializeComponent ()

}

Private void Window_Loaded (object sender, RoutedEventArgs e) {

This.media.PlayList ()

}

}

}

The copy 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.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 files = new ObservableCollection ()

Private int index = 0

Private string [] filters = new string [] {"* .wav", "* .mp3"}

}

}

The above is all the contents of the article "how to achieve cyclic sequential playback of audio files by WPF". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report