欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Flutter 實(shí)現(xiàn)網(wǎng)易云音樂字幕的代碼

 更新時間:2020年04月15日 08:43:04   作者:老孟程序員  
這篇文章主要介紹了Flutter 實(shí)現(xiàn)網(wǎng)易云音樂字幕的代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

沒有接觸過音樂字幕方面知識的話,會對字幕的實(shí)現(xiàn)比較迷茫,什么時候轉(zhuǎn)到下一句?看了這篇文章,你就會明白字幕so easy。

先來一張效果圖:

字幕格式

目前市面上有很多種字幕格式,比如srt, ssa, ass(文本形式)和idx+sub(圖形格式),但不管哪一種格式都會包含2個屬性:時間戳和字幕內(nèi)容,格式如下:

00:00 歌詞:
00:25 我要穿越這片沙漠
00:28 找尋真的自我
00:30 身邊只有一匹駱駝陪我
00:34 這片風(fēng)兒吹過
00:36 那片云兒飄過

上面字幕的意思是:在25秒的時候跳轉(zhuǎn)到下一句,在28秒的時候跳轉(zhuǎn)到下一句...

字幕實(shí)現(xiàn)

了解了字幕文件的形式,字幕實(shí)現(xiàn)起來就比較簡單了,使用ListWheelScrollView控件,然后通過ScrollController在合適的時機(jī)進(jìn)行滾動,使當(dāng)前字幕始終保持在屏幕中間。

解析字幕文件,獲取字幕數(shù)據(jù):

loadData() async {
 var jsonStr =
 await DefaultAssetBundle.of(context).loadString('assets/subtitle.txt');
 var list = jsonStr.split(RegExp('\n'));
 list.forEach((f) {
 if (f.isNotEmpty) {
 var r = f.split(RegExp(' '));
 if (r.length >= 2) {
 _subtitleList.add(SubtitleEntry(r[0], r[1]));
 }
 }
 });
 setState(() {});
}

設(shè)置字幕控件及背景圖片:

@override
Widget build(BuildContext context) {
 return Scaffold(
 appBar: AppBar(
 title: Text('彈幕'),
 ),
 body: Stack(
 children: <Widget>[
 Positioned.fill(
  child: Image.asset(
  'assets/imgs/background.png',
  fit: BoxFit.cover,
 )),
 Positioned.fill(
  child: Subtitle(
  _subtitleList,
  selectedTextStyle: TextStyle(color: Colors.white,fontSize: 18),
  unSelectedTextStyle: TextStyle(
  color: Colors.black.withOpacity(.6),
  ),
  diameterRatio: 5,
  itemExtent: 45,
 ))
 ],
 ),
 );
}

字幕控件的構(gòu)建:

@override
Widget build(BuildContext context) {
 if (widget.data == null || widget.data.length == 0) {
 return Container();
 }
 return ListWheelScrollView.useDelegate(
 controller: _controller,
 diameterRatio: widget.diameterRatio,
 itemExtent: widget.itemExtent,
 childDelegate: ListWheelChildBuilderDelegate(
 builder: (context, index) {
  return Container(
  alignment: Alignment.center,
  child: Text(
  '${widget.data[index].content}',
  style: _currentIndex == index
   ? widget.selectedTextStyle
   : widget.unSelectedTextStyle,
  ),
  );
 },
 childCount: widget.data.length),
 );
}

字幕控件封裝了選中字體和未選中字體樣式參數(shù),用法如下:

Subtitle(
	_subtitleList,
	selectedTextStyle: TextStyle(color: Colors.white,fontSize: 18),
	unSelectedTextStyle: TextStyle(
 	color: Colors.black.withOpacity(.6),
	)
)

效果如下:

設(shè)置圓筒直徑和主軸渲染窗口的尺寸的比,默認(rèn)值是2,越小表示圓筒越圓

Subtitle(
	_subtitleList,
	diameterRatio: 5,
)

下面是1和5的對比:

Github地址:https://github.com/781238222/flutter-do/tree/master/flutter_subtitle_example

交流

Github地址:https://github.com/781238222/flutter-do

170+組件詳細(xì)用法:http://laomengit.com

總結(jié)

到此這篇關(guān)于Flutter 實(shí)現(xiàn)網(wǎng)易云音樂字幕的文章就介紹到這了,更多相關(guān)Flutter 實(shí)現(xiàn)網(wǎng)易云音樂字幕內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論