@@ -94,9 +94,25 @@ const Sound = React.createClass({
9494 /**
9595 * Controls the playback status. If not set, the value of `autoPlay` determines
9696 * whether the audio plays when the component is loaded.
97+ * This is going to be renamed to `playControl`, please migrate to use `playControl`
9798 */
9899 playStatus : PropTypes . oneOf ( [ 'play' , 'pause' , 'stop' ] ) ,
99100
101+ /**
102+ * Controls the playback status. If not set, the value of `autoPlay` determines
103+ * whether the audio plays when the component is loaded.
104+ */
105+ playControl : PropTypes . oneOf ( [ 'play' , 'pause' , 'stop' ] ) ,
106+
107+ /**
108+ * playerState - playerState is a `MediaPlayerState` that controls video
109+ * playback with its inner state. When playerState is set, the value of
110+ * `autoPlay`, `muted` `volume` and `playControl` properties are ignored
111+ * as they will be set by playerState instead.
112+ * See [MediaPlayerState](docs/mediaplayerstate.html)
113+ */
114+ playerState : PropTypes . object ,
115+
100116 /**
101117 * Value of the audio volume. Minimum is zero, which mutes the sound, and the suggested
102118 * maximum is 1.0, which is also the default value. Values greater than 1 are allowed
@@ -121,11 +137,91 @@ const Sound = React.createClass({
121137 autoPlay : true ,
122138 volume : 1.0 ,
123139 loop : false ,
124- playStatus : null ,
140+ playControl : null ,
125141 source : null ,
126142 } ;
127143 } ,
128144
145+ getInitialState : function ( ) {
146+ return {
147+ volume : 1.0 ,
148+ muted : false ,
149+ } ;
150+ } ,
151+
152+ componentWillMount ( ) {
153+ if ( __DEV__ ) {
154+ if ( this . props . playStatus ) {
155+ console . warn (
156+ 'playStatus has been renamed to playControl. Please update your code before v2.0.0'
157+ ) ;
158+ }
159+ }
160+ if ( this . props . playerState ) {
161+ this . _subscribe ( this . props . playerState ) ;
162+ }
163+ } ,
164+
165+ componentWillReceiveProps ( nextProps ) {
166+ if ( __DEV__ ) {
167+ if ( nextProps . playStatus ) {
168+ console . warn (
169+ 'playStatus has been renamed to playControl. Please update your code before v2.0.0'
170+ ) ;
171+ }
172+ }
173+ if ( this . props . playerState !== nextProps . playerState ) {
174+ if ( this . props . playerState ) {
175+ this . _unsubscribe ( this . props . playerState ) ;
176+ }
177+ if ( nextProps . playerState ) {
178+ this . _subscribe ( nextProps . playerState ) ;
179+ }
180+ }
181+ } ,
182+
183+ componentWillUnmount ( ) {
184+ if ( this . props . playerState ) {
185+ this . _unsubscribe ( this . props . playerState ) ;
186+ }
187+ } ,
188+
189+ _subscribe ( playerState ) {
190+ playerState . addListener ( 'play' , this . _play ) ;
191+ playerState . addListener ( 'pause' , this . _pause ) ;
192+ playerState . addListener ( 'seekTo' , this . _seekTo ) ;
193+ playerState . addListener ( 'volumeChange' , this . _volumeChange ) ;
194+ playerState . addListener ( 'mutedChange' , this . _mutedChange ) ;
195+ this . setState ( {
196+ volume : playerState . volume ,
197+ muted : playerState . muted ,
198+ } ) ;
199+ } ,
200+
201+ _unsubscribe ( playerState ) {
202+ playerState . removeListener ( 'play' , this . _play ) ;
203+ playerState . removeListener ( 'pause' , this . _pause ) ;
204+ playerState . removeListener ( 'seekTo' , this . _seekTo ) ;
205+ playerState . removeListener ( 'volumeChange' , this . _volumeChange ) ;
206+ playerState . removeListener ( 'mutedChange' , this . _mutedChange ) ;
207+ } ,
208+
209+ _play ( ) {
210+ UIManager . dispatchViewManagerCommand (
211+ ReactNative . findNodeHandle ( this ) ,
212+ UIManager . Sound . Commands . play ,
213+ [ ]
214+ ) ;
215+ } ,
216+
217+ _pause ( ) {
218+ UIManager . dispatchViewManagerCommand (
219+ ReactNative . findNodeHandle ( this ) ,
220+ UIManager . Sound . Commands . pause ,
221+ [ ]
222+ ) ;
223+ } ,
224+
129225 _seekTo ( timeSec ) {
130226 UIManager . dispatchViewManagerCommand (
131227 ReactNative . findNodeHandle ( this ) ,
@@ -134,6 +230,14 @@ const Sound = React.createClass({
134230 ) ;
135231 } ,
136232
233+ _volumeChange ( volume ) {
234+ this . setState ( { volume : volume } ) ;
235+ } ,
236+
237+ _mutedChange ( muted ) {
238+ this . setState ( { muted : muted } ) ;
239+ } ,
240+
137241 _onEnded : function ( ) {
138242 this . props . onEnded && this . props . onEnded ( ) ;
139243 } ,
@@ -146,6 +250,19 @@ const Sound = React.createClass({
146250 console . warn ( '<Sound> must be a leaf node, props.children will not be rendered' ) ;
147251 }
148252 }
253+ if ( props . playStatus && ! props . playControl ) {
254+ props . playControl = props . playStatus ;
255+ delete props [ 'playStatus' ] ;
256+ }
257+ if ( this . props . playerState ) {
258+ props . autoPlay = false ;
259+ props . volume = this . state . volume ;
260+ props . muted = this . state . muted ;
261+ // events
262+ props . onDurationChange = this . props . playerState . onDurationChange ;
263+ props . onTimeUpdate = this . props . playerState . onTimeUpdate ;
264+ props . onPlayStatusChange = this . props . playerState . onPlayStatusChange ;
265+ }
149266
150267 const source = resolveAssetSource ( props . source ) ;
151268
@@ -154,9 +271,9 @@ const Sound = React.createClass({
154271 return (
155272 < RKSound
156273 style = { [ { position : 'absolute' } , props . style ] }
157- { ...this . props }
274+ { ...props }
158275 onEnded = { this . _onEnded }
159- testID = { this . props . testID }
276+ testID = { props . testID }
160277 onStartShouldSetResponder = { ( ) => true }
161278 onResponderTerminationRequest = { ( ) => false }
162279 />
0 commit comments