|
1 | | -# React-APlayer |
| 1 | +# React-APlayer []() |
| 2 | +A React wrapper component of [APlayer](https://github.com/DIYgod/APlayer) |
| 3 | + |
| 4 | +## Introduction |
| 5 | +[Demo](http://sabrinaluo.com/react-aplayer/) |
| 6 | + |
| 7 | +Screen shot |
| 8 | +[]() |
| 9 | + |
| 10 | +## Usage |
| 11 | +``` |
| 12 | +import React from 'react'; |
| 13 | +import ReactAplayer from 'react-aplayer'; |
| 14 | +
|
| 15 | +export default class App extends React.Component { |
| 16 | + constructor(props) { |
| 17 | + super(props); |
| 18 | + this.state = {}; |
| 19 | + } |
| 20 | +
|
| 21 | + playHandler() { |
| 22 | + console.log('on play'); |
| 23 | + } |
| 24 | +
|
| 25 | + pauseHandler() { |
| 26 | + console.log('on pause'); |
| 27 | + } |
| 28 | +
|
| 29 | + render() { |
| 30 | + const props = { |
| 31 | + "autoplay": true, |
| 32 | + "mutex": true, |
| 33 | + "preload": "metadata", |
| 34 | + "music": { |
| 35 | + "author": "Hans Zimmer/Richard Harvey", |
| 36 | + "url": "http://devtest.qiniudn.com/Preparation.mp3", |
| 37 | + "pic": "http://devtest.qiniudn.com/Preparation.jpg" |
| 38 | + } |
| 39 | + }; |
| 40 | +
|
| 41 | + return ( |
| 42 | + <ReactAplayer {...props} onPlay={this.playHandler} onPause={this.pauseHandler}/> |
| 43 | + ); |
| 44 | + } |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +### Props |
| 49 | +Props are almost the same with original APlayer, please check the [docs](https://aplayer.js.org/docs/#/) for more details. |
| 50 | + |
| 51 | +| Name | Type | Required | Default | Description | |
| 52 | +|---|---|---|---|---| |
| 53 | +|autoplay| Boolean | N |`false` | | |
| 54 | +|listmaxheight| String | N | N/A |e.g. `'80px'` | |
| 55 | +|mode| String | |`'circulation'`|One of `'circulation'`, `'order'`, `'random'`, `'single'`| |
| 56 | +|mute| Boolean | |`false`| | |
| 57 | +|narrow| Boolean | |`false`| | |
| 58 | +|preload| String | |`'auto'`|One of `'auto'`, `'metadata'`, `'none'`| |
| 59 | +|showlrc| Number | |`0`| | |
| 60 | +|theme| String | |`'#b7daff'`| | |
| 61 | +|music| Array \| Object | Y | N/A | | |
| 62 | +|music.url| String | Y | | | |
| 63 | +|music.title| String | | N/A | | |
| 64 | +|music.author| String | | N/A | | |
| 65 | +|music.pic| String | | N/A | | |
| 66 | +|music.lrc| String | | N/A | | |
| 67 | + |
| 68 | +### Event Handlers |
| 69 | +Event handlers are triggered when corresponding event happens, it takes a callback function as param. |
| 70 | + |
| 71 | +|Name|Type | Description | |
| 72 | +|---|---|---| |
| 73 | +|onPlay| Func | | |
| 74 | +|onCanplay| Func | | |
| 75 | +|onPlay| Func | | |
| 76 | +|onPause| Func | | |
| 77 | +|onPlaying| Func | | |
| 78 | +|onEnded| Func | | |
| 79 | +|onError| Func | | |
| 80 | + |
| 81 | +### APlayer Instance & Bind Methods |
| 82 | +APlayer Instance can be accessed through `.state.control`. |
| 83 | + |
| 84 | +``` |
| 85 | +<ReactAplayer {...props} ref={(ap) => { |
| 86 | + const aplayer = ap.state.control; |
| 87 | + }/> |
| 88 | +``` |
| 89 | + |
| 90 | +To expose APlayer methods on React level, binding is needed. |
| 91 | +``` |
| 92 | +<ReactAplayer {...props} ref={(ap) => { |
| 93 | + const player = ap.state.control; |
| 94 | + this.play = player.play.bind(player); |
| 95 | + }}/> |
| 96 | +``` |
| 97 | + |
| 98 | +## LICENSE |
| 99 | +[MIT License](https://github.com/sabrinaluo/react-aplayer/blob/master/LICENSE) |
0 commit comments