215 lines
6.8 KiB
Dart
215 lines
6.8 KiB
Dart
import 'dart:convert';
|
|
import 'dart:typed_data';
|
|
|
|
import 'package:audioplayers/audioplayers.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:manager_app/Models/managerContext.dart';
|
|
import 'package:manager_app/app_context.dart';
|
|
import 'package:manager_app/constants.dart';
|
|
import 'package:manager_api_new/api.dart';
|
|
import 'package:provider/provider.dart';
|
|
import 'package:http/http.dart' as http;
|
|
|
|
import 'loading_common.dart';
|
|
|
|
class AudioPlayerContainer extends StatefulWidget {
|
|
const AudioPlayerContainer({Key key, this.resourceDTO, this.isAuto}) : super(key: key);
|
|
|
|
final ResourceDTO resourceDTO;
|
|
final bool isAuto;
|
|
|
|
@override
|
|
State<AudioPlayerContainer> createState() => _AudioPlayerContainerState();
|
|
}
|
|
|
|
class _AudioPlayerContainerState extends State<AudioPlayerContainer> {
|
|
AudioPlayer player = AudioPlayer();
|
|
Uint8List audiobytes;
|
|
bool isplaying = false;
|
|
bool audioplayed = false;
|
|
int currentpos = 0;
|
|
int maxduration = 100;
|
|
String currentpostlabel = "00:00";
|
|
|
|
@override
|
|
void initState() {
|
|
//Uint8List base64String = base64Decode(audiobytes); // LOAD DATA
|
|
|
|
/*Future.delayed(Duration.zero, () async {
|
|
player.onDurationChanged.listen((Duration d) { //get the duration of audio
|
|
maxduration = d.inSeconds;
|
|
setState(() {
|
|
});
|
|
});
|
|
|
|
player.onAudioPositionChanged.listen((Duration p){
|
|
currentpos = p.inMilliseconds; //get the current position of playing audio
|
|
|
|
//generating the duration label
|
|
int shours = Duration(milliseconds:currentpos).inHours;
|
|
int sminutes = Duration(milliseconds:currentpos).inMinutes;
|
|
int sseconds = Duration(milliseconds:currentpos).inSeconds;
|
|
|
|
int rminutes = sminutes - (shours * 60);
|
|
int rseconds = sseconds - (sminutes * 60 + shours * 60 * 60);
|
|
|
|
String minutesToShow = rminutes < 10 ? '0$rminutes': rminutes.toString();
|
|
String secondsToShow = rseconds < 10 ? '0$rseconds': rseconds.toString();
|
|
|
|
currentpostlabel = "$minutesToShow:$secondsToShow";
|
|
|
|
setState(() {
|
|
//refresh the UI
|
|
});
|
|
});
|
|
|
|
if(widget.isAuto) {
|
|
player.playBytes(audiobytes);
|
|
setState(() {
|
|
isplaying = true;
|
|
audioplayed = true;
|
|
});
|
|
}
|
|
});*/
|
|
super.initState();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
/*player.stop();
|
|
player.dispose();*/
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final appContext = Provider.of<AppContext>(context);
|
|
Size size = MediaQuery.of(context).size;
|
|
|
|
return FutureBuilder(
|
|
future: getAudio(widget.resourceDTO.id, appContext),
|
|
builder: (context, AsyncSnapshot<dynamic> snapshot) {
|
|
if (snapshot.connectionState == ConnectionState.done) {
|
|
print("DOOOONE");
|
|
if(snapshot.data != null) {
|
|
print("snapshot.data");
|
|
print(snapshot.data);
|
|
//this.player.playBytes(audiobytes);
|
|
}
|
|
|
|
return Container(
|
|
child: Column(
|
|
children: [
|
|
|
|
Container(
|
|
child: Text(currentpostlabel, style: TextStyle(fontSize: 25),),
|
|
),
|
|
|
|
Container(
|
|
child: Wrap(
|
|
spacing: 10,
|
|
children: [
|
|
ElevatedButton.icon(
|
|
style: ElevatedButton.styleFrom(
|
|
primary: kPrimaryColor, // Background color
|
|
),
|
|
onPressed: () async {
|
|
print(isplaying);
|
|
print(audioplayed);
|
|
if(!isplaying && !audioplayed){
|
|
player.playBytes(audiobytes);
|
|
//player.playBytes(bytes)
|
|
setState(() {
|
|
isplaying = true;
|
|
audioplayed = true;
|
|
});
|
|
}else if(audioplayed && !isplaying){
|
|
player.resume();
|
|
setState(() {
|
|
isplaying = true;
|
|
audioplayed = true;
|
|
});
|
|
}else{
|
|
player.pause();
|
|
setState(() {
|
|
isplaying = false;
|
|
});
|
|
}
|
|
},
|
|
icon: Icon(isplaying?Icons.pause:Icons.play_arrow),
|
|
label:Text(isplaying?"Pause":"Play")
|
|
),
|
|
|
|
ElevatedButton.icon(
|
|
style: ElevatedButton.styleFrom(
|
|
primary: kPrimaryColor, // Background color
|
|
),
|
|
onPressed: () async {
|
|
/*player.stop();
|
|
setState(() {
|
|
isplaying = false;
|
|
audioplayed = false;
|
|
currentpostlabel = "00:00";
|
|
});*/
|
|
},
|
|
icon: Icon(Icons.stop),
|
|
label:Text("Stop")
|
|
),
|
|
],
|
|
),
|
|
)
|
|
],
|
|
),
|
|
);
|
|
}else if (snapshot.connectionState == ConnectionState.none) {
|
|
return Text("No data");
|
|
} else {
|
|
return Center(
|
|
child: Container(
|
|
height: size.height * 0.2,
|
|
child: LoadingCommon()
|
|
)
|
|
);
|
|
}
|
|
|
|
}
|
|
);
|
|
}
|
|
}
|
|
|
|
Future<Uint8List> getAudio(String resourceId, AppContext appContext) async {
|
|
try {
|
|
ManagerAppContext managerAppContext = appContext.getContext() as ManagerAppContext;
|
|
var url = managerAppContext.host + "/api/Resource/" + resourceId; // TO TEST TODO UPDATE ROUTE
|
|
print("DOWNLOAD AUDDDDIOOOOO ------------");
|
|
print(url);
|
|
//HttpClient client2 = HttpClient();
|
|
print("before.. hmmm? ");
|
|
var _downloadData = <int>[];
|
|
|
|
print("before.. ? ");
|
|
|
|
/*var test = await http.get(Uri.parse(url));
|
|
print("test");
|
|
print(test);*/
|
|
|
|
var test2 = await http.readBytes(Uri.parse(url));
|
|
print("test2");
|
|
print(test2);
|
|
|
|
//final HttpClientRequest request = await client2.getUrl(url);
|
|
print("RESPONSE");
|
|
/*HttpClientResponse response = await request.close();
|
|
await for(dynamic d in response) { _downloadData.addAll(d); }
|
|
print("AFTER");*/
|
|
final base64Str = base64.encode(test2);
|
|
Uint8List base64String = base64Decode(base64Str); // LOAD DATA
|
|
return base64String;
|
|
} catch (e) {
|
|
print(e);
|
|
print("IN CATCH");
|
|
return null;
|
|
}
|
|
}
|
|
|