@aochuang/common 1.0.152 → 1.0.154

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -50,14 +50,48 @@ export default class VoicePlayerAudio {
50
50
  })
51
51
  this.$audio.onerror = () => {
52
52
  clearInterval(this._timer)
53
- this.options.onError && this.options.onError({
54
- url: this._url,
55
- message: 'Invalid Url'
56
- })
53
+ const info = this._getErrorInfo()
54
+ // 元数据已经加载成功后才出现的解码错误(如mp3尾帧不完整,Windows Chrome的Symphonia解码器会报错),
55
+ // 不当成错误处理,直接按播放结束收尾
56
+ if (info.code === 3 && info.readyState > 0) {
57
+ this.options.onPlaying && this.options.onPlaying({
58
+ currPosition: Math.min(this.$audio.currentTime, this.$audio.duration),
59
+ totalPosition: this.$audio.duration
60
+ })
61
+ this._play = false
62
+ this.$audio.pause()
63
+ this.$audio.dispatchEvent(new Event('stop'))
64
+ return
65
+ }
66
+ this.options.onError && this.options.onError(info)
57
67
  }
58
68
  this.setPlaybackRate(this.options.playRate)
59
69
  }
60
70
 
71
+ /**
72
+ * 获取audio元素的真实错误信息(MediaError code/message),方便定位是网络、解码还是格式不支持
73
+ */
74
+ _getErrorInfo () {
75
+ const err = this.$audio.error
76
+ const codeMsgMap = {
77
+ 1: 'MEDIA_ERR_ABORTED',
78
+ 2: 'MEDIA_ERR_NETWORK',
79
+ 3: 'MEDIA_ERR_DECODE',
80
+ 4: 'MEDIA_ERR_SRC_NOT_SUPPORTED'
81
+ }
82
+ const info = {
83
+ url: this._url,
84
+ message: 'Invalid Url',
85
+ code: err ? err.code : null,
86
+ codeText: err ? codeMsgMap[err.code] : null,
87
+ detail: err ? err.message : null,
88
+ networkState: this.$audio.networkState,
89
+ readyState: this.$audio.readyState
90
+ }
91
+ console.warn('[VoicePlayerAudio] audio error', info)
92
+ return info
93
+ }
94
+
61
95
  /**
62
96
  * 设置播放倍率
63
97
  */
@@ -99,13 +133,13 @@ export default class VoicePlayerAudio {
99
133
  }
100
134
  const onError = () => {
101
135
  cleanup()
102
- this.options.onError && this.options.onError({
103
- url: this._url,
104
- message: 'Invalid Url'
105
- })
136
+ const info = this._getErrorInfo()
137
+ this.options.onError && this.options.onError(info)
106
138
  cb && cb({
107
139
  status: 'error',
108
- message: 'Invalid Url'
140
+ message: info.message,
141
+ code: info.code,
142
+ detail: info.detail
109
143
  })
110
144
  }
111
145
  const cleanup = () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aochuang/common",
3
- "version": "1.0.152",
3
+ "version": "1.0.154",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "publishConfig": {