简体   繁体   English

按下按钮以C#播放声音

[英]Play sound in C# on button press

I've tried to play a sound from a button event handler: 我试图从按钮事件处理程序中播放声音:

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();
        }
    }
}

But I'm getting an error where the type or namespace "media" does not exist in the namespace "system". 但是我收到一个错误,即名称空间“ system”中不存在类型或名称空间“ media”。 What seems to be the problem? 似乎是什么问题?

I'm using visual studio 2017 and I've created a universal windows c# blank app. 我正在使用Visual Studio 2017,并且创建了通用Windows C#空白应用程序。

Edit: 编辑:

I've changed my code to this but I'm still not getting any audio playback. 我已将代码更改为此,但仍无法播放任何音频。

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();
        }
    }
}

I've tried adding a background declaration tho to allow background audio but still to no avail. 我尝试添加背景声明tho允许背景音频,但仍然无济于事。

You are using .Net Core and System.Media is in .Net Framework. 您正在使用.Net Core和System.Media在.Net Framework中。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM