要做基于WPF的音频文件循环顺序播放首先要了解WPF下有哪些类是用于控制音频的. 
WPF下主要有两个音频控制的类,这里做下比较:
    1.SoundPlayer  
    2.MediaPlayer
     派生MediaElement

一.SoundPlayer类  
    1.基于.NET FRAMEWORK 2.0;
    2.可播放WAV音频文件; 
    3.只能播放一个文件,同时播放多个文件会后一个文件的播放操作会终止前一个播放的文件;
    4.不能对音量进行控制;
二.MediaPlayer类
    1.基于WPF; 
    2.支持多种音频文件;
    3.可以同时播放多个声音;
    4.可以调整音量对音频进行控制;
    5.支持设置静音和左右扬声器;
    6.可以控制音频播放速度和获取播放进度和控制进度;     

MediaElement类同MediaPlayer的功能类似,作为WPF页面可用的标签是MediaPlayer的衍生; 
WPF下音频文件循环顺序播放的开发思路:
首先新建一个类继承MediaElement;
这个类包含播放逻辑功能:
    1.读取指定文件夹内的所有音频文件;
    2.将读取的文件路径放入列表;
    3.顺序读取列表中的文件名;
    4.播放音频文件;
    5.播放完毕读取下一个文件名直至列表结尾;
    6.播放音频文件至列表结尾则转制列表头继续播放;
在XAML界面加载这个类;
Window Load事件里执行这个类的播放列表;

下面贴出了WPF下音频文件循环顺序播放的代码:
复制代码 代码如下:
WPF界面代码
 <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界面CS代码
 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类
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" };
    }
}

广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!