基于WPF的音频文件循环顺序播放

 要做基于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界面代码
   
     
1 < Window x:Class = " MediaApplication.MainWindow "
2 xmlns = " http://schemas.microsoft.com/winfx/2006/xaml/presentation "
3 xmlns:x = " http://schemas.microsoft.com/winfx/2006/xaml "
4 xmlns:md = " clr-namespace:MediaApplication "
5 Title = " MainWindow " Height = " 350 " Width = " 525 " Loaded = " Window_Loaded " >
6 < StackPanel >
7 < md:MediaManager x:Name = " media " ></ md:MediaManager >
8 </ StackPanel >
9   </ Window >

 

WPF界面CS代码
   
     
1 using System;
2   using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Windows;
6 using System.Windows.Controls;
7 using System.Windows.Data;
8 using System.Windows.Documents;
9 using System.Windows.Input;
10 using System.Windows.Media;
11 using System.Windows.Media.Imaging;
12 using System.Windows.Navigation;
13 using System.IO;
14 using System.Collections.ObjectModel;
15 using System.Configuration;
16
17 namespace MediaApplication {
18 /// <summary>
19 /// Interaction logic for MainWindow.xaml
20 /// </summary>
21 public partial class MainWindow : Window {
22 public MainWindow() {
23 InitializeComponent();
24 }
25
26 private void Window_Loaded( object sender, RoutedEventArgs e) {
27 this .media.PlayList();
28 }
29
30
31 }
32 }

  

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

    

 

  转载时,请注明本文来源:www.cnblogs.com/tmywu

   

  作者: 淘米部落

  联系邮箱:[email protected]  

    

 

 

你可能感兴趣的:(WPF)