小程序---聲音庫播放


一:運行預覽

二:解決方案截圖

三:主要代碼

程序入口: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; } } /// <summary>
        /// 提供對電話應用程序的根框架的輕松訪問。
        /// </summary>
        /// <returns>電話應用程序的根框架。</returns>
        public static PhoneApplicationFrame RootFrame { get; private set; }

        /// <summary>
        /// Application 對象的構造函數。
        /// </summary>
        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">
    
    <!--data每一項的設置-->
    <phone:PhoneApplicationPage.Resources>
        <DataTemplate x:Key="SoundTitleDataTemplate">
            <Grid Background="{StaticResource PhoneAccentBrush}"
                  Margin="0,0,12,12">
                <Grid VerticalAlignment="Top"
                                      HorizontalAlignment="Right"
                                      Width="40"
                                      Height="40"
                                      Margin="0,6,6,0">
                    <Ellipse Stroke="{StaticResource PhoneForegroundBrush}"
                                             StrokeThickness="3"></Ellipse>
                    <Image Source="/Assets/AppBar/play.png"></Image>
                </Grid>
                <StackPanel VerticalAlignment="Bottom">
                    <TextBlock Text="{Binding Title}" Margin="6,0,0,6"></TextBlock>
                </StackPanel>
            </Grid>
        </DataTemplate>
    </phone:PhoneApplicationPage.Resources>
    
    <!--LayoutRoot 是包含所有頁面內容的根網格-->
    <Grid x:Name="LayoutRoot" Background="Transparent">

        <MediaElement Name="AudioPlayer"
                      Volume="1"></MediaElement>

        <!--model的設置-->
        <phone:Pivot Title="{Binding Path=LocalizedResources.ApplicationTitle,
            Source={StaticResource LocalizedStrings}}">
            <!--group的頁的設置-->
            <phone:PivotItem Header="{Binding Animals.Title}">
                <phone:LongListSelector Margin="0,0,-12,0"
                                        ItemsSource="{Binding Animals.Items}"
                                        LayoutMode="Grid"
                                        GridCellSize="150,150"
                                        ItemTemplate="{StaticResource SoundTitleDataTemplate}"
                                        SelectionChanged="LongListSelector_SelectionChanged">
                </phone:LongListSelector>
            </phone:PivotItem>

            <!--group的頁的設置-->
            <phone:PivotItem Header="{Binding Cartoons.Title}">
                <phone:LongListSelector Margin="0,0,-12,0" ItemsSource="{Binding Cartoons.Items}"
                                         LayoutMode="Grid"
                                        GridCellSize="150,150"
                                        ItemTemplate="{StaticResource SoundTitleDataTemplate}"
                                        SelectionChanged="LongListSelector_SelectionChanged">                     
                </phone:LongListSelector>
            </phone:PivotItem>


            <!--group的頁的設置-->
            <phone:PivotItem Header="{Binding Warnings.Title}">
                <phone:LongListSelector Margin="0,0,-12,0" ItemsSource="{Binding Warnings.Items}"
                                         LayoutMode="Grid"
                                        GridCellSize="150,150"
                                        ItemTemplate="{StaticResource SoundTitleDataTemplate}"
                                        SelectionChanged="LongListSelector_SelectionChanged">
            </phone:LongListSelector>
        </phone:PivotItem>
        </phone:Pivot>

        <!--group的頁的設置-->
        <phone:PivotItem Header="{Binding Taunts.Title}">
            <phone:LongListSelector Margin="0,0,-12,0" ItemsSource="{Binding Taunts.Items}"
                                     LayoutMode="Grid"
                                      GridCellSize="150,150"
                                    ItemTemplate="{StaticResource SoundTitleDataTemplate}"
                                    SelectionChanged="LongListSelector_SelectionChanged">
            </phone:LongListSelector>
        </phone:PivotItem>

     
    </Grid>

</phone:PhoneApplicationPage>
自定義的三個類: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<SoundData>();
        }
        public List<SoundData> 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"><!--命名空間-->
    
<!--設計窗體時的數據-->
    <vm:SoundModel.Animals>
        <vm:SoundGroup Title="Animals Sample">
            <vm:SoundGroup.Items>
                <vm:SoundData Title="Animals 1" FilePath="Animals.wav" />
            </vm:SoundGroup.Items>
        </vm:SoundGroup>
    </vm:SoundModel.Animals>

    <vm:SoundModel.Cartoons>
        <vm:SoundGroup Title="Cartoons Sample">
            <vm:SoundGroup.Items>
                <vm:SoundData Title="Cartoons 1" FilePath="Cartoons.wav" />
                <vm:SoundData Title="Cartoons 2" FilePath="Cartoons.wav" />
            </vm:SoundGroup.Items>
        </vm:SoundGroup>
    </vm:SoundModel.Cartoons>

    <vm:SoundModel.Taunts>
        <vm:SoundGroup Title="Taunts Sample">
            <vm:SoundGroup.Items>
                <vm:SoundData Title="Taunts 1" FilePath="Taunts.wav" />
                <vm:SoundData Title="Taunts 2" FilePath="Taunts.wav" />
                <vm:SoundData Title="Taunts 3" FilePath="Taunts.wav" />
            </vm:SoundGroup.Items>
        </vm:SoundGroup>
    </vm:SoundModel.Taunts>

    <vm:SoundModel.Warnings>
        <vm:SoundGroup Title="Warnings Sample">
            <vm:SoundGroup.Items>
                <vm:SoundData Title="Warnings 1" FilePath="Warnings.wav" />
                <vm:SoundData Title="Warnings 2" FilePath="Warnings.wav" />
                <vm:SoundData Title="Warnings 3" FilePath="Warnings.wav" />
                <vm:SoundData Title="Warnings 4" FilePath="Warnings.wav" />
            </vm:SoundGroup.Items>
        </vm:SoundGroup>
    </vm:SoundModel.Warnings>

    <vm:SoundModel.CustomSounds>
        <vm:SoundGroup Title="Custom">
            <vm:SoundGroup.Items>
                <vm:SoundData Title="Custom 1" FilePath="Custom.wav" />
                <vm:SoundData Title="Custom 2" FilePath="Custom.wav" />
                <vm:SoundData Title="Custom 3" FilePath="Custom.wav" />
                <vm:SoundData Title="Custom 4" FilePath="Custom.wav" />
            </vm:SoundGroup.Items>
        </vm:SoundGroup>
    </vm:SoundModel.CustomSounds>

</vm:SoundModel>

四:總結

總的來說該程序還不是太完善,本來想這把程序也上傳,但大笑已經超過上傳的10M上限,這里已將全部自寫代碼貼出

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM