0

我正在开发一个颤振的音频播放器应用程序,我真的是一个初学者,我想在我的应用程序上实现一个搜索器和持续时间功能,但我失败了。我想获得正在播放的歌曲的持续时间,并让用户知道他们播放歌曲的时间以及让他们寻找。请各位大侠帮忙。贝娄是我的代码。

import 'package:app/shared/MusicProgressIndicator.dart';
import 'package:app/screens/SingleSongScreen.dart';
import 'package:just_audio/just_audio.dart';

class SingleSongScreen extends StatefulWidget {
  static const String id = "SingleSongScreen";
  static var song = {};
  SingleSongScreen({Key key}) : super(key: key);

  @override
  _SingleSongScreenState createState() => _SingleSongScreenState();
}

class _SingleSongScreenState extends State<SingleSongScreen> with TickerProviderStateMixin {
  Duration currentDuration = Duration(milliseconds: 1000);
  bool showRemaining = false;
  AnimationController controller;
  final player  = AudioPlayer();

  @override
  void initState() {
    controller = AnimationController(
      vsync: this,
      duration: const Duration(seconds: 5),
    )..addListener(() {
        setState(() {});
      });
    controller.repeat(reverse: true);
    super.initState();
  }

  @override
  void dispose() {
    controller.dispose();
    player.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {

    var song      = SingleSongScreen.song;
    var songPath  = "";
    songPath      = app.base+"play.php?file="+song['song'];
    print(song);
    print(songPath);

    player.setUrl(songPath);

    return Window(
      backgroundColor: backgroundColor,
      header: Container(
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            IconicButton(
              icon: backIcon,
              iconColor: Colors.grey,
              transparent: true,
              circledIcon: false,
              size: 40,
              onClicked: () {
                goBack(context);
              },
            ),
            Favicon(onClicked: () {
              goto(page: MainScreen.id, context: context);
            }),
          ],
        ),
      ),
      body: [
        VerticalSpacer(
          h: 20,
        ),
        Container(
          height: 250,
          child: Carousel(
            boxFit: BoxFit.contain,
            autoplay: carouselAutoPlay,
            animationCurve: Curves.fastOutSlowIn,
            animationDuration: Duration(milliseconds: 1000),
            dotSize: 6.0,
            dotIncreasedColor: Color(0xFF33A3FF),
            dotBgColor: Colors.transparent,
            dotPosition: DotPosition.bottomCenter,
            dotVerticalPadding: 10.0,
            showIndicator: true,
            indicatorBgPadding: 7.0,
            images: images(),
          ),
        )
      ],
      footer: Container(
        child: Column(
          children: [
            Container(
              child: Marquee(
                child: Label(
                  song['title'],
                  bold: true,
                ),
              ),
            ),
            Container(
              child: Marquee(
                child: Label(
                  song['artist'],
                  bold: true,
                  color: Colors.grey,
                ),
              ),
            ),

            // ACTION ICONS SECTION
            VerticalSpacer(
              h: 25,
            ),
            Container(
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  VerticalIconicButton(
                    icon: FontAwesomeIcons.fileDownload,
                    label: "Download",
                    iconColor: Colors.grey,
                    textColor: Colors.grey,
                    scale: 1.2,
                    onClicked: (){

                    });
                    },
                  ),
                  VerticalIconicButton(
                    icon: Icons.favorite_border_outlined,
                    label: "Favourite",
                    iconColor: Colors.grey,
                    textColor: Colors.grey,
                    scale: 1.2,
                  ),
                  VerticalIconicButton(
                    icon: FontAwesomeIcons.readme,
                    label: "Lyrics",
                    iconColor: Colors.grey,
                    textColor: Colors.grey,
                    scale: 1.2,
                    onClicked: () {
                    
                    },
                  ),
                ],
              ),
            ),

            // SONG PLAY PROGRESS INDICATOR
            VerticalSpacer(),
            MusicProgressIndicator(
              progress: currentDuration,
              buffered: Duration(milliseconds: 0),
              total: Duration(milliseconds: 5000),
              showTotalRemainingTime: showRemaining,
              onSeek: (duration) {
                setState(() {
                  currentDuration = duration;
                  showRemaining = !showRemaining;
                });
                 player.seek(duration);
              },
            ),

            // MUSIC PLAY CONTROLLER ACTION BUTTON
            VerticalSpacer(
              h: 20,
            ),

            Container(
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  IconicButton(
                    icon: Icons.repeat_one,
                    iconColor: Colors.grey,
                    transparent: true,
                    circledIcon: false,
                    size: 50,
                  ),
                  Container(
                    child: Row(
                      children: [
                        Container(
                          child: Center(
                            child: IconicButton(
                              icon: Icons.skip_previous_rounded,
                              iconColor: Colors.white,
                              transparent: true,
                              circledIcon: false,
                              size: 50,
                            ),
                          ),
                        ),
                        SizedBox(width: 15),
                        IconicButton(
                          icon: Icons.play_circle_filled,
                          iconColor: Colors.grey,
                          iconCircleColor: Colors.blue,
                          size: 50,
                          onClicked: (){
                            //player.stop();
                            player.play();
                            currentDuration=player.position;
                          },
                        ),
                        SizedBox(
                          width: 15,
                        ),
                        Container(
                          child: Center(
                            child: IconicButton(
                              icon: Icons.skip_next_rounded,
                              iconColor: Colors.white,
                              transparent: true,
                              circledIcon: false,
                              size: 50,
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                  IconicButton(
                    icon: Icons.queue_music_outlined,
                    iconColor: Colors.grey,
                    transparent: true,
                    circledIcon: false,
                    size: 50,
                    onClicked: () {
                      goto(page: AllSongsScreen.id, context: context);
                    },
                  ),
                ],
              ),
            ),

            // BOTTTOM SPACE
            VerticalSpacer(
              h: 30,
            ),
          ],
        ),
      ),
    );
  }

  void toast(String message){
    ScaffoldMessenger.of(context).showSnackBar(SnackBar(
      content:  Text(message.toString()),
      duration: const Duration(seconds: 1),
      action: SnackBarAction(
        label: 'OK',
        onPressed: () { },
      ),
    )
    );
  }
}

MusicProgressIndicator.dart

import 'package:audio_video_progress_bar/audio_video_progress_bar.dart';
import 'package:flutter/material.dart';

class MusicProgressIndicator extends StatelessWidget {
  Duration progress, buffered, total;
  Function onSeek;
  bool showTotalRemainingTime, minified, showTime;
  MusicProgressIndicator({
    Key key,
    this.buffered,
    @required this.onSeek,
    @required this.progress,
    @required this.total,
    this.showTotalRemainingTime: false,
    this.showTime: true,
    this.minified: false,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ProgressBar(
      progress: progress,
      buffered: buffered,
      total: total,
      progressBarColor: Colors.blue.shade900,
      baseBarColor: Colors.white.withOpacity(0.24),
      bufferedBarColor: Colors.white.withOpacity(0.24),
      timeLabelLocation:
          showTime ? TimeLabelLocation.sides : TimeLabelLocation.none,
      timeLabelTextStyle: TextStyle(color: Colors.white),
      timeLabelType: showTotalRemainingTime
          ? TimeLabelType.remainingTime
          : TimeLabelType.totalTime,
      thumbColor: Colors.white,
      barHeight: minified ? 2.0 : 5.0,
      thumbRadius: minified ? 1.0 : 5.0,
      onSeek: (duration) {
        onSeek(duration);
        print('User selected a new time: $duration');
       
      },
    );
  }
}
4

0 回答 0