【问题标题】:how to play audio from previous screen in flutter?如何在颤动中播放上一个屏幕的音频?
【发布时间】:2020-08-11 08:28:15
【问题描述】:

有一个带有 url 的歌曲列表,我正在使用Assets_Audio_Player,它可以从 url 播放音频,当我添加播放按钮和播放功能时,它会在我流式传输名称的地方播放这首歌曲。但我创建了一个播放器,我想要它,这样当我点击歌曲时,它会转到播放器并播放歌曲。

歌曲列表

class AryanaSayeed extends StatefulWidget {
  @override
  _AryanaSayeedState createState() => _AryanaSayeedState();
}

class _AryanaSayeedState extends State<AryanaSayeed> {
  var played = 0;
  final assetsAudioPlayer = AssetsAudioPlayer.withId("0");

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.red[500],
        title: Text('Aryana Sayeed'),
        centerTitle: true,
        actions: <Widget>[
          Icon(
            Icons.search,
          ),
          SizedBox(width: 10)
        ],
      ),
      body: StreamBuilder(
        stream: Firestore.instance.collection('aryana sayeed').snapshots(),
        builder: (
          context,
          snapshot,
        ) {
          if (snapshot.data == null)
            return Center(
              child: CircularProgressIndicator(
                backgroundColor: Colors.red,
                valueColor: new AlwaysStoppedAnimation<Color>(Colors.teal),
              ),
            );
          return ListView.builder(
              itemCount: snapshot.data.documents.length,
              itemBuilder: (context, index) {
                return SingleChildScrollView(
                    child: Column(
                  children: <Widget>[
                    ListTile(
                      onTap: () {
                        Navigator.push(
                            context,
                            MaterialPageRoute(
                              builder: (context) => Player1(),
                            ));
                      },
                      leading: Icon(
                        Icons.audiotrack,
                        color: Colors.red,
                      ),
                      title: Text(
                        snapshot.data.documents[index]['name'],
                        style: TextStyle(fontFamily: 'Font'),
                      ),
                    ),
                    Divider(
                      thickness: 1,
                      color: Colors.red,
                      indent: 40,
                      endIndent: 40,
                    ),
                  ],
                ));
              });
        },
      ),
    );
  }
}

数据库 播放器

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

class Player1 extends StatefulWidget {
  @override
  _Player1State createState() => _Player1State();
}

class _Player1State extends State<Player1> {
  final assetsAudioPlayer = AssetsAudioPlayer.withId("0");

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
          backgroundColor: Colors.grey[800],
          body: Padding(
            padding: const EdgeInsets.all(10.0),
            child: Column(
              children: <Widget>[
                /////////back button////options////////////
                Padding(
                  padding: const EdgeInsets.all(25.0),
                  child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: <Widget>[
                        InkWell(
                          onTap: () {},
                          child: Container(
                            height: 30,
                            width: 60,
                            decoration: BoxDecoration(
                              border: Border.all(
                                  color: Colors.blueAccent, width: 0.5),
                              borderRadius: BorderRadius.circular(100),
                              color: Colors.grey[800],
                              boxShadow: [
                                BoxShadow(
                                  color: Colors.grey[700],
                                  spreadRadius: 2,
                                  blurRadius: 2,
                                  offset: Offset(0, 1),
                                ),
                              ],
                            ),
                            child: Icon(
                              Icons.arrow_back,
                              color: Colors.blueAccent,
                            ),
                          ),
                        ),
                        Image.asset(
                          'assets/logo.png',
                          height: 20,
                        ),
                        InkWell(
                          onTap: () {},
                          child: Container(
                              height: 30,
                              width: 65,
                              decoration: BoxDecoration(
                                border: Border.all(
                                    color: Colors.blueAccent, width: 0.5),
                                borderRadius: BorderRadius.circular(100),
                                color: Colors.grey[800],
                                boxShadow: [
                                  BoxShadow(
                                    color: Colors.grey[700],
                                    spreadRadius: 2,
                                    blurRadius: 2,
                                    offset: Offset(0, 1),
                                  ),
                                ],
                              ),
                              child: Center(
                                  child: InkWell(
                                onTap: () {
                                  Navigator.pushNamed(context, '/singers');
                                },
                                child: Text(
                                  'Singers',
                                  style: TextStyle(color: Colors.blueAccent),
                                ),
                              ))),
                        ),
                      ]),
                  /////end of ////back button////options////////////
                ),
                SizedBox(height: 70),
                /////////////////image//////////////////
                Image.asset(
                  'assets/back.png',
                  height: 180,
                ),
                ////////////////////end image////////////
                SizedBox(height: 70),
                ///////Singer name/////////////
                Text('singer name',
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: 16,
                    )),
                ///////////////Song Name//////////
                Text('song name',
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: 23,
                      fontWeight: FontWeight.bold,
                    )),
                ///////////////position
                SizedBox(height: 60),
                Padding(
                    padding: const EdgeInsets.all(15.0),
                    child: Divider(
                        thickness: 3,
                        color: Colors.blueAccent,
                        indent: 20,
                        endIndent: 20)),

                /////////////////////buttons//////////
                ///1///
                SizedBox(height: 70),
                Row(
                    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                    children: <Widget>[
                      InkWell(
                        onTap: () {
                          assetsAudioPlayer.previous();
                        },
                        child: Container(
                          height: 40,
                          width: 40,
                          decoration: BoxDecoration(
                            border: Border.all(
                                color: Colors.blueAccent, width: 0.5),
                            borderRadius: BorderRadius.circular(100),
                            color: Colors.grey[800],
                            boxShadow: [
                              BoxShadow(
                                color: Colors.grey[700],
                                spreadRadius: 2,
                                blurRadius: 2,
                                offset: Offset(0, 1),
                              ),
                            ],
                          ),
                          child: Icon(
                            Icons.skip_previous,
                            color: Colors.blueAccent,
                          ),
                        ),
                      ),
                      /////2
                      InkWell(
                        onTap: () {
                          assetsAudioPlayer..seekBy(Duration(seconds: -30));
                        },
                        child: Container(
                          alignment: FractionalOffset(0.5, 0.5),
                          height: 45,
                          width: 45,
                          decoration: BoxDecoration(
                            border: Border.all(
                                color: Colors.blueAccent, width: 0.5),
                            borderRadius: BorderRadius.circular(100),
                            color: Colors.grey[800],
                            boxShadow: [
                              BoxShadow(
                                color: Colors.grey[700],
                                spreadRadius: 2,
                                blurRadius: 2,
                                offset: Offset(0, 1),
                              ),
                            ],
                          ),
                          child: Text(
                            '-30s',
                            style: TextStyle(
                                color: Colors.blueAccent, fontSize: 12),
                          ),
                        ),
                      ),
                      ////3
                      InkWell(
                        onTap: () {
                          assetsAudioPlayer.playOrPause();
                        },
                        child: Container(
                          height: 55,
                          width: 55,
                          decoration: BoxDecoration(
                            border: Border.all(
                                color: Colors.blueAccent, width: 0.5),
                            borderRadius: BorderRadius.circular(100),
                            color: Colors.grey[800],
                            boxShadow: [
                              BoxShadow(
                                color: Colors.grey[700],
                                spreadRadius: 2,
                                blurRadius: 2,
                                offset: Offset(0, 1),
                              ),
                            ],
                          ),
                          child: Icon(
                            Icons.play_arrow,
                            color: Colors.blueAccent,
                          ),
                        ),
                      ),
                      /////////4
                      InkWell(
                        onTap: () {
                          assetsAudioPlayer.seekBy(Duration(seconds: 30));
                        },
                        child: Container(
                          alignment: FractionalOffset(0.5, 0.5),
                          height: 45,
                          width: 45,
                          decoration: BoxDecoration(
                            border: Border.all(
                                color: Colors.blueAccent, width: 0.5),
                            borderRadius: BorderRadius.circular(100),
                            color: Colors.grey[800],
                            boxShadow: [
                              BoxShadow(
                                color: Colors.grey[700],
                                spreadRadius: 2,
                                blurRadius: 2,
                                offset: Offset(0, 1),
                              ),
                            ],
                          ),
                          child: Text(
                            '+30s',
                            style: TextStyle(
                                color: Colors.blueAccent, fontSize: 12),
                          ),
                        ),
                      ),
                      //////////////5
                      InkWell(
                        onTap: () {
                          assetsAudioPlayer.next();
                        },
                        child: Container(
                          height: 40,
                          width: 40,
                          decoration: BoxDecoration(
                            border: Border.all(
                                color: Colors.blueAccent, width: 0.5),
                            borderRadius: BorderRadius.circular(100),
                            color: Colors.grey[800],
                            boxShadow: [
                              BoxShadow(
                                color: Colors.grey[700],
                                spreadRadius: 2,
                                blurRadius: 2,
                                offset: Offset(0, 1),
                              ),
                            ],
                          ),
                          child: Icon(
                            Icons.skip_next,
                            color: Colors.blueAccent,
                          ),
                        ),
                      ),
                    ]),
                SizedBox(height: 30),
              ],
            ),
          )),
    );
  }
}

              

【问题讨论】:

    标签: flutter dart google-cloud-firestore


    【解决方案1】:

    在歌曲列表文件中,ListTitle 小部件下的 onTap 函数应该是这样的:

    onTap: () {
                 Navigator.push(
                            context,
                            MaterialPageRoute(
                              builder: (context) => Player1(name: snapshot.data.documents[index]['name'], url: snapshot.data.documents[index]['url']),
                            ));
                      },
    

    那么播放器文件应该是这样的:

    import 'package:flutter/material.dart';
    
    class _PlayerState1 extends StatefulWidget {
     String name;
     String url;
     _PlayerState1({this.name, this.url});
     @override
     __PlayerState1State createState() => __PlayerState1State();
    }
    
    class __PlayerState1State extends State<_PlayerState1> {
      final assetsAudioPlayer = AssetsAudioPlayer.withId("0");
      @override
      Widget build(BuildContext context) {
      return Scaffold(
      body: GestureDetector(
        onTap: () {
          try {
            await assetsAudioPlayer.open(
              Audio.network(widget.url),
            );
          } catch (t) {
            //mp3 unreachable
          }
         },
        ),
       );
      }
     }
    

    【讨论】:

      猜你喜欢
      • 2020-07-14
      • 1970-01-01
      • 1970-01-01
      • 2020-08-10
      • 1970-01-01
      • 2022-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多