要做基于WPF的音頻文件循環(huán)順序播放首先要了解WPF下有哪些類是用于控制音頻的.
WPF下主要有兩個(gè)音頻控制的類,這里做下比較:
1.SoundPlayer
2.MediaPlayer
派生MediaElement
一.SoundPlayer類
1.基于.NET FRAMEWORK 2.0;
2.可播放WAV音頻文件;
3.只能播放一個(gè)文件,同時(shí)播放多個(gè)文件會(huì)后一個(gè)文件的播放操作會(huì)終止前一個(gè)播放的文件;
4.不能對(duì)音量進(jìn)行控制;
二.MediaPlayer類
1.基于WPF;
2.支持多種音頻文件;
3.可以同時(shí)播放多個(gè)聲音;
4.可以調(diào)整音量對(duì)音頻進(jìn)行控制;
5.支持設(shè)置靜音和左右揚(yáng)聲器;
6.可以控制音頻播放速度和獲取播放進(jìn)度和控制進(jìn)度;
MediaElement類同MediaPlayer的功能類似,作為WPF頁面可用的標(biāo)簽是MediaPlayer的衍生;
WPF下音頻文件循環(huán)順序播放的開發(fā)思路:
首先新建一個(gè)類繼承MediaElement;
這個(gè)類包含播放邏輯功能:
1.讀取指定文件夾內(nèi)的所有音頻文件;
2.將讀取的文件路徑放入列表;
3.順序讀取列表中的文件名;
4.播放音頻文件;
5.播放完畢讀取下一個(gè)文件名直至列表結(jié)尾;
6.播放音頻文件至列表結(jié)尾則轉(zhuǎn)制列表頭繼續(xù)播放;
在XAML界面加載這個(gè)類;
Window Load事件里執(zhí)行這個(gè)類的播放列表;
下面貼出了WPF下音頻文件循環(huán)順序播放的代碼:
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"
style="margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; background: rgb(242, 246, 251); width: 640px; clear: both; border-top: 1px solid rgb(0, 153, 204); border-right: 1px solid rgb(0, 153, 204); border-left: 1px solid rgb(0, 153, 204); border-image: initial; border-bottom: none; font-family: tahoma, arial, "Microsoft YaHei";"> 復(fù)制代碼代碼如下:
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" };
}
}