Skip to content

Conversation

DotnetInstall
Copy link
Contributor

PCL.Neo 跨平台音频系统 PR

概述

本PR为PCL.Neo启动器添加了完整的跨平台音频播放系统,可在Windows、macOS和Linux系统上播放音频文件,无需额外的第三方依赖。系统设计灵活,提供了统一的接口和平台特定的优化实现。

主要功能

  • 跨平台兼容:同一套API在不同操作系统上工作
  • 支持多种音频格式:MP3、WAV等常见格式
  • 完整的音频控制:播放、暂停、继续、停止功能
  • 多源播放:支持从文件路径或内存流播放音频
  • 音量控制:支持设置音量和渐变效果
  • 事件通知:提供播放完成事件和安全的事件处理机制
  • 平台特定优化:为各平台提供专门实现
  • 可扩展设计:提供受保护方法便于派生和自定义

详细修改内容

核心架构

  • IAudioService接口定义了音频播放的核心功能
  • AudioService基类实现了通用的跨平台播放功能
  • WindowsAudioService提供Windows平台特定的优化实现
  • AudioServiceFactory工厂类根据当前平台自动创建合适的音频服务
  • AudioPlayerExtensions提供便捷的扩展方法
  • OnPlaybackFinished受保护方法提供安全的事件触发机制

分平台实现

  1. Windows

    • 使用Windows MCI命令实现音频控制
    • 提供WaveOut接口的音量控制
    • 支持多种音频格式和状态监控
    • 通过定期检查MCI状态实现精确的播放完成通知
  2. macOS 和 Linux

    • 使用本地命令行工具(afplay/aplay/mpg123)
    • 使用UNIX信号控制播放状态(SIGSTOP/SIGCONT)
    • 自动选择合适的播放器处理不同音频格式
    • 通过进程退出事件实现播放完成通知

额外功能

  • 音频流处理:支持从内存流播放
  • 临时文件管理:自动管理流播放的临时文件
  • 音量渐变:提供淡入淡出效果
  • 超时控制:支持播放超时设置
  • 事件处理安全:使用受保护方法确保派生类安全触发事件

添加的文件

  1. 接口和基础类:

    • PCL.Neo.Core/Service/Audio/IAudioService.cs - 音频服务接口
    • PCL.Neo.Core/Service/Audio/AudioOptions.cs - 音频配置选项
    • PCL.Neo.Core/Service/Audio/AudioService.cs - 基础实现类
  2. 平台特定实现:

    • PCL.Neo.Core/Service/Audio/WindowsAudioService.cs - Windows平台实现
  3. 工具和扩展:

    • PCL.Neo.Core/Service/Audio/AudioServiceFactory.cs - 音频服务工厂
    • PCL.Neo.Core/Service/Audio/AudioPlayerExtensions.cs - 扩展方法
  4. 示例和文档:

    • PCL.Neo.Core/Service/Audio/AudioDemo.cs - 示例代码
    • PCL.Neo.Core/Service/Audio/README.md - 使用文档

使用示例

// 创建音频服务
IAudioService audioService = AudioServiceFactory.CreateForCurrentPlatform();

// 播放音频文件
await audioService.PlayAsync("path/to/audio.mp3");

// 暂停和继续
await audioService.PauseAsync();
await audioService.ResumeAsync();

// 使用扩展方法
await audioService.PlaySoundAndWaitAsync("path/to/effect.wav", 5000); // 5秒超时
await audioService.FadeVolumeAsync(0.0f, 2000); // 2秒内淡出

// 监听播放完成事件
audioService.PlaybackFinished += (sender, args) => {
    Console.WriteLine("音频播放完成");
};

扩展和自定义

// 继承AudioService创建自定义音频服务
public class MyCustomAudioService : AudioService
{
    // 重写受保护方法自定义播放行为
    protected override async Task<bool> StartPlaybackAsync(string filePath, CancellationToken cancellationToken)
    {
        // 自定义实现
        return await base.StartPlaybackAsync(filePath, cancellationToken);
    }
    
    // 重写OnPlaybackFinished添加额外逻辑
    protected override void OnPlaybackFinished(object sender)
    {
        // 在触发事件前执行自定义逻辑
        base.OnPlaybackFinished(sender);
        // 触发事件后执行额外操作
    }
}

注意事项

  • Windows平台使用MCI命令实现,需要winmm.dll
  • Linux平台需要安装aplay和mpg123命令行工具
  • macOS平台默认已包含afplay工具
  • 音量控制在不同平台上实现效果可能略有差异
  • 派生类应使用OnPlaybackFinished方法而非直接调用PlaybackFinished事件

@DotnetInstall DotnetInstall self-assigned this May 31, 2025
@DotnetInstall DotnetInstall added the 新功能 包括了新功能的更改 label May 31, 2025
@DotnetInstall DotnetInstall added this to the 0.1a milestone May 31, 2025
@DotnetInstall DotnetInstall linked an issue May 31, 2025 that may be closed by this pull request
@DotnetInstall DotnetInstall removed the request for review from copytiao May 31, 2025 14:07
@DotnetInstall
Copy link
Contributor Author

@shimoranla

Copy link
Member

@whitecat346 whitecat346 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice work

@lhx077
Copy link
Collaborator

lhx077 commented May 31, 2025

头疼
还有些功能写不了
比如音量获取和Windows暂停

@lhx077
Copy link
Collaborator

lhx077 commented May 31, 2025

头疼 还有些功能写不了 比如音量获取和Windows暂停

让我再修改一下

@whitecat346
Copy link
Member

比如音量获取和Windows暂停

彳亍,你可以先Draft,做好了再Ready

@lhx077
Copy link
Collaborator

lhx077 commented May 31, 2025

比如音量获取和Windows暂停

彳亍,你可以先Draft,做好了再Ready

其实是已经在WindowsAudioService里做好了,但是不知道为什么两块是散的,没有整合到一起

@lhx077 lhx077 merged commit 8d03625 into PCL-Community:main May 31, 2025
3 checks passed
whitecat346 pushed a commit to whitecat346/PCL.Neo that referenced this pull request Jun 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
新功能 包括了新功能的更改
Projects
None yet
Development

Successfully merging this pull request may close these issues.

跨平台音频播放模块
3 participants