You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Visual Studio C# 调用WindowsMediaPlayer播放MP3无声音输出问题排查

问题原因及修复方案

1 代码层面已知错误

  • 路径格式错误:C#中普通字符串的反斜杠需要转义,或使用@前缀声明逐字字符串,且你当前路径缺少.mp3后缀,播放器无法识别文件类型。
    错误写法:myplayer.URL = "C:\Users\truale\Desktop\RickRoll";
    正确写法:myplayer.URL = @"C:\Users\truale\Desktop\RickRoll.mp3";
  • 播放对象被GC提前回收:myplayer是case"2"块内的局部变量,调用play()后代码直接执行continue回到循环,局部变量会被垃圾回收机制销毁,播放进程直接中断。
  • 无音量配置:未主动设置播放器实例音量,可能存在默认静音/音量为0的情况。
  • 类成员定义错误:类下直接写string keyChoice = Console.ReadLine();属于非法语法,控制台程序启动后只会执行Main方法,这行代码不会运行还会引发编译报错。

2 修复后的核心代码示例

using System;
using System.Threading;
using WMPLib; // 需先在项目引用中添加Windows Media Player COM组件

class Program
{
    // 将播放器实例声明为静态全局变量,避免被GC回收
    private static WindowsMediaPlayer myplayer = new WindowsMediaPlayer();
    
    static void Main(string[] args)
    {
        // 初始化播放器配置
        myplayer.settings.volume = 100;
        myplayer.settings.autoStart = false;

        Console.WriteLine("Hello,[user]!");
        Console.WriteLine("Please Enter Your Username");
        Console.WriteLine();
        
        string uname = Console.ReadLine();

        Thread.Sleep(600);
        Console.Clear();
        Thread.Sleep(600);

        Console.WriteLine("Welcome to Axiom, " + uname);
        Console.WriteLine("If You Want To Find The Full List Of Commands, Please Press 1");
        Console.WriteLine();

        while (true)
        {
            string keyChoice = Console.ReadLine();
           
            switch (keyChoice)
            {
                case "1":
                    Thread.Sleep(600);
                    Console.Clear();
                    Thread.Sleep(600);
                    Console.WriteLine("Console Commands:");
                    Console.WriteLine("Press '2' To Get 1000$ FREE");
                    Console.WriteLine("Press '3' To Pick Command 3");
                    Console.WriteLine();
                    Console.WriteLine("Press '0' To Exit Or Press A Command Button To Execute A Command");
                    Console.WriteLine();
                    continue; 

                case "2":
                    Thread.Sleep(600);
                    Console.Clear();
                    Thread.Sleep(600);
                    Console.WriteLine("Get Messed Up");
                    // 替换为你的实际文件路径
                    myplayer.URL = @"C:\Users\truale\Desktop\RickRoll.mp3";
                    myplayer.controls.play();
                    continue; 

                case "3":
                    Thread.Sleep(600);
                    Console.Clear();
                    Thread.Sleep(600);
                    Console.WriteLine("You Chose Command 3");
                    Console.WriteLine();
                    Console.WriteLine("Press '0' To Exit Or Press A Command Button To Execute A Command");
                    Console.WriteLine();
                    continue;

                case "0":
                    // 退出前释放播放器资源
                    myplayer.close();
                    Thread.Sleep(600);
                    Console.Clear();
                    Thread.Sleep(600);
                    return;

                default:
                    Thread.Sleep(600);
                    Console.Clear();
                    Thread.Sleep(600);
                    Console.WriteLine("Input Not Valid");
                    Console.WriteLine();
                    Console.WriteLine("Press '0' To Exit Or Press A Command Button To Execute A Command");
                    Console.WriteLine();
                    continue; 
            }
        }
    }
}

3 其他排查项

  • 项目引用配置:右键项目→添加→引用→COM,找到Windows Media Player勾选添加,才能正常调用WMPLib库。
  • 平台目标配置:右键项目→属性→生成,将平台目标设置为和你系统匹配的架构(如64位系统设为x64或Any CPU,取消勾选“首选32位”),避免COM组件调用不兼容。
  • 本地文件校验:先手动双击打开你路径下的MP3文件,确认文件本身可以正常播放,路径没有拼写错误。

内容的提问来源于stack exchange,提问作者Blue_A55T

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 12:24:03