【问题标题】:SoundPlayer on Visual Studio 2017 Mac outputs no soundVisual Studio 2017 Mac 上的 SoundPlayer 不输出声音
【发布时间】:2017-11-18 20:18:47
【问题描述】:

我已经实例化了一个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 文件。

希望你能帮上忙!

【问题讨论】:

    标签: c# audio visual-studio-2017 visual-studio-mac soundplayer


    【解决方案1】:

    Mono 用对 ALSA(高级 Linux 声音架构)特定 libasound.so 库的调用替换特定于 Windows 的内容。

    回复:http://www.alsa-project.org/main/index.php/Main_Page

    在 Mac 上从控制台应用程序播放音频的简单方法是壳一个进程并运行 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 的应用程序,并且您将拥有对音频系统的完全访问权限,因此您可以使用NSSoundCoreAudio 框架。

    【讨论】:

      猜你喜欢
      • 2023-04-04
      • 2019-08-02
      • 1970-01-01
      • 1970-01-01
      • 2018-01-20
      • 1970-01-01
      • 2014-04-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多