@@ -29,11 +29,11 @@ const Player = () => {
2929 return ( ) => {
3030 if ( "mediaSession" in navigator ) {
3131 navigator . mediaSession . metadata = null ;
32- navigator . mediaSession . setActionHandler ( "play" , null ) ;
33- navigator . mediaSession . setActionHandler ( " pause", null ) ;
34- navigator . mediaSession . setActionHandler ( "nexttrack" , null ) ;
35- navigator . mediaSession . setActionHandler ( "previoustrack" , null ) ;
36- navigator . mediaSession . setActionHandler ( "seekto" , null ) ;
32+ (
33+ [ "play" , " pause", "nexttrack" , "previoustrack" , "seekto" ] as const
34+ ) . forEach ( ( eventName ) =>
35+ navigator . mediaSession . setActionHandler ( eventName , null ) ,
36+ ) ;
3737 }
3838 } ;
3939 } , [ ] ) ;
@@ -79,23 +79,26 @@ const Player = () => {
7979 const controls = usePlayAudio ( rawUrl , undefined , next ) ;
8080
8181 // 设置媒体通知 https://developer.mozilla.org/en-US/docs/Web/API/Media_Session_API
82- if ( "mediaSession" in navigator ) {
83- navigator . mediaSession . metadata = new MediaMetadata ( {
84- title,
85- artist,
86- artwork : [ { src : coverUrl || "" } ] ,
87- } ) ;
88-
89- navigator . mediaSession . setActionHandler ( "play" , ( ) => controls . play ( ) ) ;
90- navigator . mediaSession . setActionHandler ( "pause" , ( ) => controls . pause ( ) ) ;
91- navigator . mediaSession . setActionHandler ( "nexttrack" , ( ) => next ( ) ) ;
92- navigator . mediaSession . setActionHandler ( "previoustrack" , prev ) ;
93- navigator . mediaSession . setActionHandler ( "seekto" , ( val ) => {
94- if ( val . seekTime ) {
95- controls . jump ( val . seekTime ) ;
96- }
97- } ) ;
98- }
82+ useEffect ( ( ) => {
83+ if ( "mediaSession" in navigator ) {
84+ navigator . mediaSession . metadata = new MediaMetadata ( {
85+ title,
86+ artist,
87+ artwork : [ { src : coverUrl || "" } ] ,
88+ } ) ;
89+
90+ navigator . mediaSession . setActionHandler ( "play" , ( ) => controls . play ( ) ) ;
91+ navigator . mediaSession . setActionHandler ( "pause" , ( ) => controls . pause ( ) ) ;
92+ navigator . mediaSession . setActionHandler ( "nexttrack" , ( ) => next ( ) ) ;
93+ navigator . mediaSession . setActionHandler ( "previoustrack" , prev ) ;
94+ navigator . mediaSession . setActionHandler ( "seekto" , ( val ) => {
95+ if ( val . seekTime ) {
96+ controls . jump ( val . seekTime ) ;
97+ }
98+ } ) ;
99+ }
100+ // eslint-disable-next-line react-hooks/exhaustive-deps
101+ } , [ musicInfo ] ) ;
99102
100103 const playerInfo = {
101104 ...controls ,
0 commit comments