forked from juananime/react-native-audiowaveform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
86 lines (69 loc) · 2.44 KB
/
index.js
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
/**
* Created by juanjimenez on 07/12/2016.
* Otomogroove ltd 2017
*/
"use strict";
import React, { PureComponent } from "react";
import { Platform, processColor, DeviceEventEmitter, requireNativeComponent } from "react-native";
import resolveAssetSource from "react-native/Libraries/Image/resolveAssetSource";
type StateType = { componentID: string };
export default class WaveForm extends PureComponent<WaveObjectPropsType, StateType> {
constructor(props) {
super(props);
this._onPress = this._onPress.bind(this);
this._onFinishPlay = this._onFinishPlay.bind(this);
}
_makeid() {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (var i = 0; i < 5; i++) text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
_onPress(e) {
const event = Platform.OS === "ios" ? e.nativeEvent : e;
if (event.componentID === this.state.componentID && this.props.onPress) {
this.props.onPress(event);
}
}
_onFinishPlay(e) {
const event = Platform.OS === "ios" ? e.nativeEvent : e;
if (event.componentID === this.state.componentID && this.props.onFinishPlay) {
this.props.onFinishPlay(event);
}
}
componentWillMount() {
DeviceEventEmitter.addListener("OGOnPress", this._onPress);
DeviceEventEmitter.addListener("OGFinishPlay", this._onFinishPlay);
const componentID = this._makeid();
this.setState({ componentID });
}
render() {
const { source } = this.props;
const { componentID } = this.state;
const assetResoved = resolveAssetSource(source) || {};
let uri = assetResoved.uri;
if (uri && uri.match(/^\//)) {
uri = `file://${uri}`;
}
const isNetwork = !!(uri && uri.match(/^https?:/));
const isAsset = !!(uri && uri.match(/^(assets-library|file|content|ms-appx|ms-appdata):/));
const nativeProps = {
...this.props,
waveFormStyle: {
ogWaveColor: processColor(this.props.waveFormStyle.waveColor),
ogScrubColor: processColor(this.props.waveFormStyle.scrubColor)
},
src: {
uri,
isNetwork,
isAsset,
type: source.type,
mainVer: source.mainVer || 0,
patchVer: source.patchVer || 0
},
componentID
};
return <OGWaverformView {...nativeProps} onPress={this._onPress} onFinishPlay={this._onFinishPlay} />;
}
}
const OGWaverformView = requireNativeComponent("OGWave", WaveForm);