Windows Phone 播放音频之SoundEffect


  前面介绍了Windows Phone 录制音频Windows Phone 保存录音录制的音频保存为WAV格式。在WindowsPhone中播放音频的方式有很多种,下面就介绍一种专一用于播放WAV格式的播放方式。需要用到SoundEffectSoundEffectInstance两个类,这两个类属于 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;

复制代码

  使用SoundEffectSoundEffectInstance方式只能播放WAV格式的音频,并且还需要引入XNA库。

 

 

 

 

Windows phone 7 判断网络连接

类名: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();

}

 執行結果:

    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值