媒体元素在windows商店应用程序中不播放音频

本文关键字:播放 音频 应用程序 元素 windows 媒体 | 更新日期: 2023-09-27 17:51:18

我想在我的应用程序中播放mp3音频,但没有任何事情发生我的代码是

XAML:

<Page
    x:Class="SunnahForKids.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:SunnahForKids"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
    <Grid Background="Black">
        <StackPanel Width="900" Height="700" HorizontalAlignment="Center">
            <MediaElement HorizontalAlignment="Left" Name="a" AutoPlay="False" Source="azan.mp3" Height="100" Margin="451,299,0,0" VerticalAlignment="Top" Width="100" Volume="100"/>
            <Button Content="Button" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0,0,676,0" Height="189" Click="Button_Click"/>
        </StackPanel>
    </Grid>
</Page>

这里是。cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Data.Json;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.Media.SpeechSynthesis;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
namespace SunnahForKids
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
       }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            a.Play();
        }

    }
}

和我挣扎了2天需要帮助。我也跟着这个链接MediaElement在WinRT/Win8根本不起作用更新我的驱动程序,但它是最新的。显示驱动在Intel R(Q35) express芯片组家族(Microsoft Corporation WDDM-1.0)请让我离开这里

媒体元素在windows商店应用程序中不播放音频

我试过你的代码和代码工作,所以一些奇怪的事情正在发生。我会尝试收听链接中提到的事件(示例进一步向下为开发人员寻找代码示例)。

检查你的事件日志(Windows =>应用程序),如果你错过了Windows的更新,并检查mp3是否会播放。

监听MediaFailed事件(建议您在文档中这样做),看看是否可以在那里获取一些信息。代码来自msdn:

private void videoMediaElement_MediaFailed(object sender, ExceptionRoutedEventArgs e)
{
    // Check the event arguments => e
    // get HRESULT from event args 
    string hr = GetHresultFromErrorMessage(e);
    // Handle media failed event appropriately 
}
private string GetHresultFromErrorMessage(ExceptionRoutedEventArgs e)
{
    String hr = String.Empty;
    String token = "HRESULT - ";
    const int hrLength = 10;     // eg "0xFFFFFFFF"
    int tokenPos = e.ErrorMessage.IndexOf(token, StringComparison.Ordinal);
    if (tokenPos != -1)
    {
        hr = e.ErrorMessage.Substring(tokenPos + token.Length, hrLength);
    }
    return hr;
}