-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
189 lines (169 loc) · 5.68 KB
/
App.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
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
import * as Font from 'expo-font';
import React, {Component} from 'react';
import {Platform, StatusBar, StyleSheet, View} from 'react-native';
import AppNavigator from './navigation/AppNavigator';
import {Audio} from "expo-av";
import {getAllSongs} from "./services/SongService";
import {LinearGradient} from "expo-linear-gradient";
import Colors from "./constants/Colors";
import {responsiveFontSize, responsiveHeight, responsiveWidth} from "react-native-responsive-dimensions";
export default class App extends React.Component {
constructor(props) {
super(props);
const sound = new Audio.Sound();
this.state = {
fontLoaded: false,
isPaused:false,
sound:sound,
currentSong:{},
isSongLoading:false,
songs:[],
duration:0,
position:0,
};
}
async componentWillMount() {
await Font.loadAsync({
'fira-regular': require("./assets/fonts/fira-sans/FiraSans-Regular.ttf"),
'fira-semibold': require("./assets/fonts/fira-sans/FiraSans-SemiBold.ttf")
});
let songs=await getAllSongs();
this.setState({
fontLoaded: true,
songs:songs,
});
}
async componentDidMount() {
await Audio.setAudioModeAsync({
allowsRecordingIOS: false,
interruptionModeIOS: Audio.INTERRUPTION_MODE_IOS_DO_NOT_MIX,
playsInSilentModeIOS: true,
shouldDuckAndroid: true,
interruptionModeAndroid: Audio.INTERRUPTION_MODE_ANDROID_DO_NOT_MIX,
playThroughEarpieceAndroid: false,
staysActiveInBackground: true
});
this.state.sound.setOnPlaybackStatusUpdate(this.updatePosition.bind(this));
}
render() {
console.log(Object.keys(this.state.currentSong).length !== 0);
console.log("Render Called");
if (this.state.fontLoaded) {
return (
<LinearGradient colors={[Colors.primaryGradientStart,Colors.primaryGradientEnd]}
start={[0,0]}
end={[1,1]}
style={styles.container}>
{Platform.OS === 'ios' && <StatusBar barStyle="light-content"/>}
<AppNavigator
screenProps={{
currentSong: this.state.currentSong,
isSongActive: (item)=> this.isSongActive(item),
isSongSelected: ()=> this.isSongSelected(),
playSong: async (song) => this.playSong(song),
nextSong :()=>this.nextSong(),
previousSong:()=>this.previousSong(),
duration: this.state.duration,
position: this.state.position,
seek: (positionInPercentage) => this.seek(positionInPercentage),
togglePause : async () => this.togglePause(),
isPaused: this.state.isPaused,
}}
/>
</LinearGradient>
);
}
return null;
}
isSongActive(item){
return (this.isSongSelected() && this.state.currentSong.id === item.id)
}
isSongSelected(){
return (Object.keys(this.state.currentSong).length !== 0)
}
async playSong(song){
console.log(Object.keys(this.state.currentSong).length !== 0);
let songLoaded = Object.keys(this.state.currentSong).length !== 0;
if(!this.state.isSongLoading &&
(!songLoaded || this.state.currentSong.id !== song.id )){
this.setState({
isSongLoading:true,
});
if(songLoaded){
await this.state.sound.unloadAsync();
}
console.log("Loading Song");
await this.state.sound.loadAsync({uri: song.location},{}, false);
console.log("Playing Song");
await this.state.sound.playAsync();
this.setState({
currentSong:song,
isSongLoading:false,
isPaused:false
});
}
}
nextSong(){
if(this.isSongSelected()){
let currentIndex = this.indexOfSong(this.state.currentSong);
let nextSong = this.state.songs[(currentIndex + 1) % this.state.songs.length];
this.playSong(nextSong);
}
}
previousSong(){
if(this.isSongSelected()){
let currentIndex = this.indexOfSong(this.state.currentSong);
let previousSong = this.state.songs[(currentIndex - 1) % this.state.songs.length];
this.playSong(previousSong);
}
}
async togglePause(){
console.log("Toggle Pause Called");
if(this.state.currentSong){
console.log("Going to pause current song: "+this.state.currentSong);
let isPaused = !this.state.isPaused;
if(isPaused){
console.log("Pausing song");
await this.state.sound.pauseAsync();
console.log("Paused");
}else{
console.log("Playing SOng");
await this.state.sound.playAsync();
console.log("Played");
}
this.setState({
isPaused:isPaused
});
}
}
indexOfSong(song){
for(let i=0;i<this.state.songs.length;i++){
if(song.id=== this.state.songs[i].id)
return i;
}
}
seek(positionInPercentage){
let positionInMillis = (positionInPercentage * this.state.duration) / 100;
this.state.sound.setPositionAsync(positionInMillis);
this.setState({
positionInMillis: positionInPercentage
});
}
updatePosition({durationMillis,positionMillis}){
if((typeof durationMillis !== "undefined" && (typeof positionMillis !== "undefined"))){
let positionInPercentage = (positionMillis/durationMillis) * 100;
console.log(`Duration in Millis: ${durationMillis}, position: ${positionInPercentage}, poisition millis : ${positionMillis}`);
this.setState({
duration:durationMillis,
position: positionInPercentage,
});
}
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
paddingTop:responsiveHeight(4)
},
});