Visual Studio 2017 Mac 上的 SoundPlayer 没有输出声音
SoundPlayer on Visual Studio 2017 Mac outputs no sound
我已经实例化了一个 System.Media.SoundPlayer
并且我正在(尝试)使用它来播放 .wav 文件。但是,正如您可能已经从标题中了解到的那样,它不起作用(没有声音输出)。我正在使用 C# 并在 Visual Studio 2017 Mac 中创建控制台应用程序。这是我的代码:
using System;
using System.Threading;
using System.IO; // Not needed yet
using System.Media;
namespace mathsTestConsoleC
{
public class OutputClass
{
public void PlayMusic()
{
SoundPlayer player = new SoundPlayer
{
SoundLocation = @"/Users/felix/Projects/mathsTestConsoleC/mathsTestConsoleC/backgroundMusic.wav"
};
player.Load();
player.Play();
}
}
}
(我知道您不一定需要使用 player.Load();
,因为 player.Play();
包含它。)
这也不行:
public void PlayMusic()
{
SoundPlayer player = new SoundPlayer
{
SoundLocation = "/Users/felix/Projects/mathsTestConsoleC/mathsTestConsoleC/backgroundMusic.wav"
};
player.Load();
Thread.Sleep(500);
if (player.IsLoadCompleted == true)
{
player.Play();
}
else if (player.IsLoadCompleted == false)
{
Console.WriteLine("player.IsLoadCompleted == false");
}
else
{
Console.WriteLine("player.IsLoadCompleted == not set");
}
}
当我调用 PlayMusic()
时,我的应用程序不播放我提供给它的 .wav 文件。
希望对您有所帮助!
Mono
将 Windows-specific 替换为对 ALSA(高级 Linux 声音架构)特定 libasound.so
库的调用。
回复:http://www.alsa-project.org/main/index.php/Main_Page
从 Mac 上的控制台应用程序播放音频的简单方法是 shell 一个进程和 运行 afplay
:
afplay
Audio File Play
Version: 2.0
Copyright 2003-2013, Apple Inc. All Rights Reserved.
Specify -h (-help) for command options
Usage:
afplay [option...] audio_file
否则,您可以创建一个基于 Xamarin.Mac
的应用程序,您将拥有对音频系统的完全访问权限,因此您可以使用 NSSound
或 CoreAudio
框架。
我已经实例化了一个 System.Media.SoundPlayer
并且我正在(尝试)使用它来播放 .wav 文件。但是,正如您可能已经从标题中了解到的那样,它不起作用(没有声音输出)。我正在使用 C# 并在 Visual Studio 2017 Mac 中创建控制台应用程序。这是我的代码:
using System;
using System.Threading;
using System.IO; // Not needed yet
using System.Media;
namespace mathsTestConsoleC
{
public class OutputClass
{
public void PlayMusic()
{
SoundPlayer player = new SoundPlayer
{
SoundLocation = @"/Users/felix/Projects/mathsTestConsoleC/mathsTestConsoleC/backgroundMusic.wav"
};
player.Load();
player.Play();
}
}
}
(我知道您不一定需要使用 player.Load();
,因为 player.Play();
包含它。)
这也不行:
public void PlayMusic()
{
SoundPlayer player = new SoundPlayer
{
SoundLocation = "/Users/felix/Projects/mathsTestConsoleC/mathsTestConsoleC/backgroundMusic.wav"
};
player.Load();
Thread.Sleep(500);
if (player.IsLoadCompleted == true)
{
player.Play();
}
else if (player.IsLoadCompleted == false)
{
Console.WriteLine("player.IsLoadCompleted == false");
}
else
{
Console.WriteLine("player.IsLoadCompleted == not set");
}
}
当我调用 PlayMusic()
时,我的应用程序不播放我提供给它的 .wav 文件。
希望对您有所帮助!
Mono
将 Windows-specific 替换为对 ALSA(高级 Linux 声音架构)特定 libasound.so
库的调用。
回复:http://www.alsa-project.org/main/index.php/Main_Page
从 Mac 上的控制台应用程序播放音频的简单方法是 shell 一个进程和 运行 afplay
:
afplay
Audio File Play
Version: 2.0
Copyright 2003-2013, Apple Inc. All Rights Reserved.
Specify -h (-help) for command options
Usage:
afplay [option...] audio_file
否则,您可以创建一个基于 Xamarin.Mac
的应用程序,您将拥有对音频系统的完全访问权限,因此您可以使用 NSSound
或 CoreAudio
框架。