Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
按C#on按钮播放声音_C#_Windows - Fatal编程技术网

按C#on按钮播放声音

按C#on按钮播放声音,c#,windows,C#,Windows,我尝试播放按钮事件处理程序中的声音: using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.InteropServices.WindowsRuntime; using Windows.Foundation; using Windows.Foundation.Collections; using Windows.UI.Xaml; using W

我尝试播放按钮事件处理程序中的声音:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
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;

// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

namespace App1
{
    /// <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 mainButton_Click_1(object sender, RoutedEventArgs e)
        {
            System.Media.SoundPlayer player = new System.Media.SoundPlayer(@"c:\mywavfile.wav");
            player.Play();
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
使用System.Runtime.InteropServices.WindowsRuntime;
使用Windows基金会;
使用Windows。
使用Windows.UI.Xaml;
使用Windows.UI.Xaml.Controls;
使用Windows.UI.Xaml.Controls.Primitives;
使用Windows.UI.Xaml.Data;
使用Windows.UI.Xaml.Input;
使用Windows.UI.Xaml.Media;
使用Windows.UI.Xaml.Navigation;
//空白页项模板被记录在https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
名称空间App1
{
/// 
///可以单独使用或在框架内导航到的空页。
/// 
公共密封部分类主页面:第页
{
公共主页()
{
this.InitializeComponent();
}
私有void主按钮\u单击\u 1(对象发送者,路由目标)
{
System.Media.SoundPlayer player=新的System.Media.SoundPlayer(@“c:\mywavfile.wav”);
player.Play();
}
}
}
但是我遇到了一个错误,即名称空间“system”中不存在类型或名称空间“media”。有什么问题吗

我使用的是visual studio 2017,我创建了一个通用的windows c#blank应用程序

编辑:

我已将代码更改为此,但仍无法播放任何音频

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
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;

// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

namespace App1
{
    /// <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 mainButton_Click_1(object sender, RoutedEventArgs e)
        {
            MediaElement PlayMusic = new MediaElement();
            PlayMusic.AudioCategory = Windows.UI.Xaml.Media.AudioCategory.Media;
            PlayMusic.Source = new Uri(@"D:/Downloads/sounds.mp3");
            PlayMusic.Play();
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
使用System.Runtime.InteropServices.WindowsRuntime;
使用Windows基金会;
使用Windows。
使用Windows.UI.Xaml;
使用Windows.UI.Xaml.Controls;
使用Windows.UI.Xaml.Controls.Primitives;
使用Windows.UI.Xaml.Data;
使用Windows.UI.Xaml.Input;
使用Windows.UI.Xaml.Media;
使用Windows.UI.Xaml.Navigation;
//空白页项模板被记录在https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
名称空间App1
{
/// 
///可以单独使用或在框架内导航到的空页。
/// 
公共密封部分类主页面:第页
{
公共主页()
{
this.InitializeComponent();
}
私有void主按钮\u单击\u 1(对象发送者,路由目标)
{
MediaElement播放音乐=新MediaElement();
PlayMusic.AudioCategory=Windows.UI.Xaml.Media.AudioCategory.Media;
PlayMusic.Source=newURI(@“D:/Downloads/sounds.mp3”);
播放音乐;
}
}
}

我尝试添加一个背景声明来允许背景音频,但仍然没有效果

您正在使用.Net Core和System.Media在.Net Framework中


在.Net Core中,您可以使用Windows.UI.Xaml.Controls.MediaElement。或者,您可以使用Windows.Media.Audio.AudioGraph。

哇,您是怎么知道的?你真的做过研究吗?还是很明显?我正忙着写一个UWP视频标签应用程序,所以我自己也经历了这一切。我已经更新了答案,仍然无法播放音频。我尝试了MediaElement方法,假设您的XAML中有一个网格