WP8开发(6)小程序声音库播放 | 您所在的位置:网站首页 › audioplayer函数 › WP8开发(6)小程序声音库播放 |
一:运行预览
二:解决方案截图
三:主要代码
程序入口:App.xaml
using System;
using System.Diagnostics;
using System.Resources;
using System.Windows;
using System.Windows.Markup;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using 声音库.Resources;
using 声音库.ViewModels;
namespace 声音库
{
public partial class App : Application//程序的入口
{
//粗体为主要自写代码
private static SoundModel viewModel = null;//软件整个的模板
public static SoundModel ViewModel//属性:如果模板为空加载数据
{
get
{
if (viewModel == null)
{
viewModel = new SoundModel();
viewModel.LoadData();
}
return viewModel;
}
}
///
/// 提供对电话应用程序的根框架的轻松访问。
///
/// 电话应用程序的根框架。
public static PhoneApplicationFrame RootFrame { get; private set; }
///
/// Application 对象的构造函数。
///
public App()
{
// 未捕获的异常的全局处理程序。
UnhandledException += Application_UnhandledException;
// 标准 XAML 初始化
InitializeComponent();
// 特定于电话的初始化
InitializePhoneApplication();
// 语言显示初始化
InitializeLanguage();
// 调试时显示图形分析信息。
if (Debugger.IsAttached)
{
// 显示当前帧速率计数器
Application.Current.Host.Settings.EnableFrameRateCounter = true;
// 显示在每个帧中重绘的应用程序区域。
//Application.Current.Host.Settings.EnableRedrawRegiOns= true;
// 启用非生产分析可视化模式,
// 该模式显示递交给 GPU 的包含彩色重叠区的页面区域。
//Application.Current.Host.Settings.EnableCacheVisualization = true;
// 通过禁用以下对象阻止在调试过程中关闭屏幕
// 应用程序的空闲检测。
// 注意: 仅在调试模式下使用此设置。禁用用户空闲检测的应用程序在用户不使用电话时将继续运行
// 并且消耗电池电量。
PhoneApplicationService.Current.UserIdleDetectiOnMode= IdleDetectionMode.Disabled;
}
}
// 应用程序启动(例如,从“开始”菜单启动)时执行的代码
// 此代码在重新激活应用程序时不执行
private void Application_Launching(object sender, LaunchingEventArgs e)
{
}
// 激活应用程序(置于前台)时执行的代码
// 此代码在首次启动应用程序时不执行
private void Application_Activated(object sender, ActivatedEventArgs e)
{
// 确保正确恢复应用程序状态
if (!App.ViewModel.IsDataLoaded)
{
App.ViewModel.LoadData();
}
}
// 停用应用程序(发送到后台)时执行的代码
// 此代码在应用程序关闭时不执行
private void Application_Deactivated(object sender, DeactivatedEventArgs e)
{
}
// 应用程序关闭(例如,用户点击“后退”)时执行的代码
// 此代码在停用应用程序时不执行
private void Application_Closing(object sender, ClosingEventArgs e)
{
// 确保所需的应用程序状态在此处保持不变。
}
// 导航失败时执行的代码
private void RootFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)
{
if (Debugger.IsAttached)
{
// 导航已失败;强行进入调试器
Debugger.Break();
}
}
// 出现未处理的异常时执行的代码
private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
{
if (Debugger.IsAttached)
{
// 出现未处理的异常;强行进入调试器
Debugger.Break();
}
}
#region 电话应用程序初始化
// 避免双重初始化
private bool phOneApplicationInitialized= false;
// 请勿向此方法中添加任何其他代码
private void InitializePhoneApplication()
{
if (phoneApplicationInitialized)
return;
// 创建框架但先不将它设置为 RootVisual;这允许初始
// 屏幕保持活动状态,直到准备呈现应用程序时。
RootFrame = new PhoneApplicationFrame();
RootFrame.Navigated += CompleteInitializePhoneApplication;
// 处理导航故障
RootFrame.NavigationFailed += RootFrame_NavigationFailed;
// 在下一次导航中处理清除 BackStack 的重置请求,
RootFrame.Navigated += CheckForResetNavigation;
// 确保我们未再次初始化
phOneApplicationInitialized= true;
}
// 请勿向此方法中添加任何其他代码
private void CompleteInitializePhoneApplication(object sender, NavigationEventArgs e)
{
// 设置根视觉效果以允许应用程序呈现
if (RootVisual != RootFrame)
RootVisual = RootFrame;
// 删除此处理程序,因为不再需要它
RootFrame.Navigated -= CompleteInitializePhoneApplication;
}
private void CheckForResetNavigation(object sender, NavigationEventArgs e)
{
// 如果应用程序收到“重置”导航,则需要进行检查
// 以确定是否应重置页面堆栈
if (e.NavigatiOnMode== NavigationMode.Reset)
RootFrame.Navigated += ClearBackStackAfterReset;
}
private void ClearBackStackAfterReset(object sender, NavigationEventArgs e)
{
// 取消注册事件,以便不再调用该事件
RootFrame.Navigated -= ClearBackStackAfterReset;
// 只为“新建”(向前)和“刷新”导航清除堆栈
if (e.NavigationMode != NavigationMode.New && e.NavigationMode != NavigationMode.Refresh)
return;
// 为了获得 UI 一致性,请清除整个页面堆栈
while (RootFrame.RemoveBackEntry() != null)
{
; // 不执行任何操作
}
}
#endregion
// 初始化应用程序在其本地化资源字符串中定义的字体和排列方向。
//
// 若要确保应用程序的字体与受支持的语言相符,并确保
// 这些语言的 FlowDirection 都采用其传统方向,ResourceLanguage
// 应该初始化每个 resx 文件中的 ResourceFlowDirection,以便将这些值与以下对象匹配
// 文件的区域性。例如:
//
// AppResources.es-ES.resx
// ResourceLanguage 的值应为“es-ES”
// ResourceFlowDirection 的值应为“LeftToRight”
//
// AppResources.ar-SA.resx
// ResourceLanguage 的值应为“ar-SA”
// ResourceFlowDirection 的值应为“RightToLeft”
//
// 有关本地化 Windows Phone 应用程序的详细信息,请参见 http://go.microsoft.com/fwlink/?LinkId=262072。
//
private void InitializeLanguage()
{
try
{
// 将字体设置为与由以下对象定义的显示语言匹配
// 每种受支持的语言的 ResourceLanguage 资源字符串。
//
// 如果显示出现以下情况,则回退到非特定语言的字体
// 手机的语言不受支持。
//
// 如果命中编译器错误,则表示以下对象中缺少 ResourceLanguage
// 资源文件。
RootFrame.Language = XmlLanguage.GetLanguage(AppResources.ResourceLanguage);
// 根据以下条件设置根框架下的所有元素的 FlowDirection
// 每个以下对象的 ResourceFlowDirection 资源字符串上的
// 受支持的语言。
//
// 如果命中编译器错误,则表示以下对象中缺少 ResourceFlowDirection
// 资源文件。
FlowDirection flow = (FlowDirection)Enum.Parse(typeof(FlowDirection), AppResources.ResourceFlowDirection);
RootFrame.FlowDirection = flow;
}
catch
{
// 如果此处导致了异常,则最可能的原因是
// ResourceLangauge 未正确设置为受支持的语言
// 代码或 ResourceFlowDirection 设置为 LeftToRight 以外的值
// 或 RightToLeft。
if (Debugger.IsAttached)
{
Debugger.Break();
}
throw;
}
}
}
}
界面代码MainPage.xaml
phone:PhoneApplicationPage
x:Class="声音库.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DataContext="{d:DesignData SampleData/SampleData.xaml}"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">
"SoundTitleDataTemplate">
"{StaticResource PhoneAccentBrush}"
Margin="0,0,12,12">
"Top"
HorizontalAlignment="Right"
Width="40"
Height="40"
Margin="0,6,6,0">
"{StaticResource PhoneForegroundBrush}"
StrokeThickness="3">
"/Assets/AppBar/play.png">
"Bottom">
"{Binding Title}" Margin="6,0,0,6">
"LayoutRoot" Background="Transparent">
"AudioPlayer"
Volume="1">
"{Binding Path=LocalizedResources.ApplicationTitle,
Source={StaticResource LocalizedStrings}}">
"{Binding Animals.Title}">
"0,0,-12,0"
ItemsSource="{Binding Animals.Items}"
LayoutMode="Grid"
GridCellSize="150,150"
ItemTemplate="{StaticResource SoundTitleDataTemplate}"
SelectionChanged="LongListSelector_SelectionChanged">
"{Binding Cartoons.Title}">
"0,0,-12,0" ItemsSource="{Binding Cartoons.Items}"
LayoutMode="Grid"
GridCellSize="150,150"
ItemTemplate="{StaticResource SoundTitleDataTemplate}"
SelectionChanged="LongListSelector_SelectionChanged">
"{Binding Warnings.Title}">
"0,0,-12,0" ItemsSource="{Binding Warnings.Items}"
LayoutMode="Grid"
GridCellSize="150,150"
ItemTemplate="{StaticResource SoundTitleDataTemplate}"
SelectionChanged="LongListSelector_SelectionChanged">
"{Binding Taunts.Title}">
"0,0,-12,0" ItemsSource="{Binding Taunts.Items}"
LayoutMode="Grid"
GridCellSize="150,150"
ItemTemplate="{StaticResource SoundTitleDataTemplate}"
SelectionChanged="LongListSelector_SelectionChanged">
自定义的三个类:SoundModel、SoundGroup、SoundData
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 声音库.ViewModels
{
public class SoundModel
{
//五个分组
public SoundGroup Animals { get; set; }
public SoundGroup Cartoons { get; set; }
public SoundGroup Warnings { get; set; }
public SoundGroup Taunts { get; set; }
public SoundGroup CustomSounds { get; set; }
//是否加载数据
public bool IsDataLoaded { get; set; }
public void LoadData()//方法:加载数据
{
Animals = CreateAnimalsGroup();
Cartoons = CreateCartoonsGroup();
Taunts = CreateTauntsGroup();
Warnings = CreateWarningsGroup();
IsDataLoaded = true;
}
//加载数据的具体方法
private SoundGroup CreateAnimalsGroup()
{
SoundGroup data = new SoundGroup();//实例化一个组
data.Title = "动物";//组名
string basePath = "assets/audio/animals/";//路径的相同部分
data.Items.Add(new SoundData//组加上项
{
Title = "小猫叫",//数据标题
FilePath = basePath + "Cat Kitten.wav"//完整音频的路径
});
data.Items.Add(new SoundData
{
Title = "老猫叫",
FilePath = basePath + "Cat Meow.wav"
});
data.Items.Add(new SoundData
{
Title = "猩猩叫",
FilePath = basePath + "Chimpanzee.wav"
});
data.Items.Add(new SoundData
{
Title = "牛",
FilePath = basePath + "Cow.wav"
});
data.Items.Add(new SoundData
{
Title = "蛐蛐叫",
FilePath = basePath + "Crickets.wav"
});
data.Items.Add(new SoundData
{
Title = "狗叫",
FilePath = basePath + "Dog.wav"
});
data.Items.Add(new SoundData
{
Title = "海豚叫",
FilePath = basePath + "Dolphin.wav"
});
data.Items.Add(new SoundData
{
Title = "鸭叫",
FilePath = basePath + "Duck.wav"
});
data.Items.Add(new SoundData
{
Title = "马跑声",
FilePath = basePath + "Horse Gallop.wav"
});
data.Items.Add(new SoundData
{
Title = "马走声",
FilePath = basePath + "Horse Walk.wav"
});
data.Items.Add(new SoundData
{
Title = "狮子叫",
FilePath = basePath + "Lion.wav"
});
data.Items.Add(new SoundData
{
Title = "猪叫",
FilePath = basePath + "Pig.wav"
});
data.Items.Add(new SoundData
{
Title = "鸡打鸣",
FilePath = basePath + "Rooster.wav"
});
data.Items.Add(new SoundData
{
Title = "羊叫",
FilePath = basePath + "Sheep.wav"
});
return data;
}
private SoundGroup CreateCartoonsGroup()
{
SoundGroup data = new SoundGroup();
data.Title = "卡通";
string basePath = "assets/audio/cartoons/";
data.Items.Add(new SoundData
{
Title = "啵嘤",
FilePath = basePath + "Boing.wav"
});
data.Items.Add(new SoundData
{
Title = "枪声",
FilePath = basePath + "Bronk.wav"
});
data.Items.Add(new SoundData
{
Title = "军号",
FilePath = basePath + "Bugle charge.wav"
});
data.Items.Add(new SoundData
{
Title = "激光器",
FilePath = basePath + "Laser.wav"
});
data.Items.Add(new SoundData
{
Title = "开溜",
FilePath = basePath + "Out Here.wav"
});
data.Items.Add(new SoundData
{
Title = "啪嗒声",
FilePath = basePath + "Splat.wav"
});
return data;
}
private SoundGroup CreateTauntsGroup()
{
SoundGroup data = new SoundGroup();
data.Title = "愚弄";
string basePath = "assets/audio/taunts/";
data.Items.Add(new SoundData
{
Title = "咯咯笑",
FilePath = basePath + "Cackle.wav"
});
data.Items.Add(new SoundData
{
Title = "滴答声",
FilePath = basePath + "Clock Ticking.wav"
});
data.Items.Add(new SoundData
{
Title = "拨号声",
FilePath = basePath + "Dial up.wav"
});
data.Items.Add(new SoundData
{
Title = "击鼓",
FilePath = basePath + "Drum roll.wav"
});
data.Items.Add(new SoundData
{
Title = "电梯音乐",
FilePath = basePath + "Elevator Music.wav"
});
data.Items.Add(new SoundData
{
Title = "大笑",
FilePath = basePath + "Laugh.wav"
});
data.Items.Add(new SoundData
{
Title = "淫笑",
FilePath = basePath + "Laugh - Evil.wav"
});
data.Items.Add(new SoundData
{
Title = "白花钱",
FilePath = basePath + "Wrong Price.wav"
});
data.Items.Add(new SoundData
{
Title = "伤感长号",
FilePath = basePath + "Sad Trombone.wav"
});
data.Items.Add(new SoundData
{
Title = "挖苦的O",
FilePath = basePath + "Sarcastic Ooo.wav"
});
data.Items.Add(new SoundData
{
Title = "叹气",
FilePath = basePath + "Sigh.wav"
});
data.Items.Add(new SoundData
{
Title = "打呼噜",
FilePath = basePath + "Snore.wav"
});
data.Items.Add(new SoundData
{
Title = "打哈欠",
FilePath = basePath + "Yawn.wav"
});
return data;
}
private SoundGroup CreateWarningsGroup()
{
SoundGroup data = new SoundGroup();
data.Title = "警告";
string basePath = "assets/audio/warnings/";
data.Items.Add(new SoundData
{
Title = "汽笛",
FilePath = basePath + "Air horn.wav"
});
data.Items.Add(new SoundData
{
Title = "空袭",
FilePath = basePath + "Air Raid.wav"
});
data.Items.Add(new SoundData
{
Title = "闹钟 - 电报",
FilePath = basePath + "Alarm Clock - Electric.wav"
});
data.Items.Add(new SoundData
{
Title = "闹钟 - 电话",
FilePath = basePath + "Alarm Clock - Bell.wav"
});
data.Items.Add(new SoundData
{
Title = "抢救",
FilePath = basePath + "Backing up.wav"
});
data.Items.Add(new SoundData
{
Title = "钟声",
FilePath = basePath + "Bell - Church.wav"
});
data.Items.Add(new SoundData
{
Title = "上课铃声",
FilePath = basePath + "Bell - School.wav"
});
data.Items.Add(new SoundData
{
Title = "电雾号",
FilePath = basePath + "Fog horn.wav"
});
data.Items.Add(new SoundData
{
Title = "打碎玻璃",
FilePath = basePath + "Glass breaking.wav"
});
data.Items.Add(new SoundData
{
Title = "错误警报",
FilePath = basePath + "Missle alert.wav"
});
data.Items.Add(new SoundData
{
Title = "急救",
FilePath = basePath + "Police - UK.wav"
});
data.Items.Add(new SoundData
{
Title = "警察",
FilePath = basePath + "Police - US.wav"
});
data.Items.Add(new SoundData
{
Title = "呜呜祖拉",
FilePath = basePath + "Vuvuzela.wav"
});
return data;
}
}
}
View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 声音库.ViewModels
{
public class SoundGroup
{
public SoundGroup()//构造函数,实例化存放数据项的属性
{
Items = new List();
}
public List Items { get; set; }//属性:用于存放数据项
public string Title { get; set; }//组的标题
}
}
View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 声音库.ViewModels
{
public class SoundData
{
public string Title { get; set; }//数据项的标题
public string FilePath { get; set; }//数据项的路径
}
}
View Code
窗体设计实例数据SampleData.xaml
vm:SoundModel
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:声音库.ViewModels">
"Animals Sample">
"Animals 1" FilePath="Animals.wav" />
"Cartoons Sample">
"Cartoons 1" FilePath="Cartoons.wav" />
"Cartoons 2" FilePath="Cartoons.wav" />
"Taunts Sample">
"Taunts 1" FilePath="Taunts.wav" />
"Taunts 2" FilePath="Taunts.wav" />
"Taunts 3" FilePath="Taunts.wav" />
"Warnings Sample">
"Warnings 1" FilePath="Warnings.wav" />
"Warnings 2" FilePath="Warnings.wav" />
"Warnings 3" FilePath="Warnings.wav" />
"Warnings 4" FilePath="Warnings.wav" />
"Custom">
"Custom 1" FilePath="Custom.wav" />
"Custom 2" FilePath="Custom.wav" />
"Custom 3" FilePath="Custom.wav" />
"Custom 4" FilePath="Custom.wav" />
四:总结
总的来说该程序还不是太完善,本来想这把程序也上传,但大笑已经超过上传的10M上限,这里已将全部自写代码贴出 WP8开发(6)--------------小程序---声音库播放 |
CopyRight 2018-2019 实验室设备网 版权所有 |