11import { ProCard } from '@ant-design/pro-components' ;
2- import { useMount } from 'ahooks ' ;
2+ import { useEffect } from 'react ' ;
33import Player from 'xgplayer' ;
44import MusicPreset , { Analyze , Lyric } from 'xgplayer-music' ;
55import 'xgplayer-music/dist/index.min.css' ;
@@ -12,30 +12,34 @@ declare global {
1212 }
1313}
1414
15+ type PlayerWithMode = Player & {
16+ mode : number ;
17+ } ;
18+
1519export default function AudioPlayer ( ) {
16- useMount ( ( ) => {
17- function initEvents ( ) {
18- let jsSelect = document . getElementById ( 'js-select' ) as HTMLSelectElement ;
19- jsSelect . addEventListener ( 'change' , function ( e : any ) {
20- let value = e . target . value ;
21- if ( window . analyze ) {
22- window . analyze . mode = value ;
23- if ( value === 'lightning' ) {
24- window . analyze . options . count = 512 ;
25- window . analyze . options . stroke = 4 ;
20+ useEffect ( ( ) => {
21+ const jsSelect = document . getElementById ( 'js-select' ) as HTMLSelectElement | null ;
22+ const handleSelectChange = ( e : Event ) => {
23+ const value = ( e . target as HTMLSelectElement ) . value ;
24+ if ( window . analyze ) {
25+ window . analyze . mode = value ;
26+ if ( value === 'lightning' ) {
27+ window . analyze . options . count = 512 ;
28+ window . analyze . options . stroke = 4 ;
29+ } else {
30+ if ( value === 'waves' ) {
31+ window . analyze . options . stroke = 3 ;
2632 } else {
27- if ( value === 'waves' ) {
28- window . analyze . options . stroke = 3 ;
29- } else {
30- window . analyze . options . stroke = 2 ;
31- }
32- window . analyze . options . count = 256 ;
33+ window . analyze . options . stroke = 2 ;
3334 }
35+ window . analyze . options . count = 256 ;
3436 }
35- } ) ;
36- }
37- initEvents ( ) ;
38- let player = new Player ( {
37+ }
38+ } ;
39+
40+ jsSelect ?. addEventListener ( 'change' , handleSelectChange ) ;
41+
42+ const player = new Player ( {
3943 id : 'mse' ,
4044 url : '//sf1-cdn-tos.huoshanstatic.com/obj/media-fe/xgplayer_doc_video/music/audio.mp3' , //[{ src: '//sf1-cdn-tos.huoshanstatic.com/obj/media-fe/xgplayer_doc_video/music/audio.mp3', name: '林宥嘉·脆弱一分钟', poster: '//sf1-cdn-tos.huoshanstatic.com/obj/media-fe/xgplayer_doc_video/music/poster-small.jpeg' }],
4145 volume : 0.8 ,
@@ -55,34 +59,32 @@ export default function AudioPlayer() {
5559 } ,
5660 } ) ;
5761 player . crossOrigin = 'anonymous' ;
58- let lyricTxts = `[00:00.00] 脆弱一分钟\n[00:00.00] 作曲 : 林家谦\n[00:00.00] 作词 : 徐世珍/吴辉福\n[00:00.000]编曲:林家谦\n[00:00.000]时钟不要走\n[00:04.220]让我脆弱一分钟\n[00:07.440]要多久才能习惯被放手\n[00:15.800]马克杯空了 暖暖的温热\n[00:22.660]却还在我手中停留\n[00:27.960]\n[00:29.790]勇气不要走\n[00:32.200]给我理由再冲动\n[00:35.690]去相信爱情 就算还在痛\n[00:43.960]如果我不说不会有人懂\n[00:50.720]失去你我有多寂寞\n[00:55.610]还是愿意\n[00:57.580]付出一切仅仅为了一个好梦\n[01:03.980]梦里有人真心爱我 陪我快乐也陪我沉默\n[01:11.260]没有无缘无故的痛承受越多越成熟\n[01:18.630]能让你拥抱更好的我\n[01:25.030] 谁也不要走\n[01:28.270]应该是一种奢求\n[01:31.900]可是我只想 握紧你的手\n[01:39.780]我宁愿等候 也不愿错过\n[01:46.630]你对我微笑的时候\n[01:56.780]\n[02:18.910]还是愿意\n[02:21.320]用尽全力仅仅为了一个以后\n[02:27.870]哪怕生命并不温柔哪怕被幸福一再反驳\n[02:34.870]也要相信伤痕累累 其实只是在琢磨\n[02:42.070]能让你为之一亮 的我\n[02:53.910]\n[02:56.350]制作人:林宥嘉\n[02:57.750]制作助理:张婕汝\n[02:59.010]录音师:陈文骏、叶育轩\n[03:00.410]录音室:白金录音室\n[03:01.740]混音师:SimonLi @ nOiz\n[03:03.000]OP: Terence Lam Production & Co. (Warner/Chappell Music, HK Ltd.)\n[03:04.050]SP: Warner/Chappell Music Taiwan Ltd.\n[03:04.910]OP:Universal Ms Publ Ltd Taiwan\n` ;
62+ const lyricTxts = `[00:00.00] 脆弱一分钟\n[00:00.00] 作曲 : 林家谦\n[00:00.00] 作词 : 徐世珍/吴辉福\n[00:00.000]编曲:林家谦\n[00:00.000]时钟不要走\n[00:04.220]让我脆弱一分钟\n[00:07.440]要多久才能习惯被放手\n[00:15.800]马克杯空了 暖暖的温热\n[00:22.660]却还在我手中停留\n[00:27.960]\n[00:29.790]勇气不要走\n[00:32.200]给我理由再冲动\n[00:35.690]去相信爱情 就算还在痛\n[00:43.960]如果我不说不会有人懂\n[00:50.720]失去你我有多寂寞\n[00:55.610]还是愿意\n[00:57.580]付出一切仅仅为了一个好梦\n[01:03.980]梦里有人真心爱我 陪我快乐也陪我沉默\n[01:11.260]没有无缘无故的痛承受越多越成熟\n[01:18.630]能让你拥抱更好的我\n[01:25.030] 谁也不要走\n[01:28.270]应该是一种奢求\n[01:31.900]可是我只想 握紧你的手\n[01:39.780]我宁愿等候 也不愿错过\n[01:46.630]你对我微笑的时候\n[01:56.780]\n[02:18.910]还是愿意\n[02:21.320]用尽全力仅仅为了一个以后\n[02:27.870]哪怕生命并不温柔哪怕被幸福一再反驳\n[02:34.870]也要相信伤痕累累 其实只是在琢磨\n[02:42.070]能让你为之一亮 的我\n[02:53.910]\n[02:56.350]制作人:林宥嘉\n[02:57.750]制作助理:张婕汝\n[02:59.010]录音师:陈文骏、叶育轩\n[03:00.410]录音室:白金录音室\n[03:01.740]混音师:SimonLi @ nOiz\n[03:03.000]OP: Terence Lam Production & Co. (Warner/Chappell Music, HK Ltd.)\n[03:04.050]SP: Warner/Chappell Music Taiwan Ltd.\n[03:04.910]OP:Universal Ms Publ Ltd Taiwan\n` ;
5963
6064 // 初始化频谱
61- // eslint-disable-next-line @typescript-eslint/no-unused-vars
62- let analyze = new Analyze ( player , document . querySelector ( 'canvas' ) as HTMLElement , {
65+ const analyze = new Analyze ( player , document . querySelector ( 'canvas' ) as HTMLElement , {
6366 bgColor : 'rgba(0,0,0,0.7)' ,
6467 stroke : 3 ,
6568 } ) ;
69+ window . analyze = analyze ;
6670
6771 // 初始化歌词模块
68- let lyric = new Lyric ( [ lyricTxts ] , document . querySelector ( '#gc' ) ) ;
72+ const lyric = new Lyric ( [ lyricTxts ] , document . querySelector ( '#gc' ) ) ;
6973 lyric . bind ( player ) ;
70- // eslint-disable-next-line @typescript-eslint/no-unused-vars
71- let nullText = 0 ;
72- player . on ( 'lyricUpdate' , ( res ) => {
73- if ( res . lyric === '\n' ) {
74- nullText ++ ;
75- }
76- } ) ;
77-
7874 player . on ( 'playing' , function ( ) {
7975 lyric . show ( ) ;
80- ( player as any ) . mode = 2 ;
76+ ( player as PlayerWithMode ) . mode = 2 ;
8177 } ) ;
8278 let canvasDom = document . getElementById ( 'canvas' ) as HTMLCanvasElement ;
8379 canvasDom . width = window . innerWidth ;
8480 canvasDom . height = window . innerHeight * 0.25 ;
85- } ) ;
81+
82+ return ( ) => {
83+ jsSelect ?. removeEventListener ( 'change' , handleSelectChange ) ;
84+ window . analyze = undefined ;
85+ player . destroy ( ) ;
86+ } ;
87+ } , [ ] ) ;
8688
8789 return (
8890 < AudioPlayerStyles >
0 commit comments