前面介绍了Windows Phone 录制音频和Windows Phone 保存录音,录制的音频保存为WAV格式。在WindowsPhone中播放音频的方式有很多种,下面就介绍一种专一用于播放WAV格式的播放方式。需要用到SoundEffect和SoundEffectInstance两个类,这两个类属于 XNAFramework ,所以需要添加引用Microsoft.Xna.Framework。
1.同录制音频一样需要透过指定一个定期执行FrameworkDispatcher.Update() 的事件。
//设置定时器 DispatcherTimer timer = new DispatcherTimer(); timer.Interval = TimeSpan.FromMilliseconds(33); timer.Tick += delegate { try { FrameworkDispatcher.Update(); } catch { } }; timer.Start();
2.获取WAV文件流,用于创建SoundEffect对象。
//获取WAV文件流 Stream stream = null; //如果是资源文件处理 StreamResourceInfo info = Application.GetResourceStream(new Uri(path, UriKind.Relative)); if (info != null) { stream = info.Stream; } //如果是独立存储文件处理 using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication()) { //打开文件 stream = myIsolatedStorage.OpenFile(path, FileMode.Open, FileAccess.Read); }
3.创建SoundEffect对象,播放音频。
//创建音频播放实例 SoundEffect sound = SoundEffect.FromStream(stream); SoundEffectInstance soundInstance = sound.CreateInstance(); //设置循环播放 soundInstance.IsLooped = true; //启动播放 soundInstance.Play();
4.音频暂停,复位,停止。
//暂停 soundInstance.Pause(); //复位 soundInstance.Resume(); //停止 soundInstance.Stop();
5.设置音频播放的音量。
//音量取值范围为[0,1],默认值为0.85 soundInstance.Volume = 0.5F;
使用SoundEffect和SoundEffectInstance方式只能播放WAV格式的音频,并且还需要引入XNA库。
类名:DeviceNetworkInformation
CellularMobileOperator获取蜂窝移动运营商的名称。
IsCellularDataEnabled获取一个值,该值指示网络是否启用了蜂窝数据。
IsCellularDataRoamingEnabled获取一个值,该值指示网络是否允许数据漫游。
IsNetworkAvailable获取一个值,该值指示网络是否可用。
IsWiFiEnabled获取一个值,该值指示网络是否启用了 Wi-Fi。
範例程式:實作註冊監控網路狀態的變動事件與識別變動後的網路狀態。
// Constructor public MainPage() { InitializeComponent(); Initialization(false); }
private void Initialization(bool pIsRegist) { //識別是否啟動行動網路 tgsIcde.IsChecked = DeviceNetworkInformation.IsCellularDataEnabled; //識別是否啟動數據漫游 tgsIcdr.IsChecked = DeviceNetworkInformation.IsCellularDataRoamingEnabled; //識別設備網路是否有網路功能 tgsIna.IsChecked = DeviceNetworkInformation.IsNetworkAvailable; //識別是否啟動WiFi tgsIwifi.IsChecked = DeviceNetworkInformation.IsWiFiEnabled;
//避免測試狀態為airplane mode if (DeviceNetworkInformation.CellularMobileOperator!= null) { //識別CellularMobileOperator的類型 switch (DeviceNetworkInformation.CellularMobileOperator.ToLower()) { case "chunghwa" tblCellularOperator.Text = "中華"; break; case "taiwanmobile" tblCellularOperator.Text = "台哥大"; break; case "fetnet" tblCellularOperator.Text = "遠傳"; break; } }
if (pIsRegist == false) { //註冊監控網路狀態 DeviceNetworkInformation.NetworkAvailabilityChanged += new EventHandler<NetworkNotificationEventArgs>(NetworkAvailabilityChanged); } }
void NetworkAvailabilityChanged(object sender, NetworkNotificationEventArgs e) { //取得info來識別目前網路資訊 NetworkInterfaceInfo tInfo = e.NetworkInterface;
//取得網路狀態通知類型 NetworkNotificationType tNotification = e.NotificationType; string tType = string.Empty; switch (tNotification) { case NetworkNotificationType.CharacteristicUpdate tType += "CharacteristicUpdate \n"; break; case NetworkNotificationType.InterfaceConnected tType += "InterfaceConnected \n"; break; case NetworkNotificationType.InterfaceDisconnected tType += "InterfaceDisconnected \n"; break; } Dispatcher.BeginInvoke(() => { tblNetworkState.Text = tType; Initialization(true); }); }
private void ApplicationBarIconButton_Click(object sender, EventArgs e) { //利用ConnectionSettingsTask修改網路狀態來測試監控網路狀態事件 ConnectionSettingsTask tConnectionSettings = new ConnectionSettingsTask(); tConnectionSettings.ConnectionSettingsType = ConnectionSettingsType.AirplaneMode; tConnectionSettings.Show(); } |
執行結果: