0
10
47
58
请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

_onLoad(),onProgress(). 等这些方法都没有回调,控制台没有打印??

var Detail=React.createClass({

getInitialState(){

var data=this.props.row

return {

data:data,

muted:false,

rate:1,

resizeMode:'contain',

repeat:false,

videoReady:true,

videoProgress:0.01,

videoTotal:0,

currentTime:0

}

},

_backToList(){

this.props.navigator.pop()

},

_onLoadStart(){

console.log('load _onLoadStart')

},

_onLoad(){

console.log('load _onLoad')

},

_onProgress(data){

if(!this.state.videoReady){

this.setState({

videoReady:false

})

}

console.log('onProgress  '+ data)

console.dir(data)

var duration=data.playableDuration

console.log(duration)

var currentTime=data.currentTime

var percent=Number((currentTime/duration))

// this.setState({

// videoTotal:duration,

// currentTime:Number(data.currentTime),

// videoProgress:percent

// })

},

_onEnd(){

console.log('load _onEnd')

},

_onError(){

console.log()

console.log('load _onError')

},

render(){

var data=this.props.row

return (

<View style={styles.container}>

<Text onPress={this._backToList}>详情页面</Text>

<View style={styles.videoBox}>

<Video

ref='videoPlayer'

source={{uri:data.video}}

style={styles.video}

volume={5}

paused={false}

rate={this.state.rate}

muted={this.state.muted}

resizeMode={this.state.reizeMode}

repeat={this.state.repeat}

onLoadStart={this.loadStart} // 当视频开始加载时的回调函数

       onLoad={this._onLoad}    // 当视频加载完毕时的回调函数

       onProgress={this._onProgress}    //  进度控制,每250ms调用一次,以获取视频播放的进度

       onEnd={this._onEnd}           // 当视频播放完毕后的回调函数

       onError={this._onError}    // 当视频不能加载,或出错后的回调函数

/>

{

this.state.videoReady && <ActivityIndicator

color='#f00' style={styles.loading}  />

}

<View style={styles.progressBox}>

<View style={[styles.progressBar,

{width:width*this.state.videoProgress}]}>

</View>

</View>

</View>

</View>

)

}

})


正在回答 回答被采纳积分+3

插入代码

3回答

Scott 2016-12-10 08:58:44

哦,看到了,你这里还是老的开发的写法,不是 ES6 的组件方式,确实不需要 bind,你比对下这个代码,看看有没有哪里遗漏。

"react-native-video": "^0.8.0"


如果都没问题的话,就跟着补录课程,把版本都相应升级一下,包括代码的写法,也从 ES5 升级到 ES6 再试试


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */
// ES5
var React = require('react-native')
var Icon = require('react-native-vector-icons/Ionicons')
var Video = require('react-native-video').default
var Button = require('react-native-button')
var config = require('../common/config')
var request = require('../common/request')
var util = require('../common/util')
var StyleSheet = React.StyleSheet
var Text = React.Text
var View = React.View
var TouchableOpacity = React.TouchableOpacity
var Dimensions = React.Dimensions
var ListView = React.ListView
var Image = React.Image
var Modal = React.Modal
var AlertIOS = React.AlertIOS
var TextInput = React.TextInput
var ActivityIndicatorIOS = React.ActivityIndicatorIOS
var AsyncStorage = React.AsyncStorage
var width = Dimensions.get('window').width
var cachedResults = {
  nextPage: 1,
  items: [],
  total: 0
}
var Detail = React.createClass({
  getInitialState() {
    var data = this.props.data
    var ds = new ListView.DataSource({
      rowHasChanged: (r1, r2) => r1 !== r2
    })
    return {
      data: data,
      // comments
      dataSource: ds.cloneWithRows([]),
      // video loads
      videoOk: true,
      videoLoaded: false,
      playing: false,
      paused: false,
      videoProgress: 0.01,
      videoTotal: 0,
      currentTime: 0,
      // modal
      content: '',
      animationType: 'none',
      modalVisible: false,
      isSending: false,
      // video player
      rate: 1,
      muted: false,
      resizeMode: 'contain',
      repeat: false
    }
  },
  _pop() {
    this.props.navigator.pop()
  },
  _onLoadStart() {
    console.log('load start')
  },
  _onLoad() {
    console.log('loads')
  },
  _onProgress(data) {
    if (!this.state.videoLoaded) {
      this.setState({
        videoLoaded: true
      })
    }
    var duration = data.playableDuration
    var currentTime = data.currentTime
    var percent = Number((currentTime / duration).toFixed(2))
    var newState = {
      videoTotal: duration,
      currentTime: Number(data.currentTime.toFixed(2)),
      videoProgress: percent
    }
    if (!this.state.videoLoaded) {
      newState.videoLoaded = true
    }
    if (!this.state.playing) {
      newState.playing = true
    }
    this.setState(newState)
  },
  _onEnd() {
    this.setState({
      videoProgress: 1,
      playing: false
    })
  },
  _onError(e) {
    this.setState({
      videoOk: false
    })
  },
  _rePlay() {
    this.refs.videoPlayer.seek(0)
  },
  _pause() {
    if (!this.state.paused) {
      this.setState({
        paused: true
      })
    }
  },
  _resume() {
    if (this.state.paused) {
      this.setState({
        paused: false
      })
    }
  },
  componentDidMount() {
    var that = this
    AsyncStorage.getItem('user')
      .then((data) => {
        var user
        if (data) {
          user = JSON.parse(data)
        }
        if (user && user.accessToken) {
          that.setState({
            user: user
          }, function() {
            that._fetchData()
          })
        }
      })
  },
  _fetchData(page) {
    var that = this
    this.setState({
      isLoadingTail: true
    })
    request.get(config.api.base + config.api.comment, {
      accessToken: this.state.user.accessToken,
      creation: this.state.data._id,
      page: page
    })
      .then((data) => {
        if (data && data.success) {
          if (data.data.length > 0) {
            var items = cachedResults.items.slice()
            items = items.concat(data.data)
            cachedResults.nextPage += 1
            cachedResults.items = items
            cachedResults.total = data.total
             
            that.setState({
              isLoadingTail: false,
              dataSource: that.state.dataSource.cloneWithRows(cachedResults.items)
            })
          }
        }
        else {
          that.setState({
            isLoadingTail: false
          })
        }
      })
      .catch((error) => {
        this.setState({
          isLoadingTail: false
        })
        console.warn(error)
      })
  },
  _hasMore() {
    return cachedResults.items.length !== cachedResults.total
  },
  _fetchMoreData() {
    if (!this._hasMore() || this.state.isLoadingTail) {
      this.setState({
        isLoadingTail: false
      })
      return
    }
    var page = cachedResults.nextPage
    this._fetchData(page)
  },
  _renderFooter() {
    if (!this._hasMore() && cachedResults.total !== 0) {
      return (
        <View style={styles.loadingMore}>
          <Text style={styles.loadingText}>没有更多了</Text>
        </View>
      )
    }
    if (!this.state.isLoadingTail) {
      return <View style={styles.loadingMore} />
    }
    return <ActivityIndicatorIOS style={styles.loadingMore} />
  },
  _renderRow(row) {
    return (
      <View key={row._id} style={styles.replyBox}>
        <Image style={styles.replyAvatar} source={{uri: util.avatar(row.replyBy.avatar)}} />
        <View style={styles.reply}>
          <Text style={styles.replyNickname}>{row.replyBy.nickname}</Text>
          <Text style={styles.replyContent}>{row.content}</Text>
        </View>
      </View>
    )
  },
  _focus() {
    this._setModalVisible(true)
  },
  _blur() {
  },
  _closeModal() {
    this._setModalVisible(false)
  },
  _setModalVisible(isVisible) {
    this.setState({
      modalVisible: isVisible
    })
  },
  _renderHeader() {
    var data = this.state.data
    return (
      <View style={styles.listHeader}>
        <View style={styles.infoBox}>
          <Image style={styles.avatar} source={{uri: util.avatar(data.author.avatar)}} />
          <View style={styles.descBox}>
            <Text style={styles.nickname}>{data.author.nickname}</Text>
            <Text style={styles.title}>{data.title}</Text>
          </View>
        </View>
        <View style={styles.commentBox}>
          <View style={styles.comment}>
            <TextInput
              placeholder='敢不敢评论一个...'
              style={styles.content}
              multiline={true}
              onFocus={this._focus}
            />
          </View>
        </View>
        <View style={styles.commentArea}>
          <Text style={styles.commentTitle}>精彩评论</Text>
        </View>
      </View>
    )
  },
  _submit() {
    var that = this
    if (!this.state.content) {
      return AlertIOS.alert('留言不能为空!')
    }
    if (this.state.isSending) {
      return AlertIOS.alert('正在评论中!')
    }
    this.setState({
      isSending: true
    }, function() {
      var body = {
        accessToken: this.state.user.accessToken,
        comment: {
          creation: this.state.data._id,
          content: this.state.content
        }
      }
      var url = config.api.base + config.api.comment
      request.post(url, body)
        .then(function(data) {
          if (data && data.success) {
            var items = cachedResults.items.slice()
            var content = that.state.content
            items = data.data.concat(items)
            cachedResults.items = items
            cachedResults.total = cachedResults.total + 1
             
            that.setState({
              content: '',
              isSending: false,
              dataSource: that.state.dataSource.cloneWithRows(cachedResults.items)
            })
            that._setModalVisible(false)
          }
        })
        .catch((err) => {
          console.log(err)
          that.setState({
            isSending: false
          })
          that._setModalVisible(false)
          AlertIOS.alert('留言失败,稍后重试!')
        })
    })
  },
  render() {
    var data = this.props.data
    return (
      <View style={styles.container}>
        <View style={styles.header}>
          <TouchableOpacity style={styles.backBox} onPress={this._pop}>
            <Icon name='ios-arrow-back' style={styles.backIcon} />
            <Text style={styles.backText}>返回</Text>
          </TouchableOpacity>
          <Text style={styles.headerTitle} numberOflines={1}>
            视频详情页</Text>
        </View>
        <View style={styles.videoBox}>
          <Video
            ref='videoPlayer'
            source={{uri: util.video(data.qiniu_video)}}
            style={styles.video}
            volume={5}
            paused={this.state.paused}
            rate={this.state.rate}
            muted={this.state.muted}
            resizeMode={this.state.resizeMode}
            repeat={this.state.repeat}
            onLoadStart={this._onLoadStart}
            onLoad={this._onLoad}
            onProgress={this._onProgress}
            onEnd={this._onEnd}
            onError={this._onError} />
          {
            !this.state.videoOk && <Text style={styles.failText}>视频出错了!很抱歉</Text>
          }
          {
            !this.state.videoLoaded && <ActivityIndicatorIOS color='#ee735c' style={styles.loading} />
          }
          {
            this.state.videoLoaded && !this.state.playing
            ? <Icon
                onPress={this._rePlay}
                name='ios-play'
                size={48}
                style={styles.playIcon} />
            //: <Text></Text>
            null
          }
          {
            this.state.videoLoaded && this.state.playing
            ? <TouchableOpacity onPress={this._pause} style={styles.pauseBtn}>
              {
                this.state.paused
                ? <Icon onPress={this._resume} size={48} name='ios-play' style={styles.resumeIcon} />
                null
              }
            </TouchableOpacity>
            null
          }
          <View style={styles.progressBox}>
            <View style={[styles.progressBar, {width: width * this.state.videoProgress}]}></View>
          </View>
        </View>
        <ListView
          dataSource={this.state.dataSource}
          renderRow={this._renderRow}
          renderHeader={this._renderHeader}
          renderFooter={this._renderFooter}
          onEndReached={this._fetchMoreData}
          onEndReachedThreshold={20}
          enableEmptySections={true}
          showsVerticalScrollIndicator={false}
          automaticallyAdjustContentInsets={false}
        />
        <Modal
          visible={this.state.modalVisible}>
          <View style={styles.modalContainer}>
            <Icon
              onPress={this._closeModal}
              name='ios-close-outline'
              style={styles.closeIcon} />
            <View style={styles.commentBox}>
              <View style={styles.comment}>
                <TextInput
                  placeholder='敢不敢评论一个...'
                  style={styles.content}
                  multiline={true}
                  defaultValue={this.state.content}
                  onChangeText={(text) => {
                    this.setState({
                      content: text
                    })
                  }}
                />
              </View>
            </View>
            <Button style={styles.submitBtn} onPress={this._submit}>评论</Button>
          </View>
        </Modal>
      </View>
    )
  }
})
var styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#F5FCFF',
  },
  modalContainer: {
    flex: 1,
    paddingTop: 45,
    backgroundColor: '#fff'
  },
  closeIcon: {
    alignSelf: 'center',
    fontSize: 30,
    color: '#ee753c'
  },
  submitBtn: {
    width: width - 20,
    padding: 16,
    marginTop: 20,
    marginBottom: 20,
    borderWidth: 1,
    borderColor: '#ee753c',
    alignSelf: 'center',
    borderRadius: 4,
    fontSize: 18,
    color: '#ee753c'
  },
  header: {
    flexDirection: 'row',
    justifyContent: 'center',
    alignItems: 'center',
    width: width,
    height: 64,
    paddingTop: 20,
    paddingLeft: 10,
    paddingRight: 10,
    borderBottomWidth: 1,
    borderColor: 'rgba(0,0,0,0.1)',
    backgroundColor: '#fff'
  },
  backBox: {
    position: 'absolute',
    left: 12,
    top: 32,
    width: 50,
    flexDirection: 'row',
    alignItems: 'center'
  },
  headerTitle: {
    width: width - 120,
    textAlign: 'center'
  },
  backIcon: {
    color: '#999',
    fontSize: 20,
    marginRight: 5
  },
  backText: {
    color: '#999'
  },
  videoBox: {
    width: width,
    height: width * 0.56,
    backgroundColor: '#000'
  },
  video: {
    width: width,
    height: width * 0.56,
    backgroundColor: '#000'
  },
  failText: {
    position: 'absolute',
    left: 0,
    top: 90,
    width: width,
    textAlign: 'center',
    color: '#fff',
    backgroundColor: 'transparent'
  },
  loading: {
    position: 'absolute',
    left: 0,
    top: 80,
    width: width,
    alignSelf: 'center',
    backgroundColor: 'transparent'
  },
  progressBox: {
    width: width,
    height: 2,
    backgroundColor: '#ccc'
  },
  progressBar: {
    width: 1,
    height: 2,
    backgroundColor: '#ff6600'
  },
  playIcon: {
    position: 'absolute',
    top: 90,
    left: width / 2 - 30,
    width: 60,
    height: 60,
    paddingTop: 8,
    paddingLeft: 22,
    backgroundColor: 'transparent',
    borderColor: '#fff',
    borderWidth: 1,
    borderRadius: 30,
    color: '#ed7b66'
  },
  pauseBtn: {
    position: 'absolute',
    left: 0,
    top: 0,
    width: width,
    height: width * 0.56
  },
  resumeIcon: {
    position: 'absolute',
    top: 80,
    left: width / 2 - 30,
    width: 60,
    height: 60,
    paddingTop: 8,
    paddingLeft: 22,
    backgroundColor: 'transparent',
    borderColor: '#fff',
    borderWidth: 1,
    borderRadius: 30,
    color: '#ed7b66'
  },
  infoBox: {
    width: width,
    flexDirection: 'row',
    justifyContent: 'center',
    marginTop: 10
  },
  avatar: {
    width: 60,
    height: 60,
    marginRight: 10,
    marginLeft: 10,
    borderRadius: 30
  },
  descBox: {
    flex: 1
  },
  nickname: {
    fontSize: 18
  },
  title: {
    marginTop: 8,
    fontSize: 16,
    color: '#666'
  },
  replyBox: {
    flexDirection: 'row',
    justifyContent: 'flex-start',
    marginTop: 10
  },
  replyAvatar: {
    width: 40,
    height: 40,
    marginRight: 10,
    marginLeft: 10,
    borderRadius: 20
  },
  replyNickname: {
    color: '#666'
  },
  replyContent: {
    marginTop: 4,
    color: '#666'
  },
  reply: {
    flex: 1
  },
  loadingMore: {
    marginVertical: 20
  },
  loadingText: {
    color: '#777',
    textAlign: 'center'
  },
  listHeader: {
    width: width,
    marginTop: 10
  },
  commentBox: {
    marginTop: 10,
    marginBottom: 10,
    padding: 8,
    width: width
  },
  content: {
    paddingLeft: 2,
    color: '#333',
    borderWidth: 1,
    borderColor: '#ddd',
    borderRadius: 4,
    fontSize: 14,
    height: 80
  },
  commentArea: {
    width: width,
    paddingBottom: 6,
    paddingLeft: 10,
    paddingRight: 10,
    borderBottomWidth: 1,
    borderBottomColor: '#eee'
  }
})
module.exports = Detail


0 回复 有任何疑惑可以回复我~
Scott 2016-12-09 13:06:52

需要 bind 一下吧,比如 this._onProgress.bind(this) this._onEnd.bind(this) 等等

0 回复 有任何疑惑可以回复我~
提问者 慕函数5092039 2016-12-09 22:19:36

react-native 回退到0.35 以下就可以了

0 回复 有任何疑惑可以回复我~
问题已解决,确定采纳
还有疑问,暂不采纳
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号