content: import 'package:flutter/material.dart';
  import 'package:audioplayers/audioplayers.dart';
  
  /// One音频播放器组件
  class OneAudioPlayer extends StatefulWidget {
    final String audioUrl;
    final String title;
    final String author;
  
    const OneAudioPlayer({
      super.key,
      required this.audioUrl,
      required this.title,
      required this.author,
    });
  
    @override
    State<OneAudioPlayer> createState() => _OneAudioPlayerState();
  }
  
  class _OneAudioPlayerState extends State<OneAudioPlayer> {
    late AudioPlayer _audioPlayer;
    bool _isPlaying = false;
    bool _isLoading = false;
    Duration _duration = Duration.zero;
    Duration _position = Duration.zero;
  
    @override
    void initState() {
      super.initState();
      _audioPlayer = AudioPlayer();
      _setupAudioPlayer();
    }
  
    @override
    void dispose() {
      _audioPlayer.dispose();
      super.dispose();
    }
  
    void _setupAudioPlayer() {
      _audioPlayer.onPlayerStateChanged.listen((state) {
        if (mounted) {
          setState(() {
            _isPlaying = state == PlayerState.playing;
            _isLoading =
                state == PlayerState.playing && _position == Duration.zero;
          });
        }
      });
  
      _audioPlayer.onDurationChanged.listen((duration) {
        if (mounted) {
          setState(() {
            _duration = duration;
          });
        }
      });
  
      _audioPlayer.onPositionChanged.listen((position) {
        if (mounted) {
          setState(() {
            _position = position;
            _isLoading = false;
          });
        }
      });
    }
  
    Future<void> _togglePlayPause() async {
      try {
        if (_isPlaying) {
          await _audioPlayer.pause();
        } else {
          setState(() {
            _isLoading = true;
          });
          await _audioPlayer.play(UrlSource(widget.audioUrl));
        }
      } catch (e) {
        if (mounted) {
          setState(() {
            _isLoading = false;
          });
          ScaffoldMessenger.of(
            context,
          ).showSnackBar(SnackBar(content: Text('播放失败: $e')));
        }
      }
    }
  
    Future<void> _seek(Duration position) async {
      await _audioPlayer.seek(position);
    }
  
    String _formatDuration(Duration duration) {
      String twoDigits(int n) => n.toString().padLeft(2, '0');
      final minutes = twoDigits(duration.inMinutes.remainder(60));
      final seconds = twoDigits(duration.inSeconds.remainder(60));
      return '$minutes:$seconds';
    }
  
    @override
    Widget build(BuildContext context) {
      return Container(
        padding: const EdgeInsets.all(16),
        decoration: BoxDecoration(
          color: Colors.grey[100],
          borderRadius: BorderRadius.circular(12),
          boxShadow: [
            BoxShadow(
              color: Colors.black.withValues(alpha: 0.05),
              blurRadius: 8,
              offset: const Offset(0, 2),
            ),
          ],
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            // 标题和作者
            Row(
              children: [
                Icon(
                  Icons.headphones,
                  color: Theme.of(context).primaryColor,
                  size: 20,
                ),
                const SizedBox(width: 8),
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        '有声阅读',
                        style: TextStyle(
                          fontSize: 14,
                          fontWeight: FontWeight.bold,
                          color: Theme.of(context).primaryColor,
                        ),
                      ),
                      if (widget.author.isNotEmpty)
                        Text(
                          '朗读者：${widget.author}',
                          style: TextStyle(fontSize: 12, color: Colors.grey[600]),
                        ),
                    ],
                  ),
                ),
              ],
            ),
            const SizedBox(height: 12),
            // 播放控制
            Row(
              children: [
                // 播放/暂停按钮
                GestureDetector(
                  onTap: _togglePlayPause,
                  child: Container(
                    width: 48,
                    height: 48,
                    decoration: BoxDecoration(
                      color: Theme.of(context).primaryColor,
                      shape: BoxShape.circle,
                    ),
                    child: _isLoading
                        ? const SizedBox(
                            width: 24,
                            height: 24,
                            child: CircularProgressIndicator(
                              color: Colors.white,
                              strokeWidth: 2,
                            ),
                          )
                        : Icon(
                            _isPlaying ? Icons.pause : Icons.play_arrow,
                            color: Colors.white,
                            size: 24,
                          ),
                  ),
                ),
                const SizedBox(width: 12),
                // 进度条和时间
                Expanded(
                  child: Column(
                    children: [
                      // 进度条
                      SliderTheme(
                        data: SliderTheme.of(context).copyWith(
                          trackHeight: 3,
                          thumbShape: const RoundSliderThumbShape(
                            enabledThumbRadius: 6,
                          ),
                          overlayShape: const RoundSliderOverlayShape(
                            overlayRadius: 12,
                          ),
                        ),
                        child: Slider(
                          value: _duration.inMilliseconds > 0
                              ? _position.inMilliseconds /
                                    _duration.inMilliseconds
                              : 0.0,
                          onChanged: (value) {
                            final position = Duration(
                              milliseconds: (value * _duration.inMilliseconds)
                                  .round(),
                            );
                            _seek(position);
                          },
                          activeColor: Theme.of(context).primaryColor,
                          inactiveColor: Colors.grey[300],
                        ),
                      ),
                      // 时间显示
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Text(
                            _formatDuration(_position),
                            style: TextStyle(
                              fontSize: 12,
                              color: Colors.grey[600],
                            ),
                          ),
                          Text(
                            _formatDuration(_duration),
                            style: TextStyle(
                              fontSize: 12,
                              color: Colors.grey[600],
                            ),
                          ),
                        ],
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ],
        ),
      );
    }
  }
  