1
我試圖從一個按鈕的事件處理程序播放聲音:播放聲音在C#中按下按鈕
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();
}
}
}
但我發現了其中的類型或命名空間「媒體」不存在的錯誤命名空間「系統」。什麼似乎是問題?
我正在使用visual studio 2017,並創建了一個通用的windows c#空白應用程序。
編輯:
我已經改變了我的代碼這一點,但我仍然沒有得到任何音頻播放。
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();
}
}
}
我已經嘗試添加一個背景聲明壽允許背景音頻,但仍然無濟於事。
哇你怎麼知道這件事?你真的在做研究嗎,還是對你很明顯? –
我正在忙於爲視頻標籤寫一個UWP應用程序,所以我自己就完成了所有這些。 –
我已經更新了答案,仍然無法播放音頻。我嘗試了MediaElement方法 – Wilson