File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ /// <reference types="wicg-mediasession" />
2+ import cplayer from './' ;
3+ import { IAudioItem } from '../lib/interfaces' ;
4+ export declare function cplayerMediaSessionPlugin ( player : cplayer ) : void ;
5+ export declare function mediaMetadata ( audio : IAudioItem ) : MediaMetadata ;
Original file line number Diff line number Diff line change 4242 "repository" : " https://github.com/MoePlayer/cPlayer" ,
4343 "devDependencies" : {
4444 "@types/node" : " ^8.0.15" ,
45+ "@types/wicg-mediasession" : " ^1.0.0" ,
4546 "autoprefixer" : " ^7.1.2" ,
4647 "awesome-typescript-loader" : " ^3.2.1" ,
4748 "babel-core" : " ^6.25.0" ,
Original file line number Diff line number Diff line change @@ -7,6 +7,7 @@ import { decodeLyricStr } from "./lyric";
77import { singlecyclePlaymode } from "./playmode/singlecycle" ;
88import { listrandomPlaymode } from "./playmode/listrandom" ;
99import shallowEqual from "./helper/shallowEqual" ;
10+ import { cplayerMediaSessionPlugin } from "./mediaSession" ;
1011
1112export interface ICplayerOption {
1213 playlist ?: Iplaylist ;
@@ -94,6 +95,7 @@ export default class cplayer extends EventEmitter {
9495 if ( options . autoplay && this . playlist . length > 0 ) {
9596 this . play ( ) ;
9697 }
98+ cplayerMediaSessionPlugin ( this )
9799 }
98100
99101 private initializeEventEmitter ( ) {
Original file line number Diff line number Diff line change @@ -6,7 +6,8 @@ export interface IAudioItem {
66 artist ?: string ; //艺术家
77 src : string ; //音频
88 lyric ?: Lyric | string ; // 歌词
9- sublyric ?: Lyric | string ; // 小歌词
9+ sublyric ?: Lyric | string ; // 小歌词;
10+ album ?: string ; // 专辑 & 唱片
1011}
1112
1213export type Iplaylist = IAudioItem [ ] ;
Original file line number Diff line number Diff line change 1+ import cplayer from './' ;
2+ import { IAudioItem } from '../lib/interfaces' ;
3+
4+ export function cplayerMediaSessionPlugin ( player : cplayer )
5+ {
6+ if ( 'mediaSession' in navigator ) {
7+ navigator . mediaSession . metadata = mediaMetadata ( player . nowplay ) ;
8+ navigator . mediaSession . setActionHandler ( 'play' , ( ) => player . play ( ) ) ;
9+ navigator . mediaSession . setActionHandler ( 'pause' , ( ) => player . pause ( ) ) ;
10+ navigator . mediaSession . setActionHandler ( 'previoustrack' , ( ) => player . prev ( ) ) ;
11+ navigator . mediaSession . setActionHandler ( 'nexttrack' , ( ) => player . next ( ) ) ;
12+ player . on ( 'openaudio' , ( ) => {
13+ navigator . mediaSession . metadata = mediaMetadata ( player . nowplay ) ;
14+ } )
15+ }
16+ }
17+
18+ export function mediaMetadata ( audio : IAudioItem ) {
19+ return new MediaMetadata ( {
20+ title : audio . name ,
21+ artist : audio . artist ,
22+ album : audio . album ,
23+ artwork : [
24+ { src : audio . poster }
25+ ]
26+ } ) ;
27+ }
You can’t perform that action at this time.
0 commit comments