Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

在silverlight中如何使用XNA库播放音频

From Wiki
Jump to: navigation, search
WP Metro Icon Multimedia.png
SignpostIcon XAML 40.png
WP Metro Icon XNA.png
SignpostIcon WP7 70px.png
Article Metadata

兼容于
文章
翻译:
最后由 hamishwillee 在 15 Jul 2013 编辑

简介

在这篇文章中,我将展示在silverlight中如何使用XNA库播放音频

如何实现

1.开始创建一个新的Windows Phone应用程序。启动Microsoft Visual Studio,创建一个新项目,选择Windows Phone应用程序模板 2.在Visual Studio解决方案资源管理器中,右键点击你的项目中的 引用 并单击 添加引用 3.在添加引用对话框中,单击.NET选项卡。单击“Microsoft.Xna.Framework”项选中它,然后单击确定。 4.在解决方案资源管理器中,你将会看到“Microsoft.Xna.Framework”项在你的项目的引用文件夹中列出。 5.在你的项目中添加一个文件夹用来放置你的音频(这里我们创建了一个“Audio”文件夹并放了一个“sound.wav”音频文件在里面) 6.在你想要使用XNA音频类编程的源文件中,添加以下代码。

using Microsoft.Xna.Framework.Audio;// for SoundEffect
using System.Windows.Resources;// for StreamResourceInfo

7.在你的代码中,你将创建一个SoundEffect实例

整个代码将会是这样的

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Xna.Framework.Audio;// for SoundEffect
using System.Windows.Resources;// for StreamResourceInfo
 
namespace Audiotest
{
public partial class MainPage : PhoneApplicationPage
{
SoundEffect alarm;
// 构造函数
public MainPage()
{
InitializeComponent();
// 加载音频文件
StreamResourceInfo info = Application.GetResourceStream(
new Uri("Audio/sound.wav", UriKind.Relative));
alarm = SoundEffect.FromStream(info.Stream);
Microsoft.Xna.Framework.FrameworkDispatcher.Update();
alarm.Play();
}
}
}

总结

你的音频文件必须是wav文件(SoundEffect.FromStream方法只对PCM波形音频有效)

This page was last modified on 15 July 2013, at 22:40.
39 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×