Skip to content

Commit

Permalink
now we contruct player logic once then pass it who needs it, it fixes…
Browse files Browse the repository at this point in the history
… the audio control problem
  • Loading branch information
raysummee committed Jul 3, 2020
1 parent b974851 commit 871d694
Show file tree
Hide file tree
Showing 14 changed files with 373 additions and 273 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public void run(){
}
long time = exoPlayer.getCurrentPosition();
channel.invokeMethod("audio.onCurrentPosition", time);
handler.postDelayed(this, 200);
handler.postDelayed(this, 20);
}
catch (Exception e) {
Log.w("ID", "When running handler", e);
Expand Down
12 changes: 12 additions & 0 deletions lib/logic/models/modelMiniplayer.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import 'package:flutter/material.dart';

class ModelMiniPlayer extends ChangeNotifier{
/* bool _miniPlayerShowing=true;
bool get miniPlayerShowing=>_miniPlayerShowing;
set miniPlayerShowing(bool isShowing){
_miniPlayerShowing = isShowing;
notifyListeners();
}*/
}
24 changes: 14 additions & 10 deletions lib/logic/playerLogic.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,22 @@ class PlayerLogic{
});
}

void nextSong(List<SongInfo> songinfos) async{
int pos = await getPlaylistPosition();
print("current playlist $pos");
setPlaylistPostion(pos + 1);
print("setting new playlist pos ");
playMusic(songinfos.elementAt(await getPlaylistPosition()).uri);
print("nextsong ${await getPlaylistPosition()}");
void nextSong(List<SongInfo> songinfos, int currentpos) async{
if(currentpos<songinfos.length){
playMusic(songinfos.elementAt(++currentpos).uri);
print("nextsong ${++currentpos}");
}else{
print("no more songs");
}
}

void prevSong(List<SongInfo> songinfos) async{
setPlaylistPostion((await getPlaylistPosition()) - 1);
playMusic(songinfos.elementAt(await getPlaylistPosition()).uri);
void prevSong(List<SongInfo> songinfos, int currentpos) async{
if(currentpos>0){
playMusic(songinfos.elementAt(--currentpos).uri);
print("nextsong ${--currentpos}");
}else{
print("no more songs");
}
}

Stream<PlayerState> get onPlayerStateChanged => _playerStateController.stream;
Expand Down
5 changes: 3 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:Raylex/logic/models/modelMiniplayer.dart';
import 'package:Raylex/logic/models/playerStateNotify.dart';
import 'package:Raylex/logic/models/playlistPosition.dart';
import 'package:Raylex/ux/pages/navPages.dart';
Expand All @@ -8,8 +9,7 @@ import 'package:provider/provider.dart';
void main(){
WidgetsFlutterBinding.ensureInitialized();
SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.light.copyWith(
systemNavigationBarColor: Colors.lightBlue,
statusBarColor: Colors.blue
systemNavigationBarColor: Colors.lightBlue.shade600.withAlpha(210),
));
runApp(Launch());
}
Expand All @@ -21,6 +21,7 @@ class Launch extends StatelessWidget {
providers: [
ChangeNotifierProvider<PlayerStateNotify>(create: (_)=>PlayerStateNotify()),
ChangeNotifierProvider<PlaylistPosition>(create: (_)=>PlaylistPosition()),
ChangeNotifierProvider<ModelMiniPlayer>(create: (_)=>ModelMiniPlayer()),
],
child: MaterialApp(
routes: {
Expand Down
188 changes: 27 additions & 161 deletions lib/ux/components/appBars/libraryAppBar.dart
Original file line number Diff line number Diff line change
@@ -1,176 +1,42 @@
import 'dart:async';
import 'package:Raylex/logic/models/playerStateNotify.dart';
import 'package:Raylex/logic/models/playlistPosition.dart';
import 'package:Raylex/logic/models/songInfo.dart';
import 'package:Raylex/logic/playerLogic.dart';
import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class LibraryAppBar extends StatefulWidget {
@override
_LibraryAppBarState createState() => _LibraryAppBarState();
}

class _LibraryAppBarState extends State<LibraryAppBar> with TickerProviderStateMixin {
AnimationController __animationController;
StreamSubscription __subscriptionPlayerStateChanged;
PlayerLogic _playerLogic;
bool isPlaying=false;
bool isActive=true;
PlayerStateNotify appstate;
//PlaylistPosition appstatepos;

@override
void didChangeDependencies(){
super.didChangeDependencies();
if(appstate==null)
appstate=Provider.of<PlayerStateNotify>(context);
//if(appstatepos==null)
//appstatepos = Provider.of<PlaylistPosition>(context);
//_playerLogic.getPlaylistPosition().then((value) => appstatepos.index = value);
print("didchangeddependencies library");
if(__subscriptionPlayerStateChanged!=null){
print("canceled onstatechangedSecondary");
__subscriptionPlayerStateChanged.cancel();
__subscriptionPlayerStateChanged = null;
}else{
isActive = true;
_playerLogic.setMethodCallHandler();
print("creating onstateSecondary");
__subscriptionPlayerStateChanged = _playerLogic.onPlayerSecondaryStateChanged.handleError((e)=>print("streamerror $e")).listen((state) {
print("onStateChangedSecondary");

if(state == true){
setState(() {
isPlaying = true;
});
__animationController.forward();
}
if(state == false){
setState(() {
isPlaying = false;
});
__animationController.reverse();
}

});
}
_playerLogic.onInstanceIsPlaying().then((isP) {
setState(() {
isPlaying = isP;
});
if(isP){
__animationController.forward(from: __animationController.upperBound);
}else{
__animationController.reverse(from: __animationController.lowerBound);
}
});

}
class LibraryAppBar extends StatelessWidget{

@override
void initState(){
super.initState();
print("library init");
_playerLogic = PlayerLogic();
__animationController = AnimationController(
vsync: this,
duration: Duration(milliseconds: 300),
);
}


@override
void dispose(){
super.dispose();
print("library dispose");
__animationController.dispose();
__subscriptionPlayerStateChanged.cancel();
//appstate.dispose();
}
@override
Widget build(BuildContext context) {
return AppBar(
backgroundColor: Colors.lightBlue,
flexibleSpace: SafeArea(
return SafeArea(
bottom: false,
child: Container(
margin: EdgeInsets.fromLTRB(20, 20, 20, 20),
alignment: Alignment.bottomLeft,
height: 200,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Container(
margin: EdgeInsets.only(bottom: 2),
child: Text(
"Library",
style: TextStyle(
color: Colors.white,
fontSize: 27,
fontWeight: FontWeight.bold
)
),
),
Row(
top: false,
child: ClipRRect(
child: BackdropFilter(
filter: ImageFilter.blur(sigmaX: 10.0, sigmaY: 10.0),
child: Container(
color: Colors.lightBlue.withAlpha(200),
alignment: Alignment.bottomLeft,
height: 100,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
IconButton(
icon: Icon(
Icons.fast_rewind,
color: Colors.white,
),
onPressed: (){
if(appstate.songinfos!=null && appstate.songinfos.isNotEmpty){
_playerLogic.prevSong(appstate.songinfos);
//appstatepos.index=appstatepos.index-1;
}
else
print("error prev");
}
),
IconButton(
iconSize: 35,
icon: AnimatedIcon(
icon: AnimatedIcons.play_pause,
progress: __animationController,
color: Colors.white,
),
onPressed: (){
if(isPlaying){
setState(() {
isPlaying = false;
});
_playerLogic.pauseMusic();
__animationController.reverse();
}
else{
setState(() {
isPlaying = true;
});
_playerLogic.playPausedMusic();
//__animationController.forward();
}
}
),
IconButton(
icon: Icon(
Icons.fast_forward,color: Colors.white,
Container(
margin: EdgeInsets.fromLTRB(20, 20, 20, 20),
child: Text(
"Library",
style: TextStyle(
color: Colors.white,
fontSize: 27,
fontWeight: FontWeight.bold
)
),
onPressed: (){
if(appstate.songinfos!=null && appstate.songinfos.isNotEmpty){
_playerLogic.nextSong(appstate.songinfos);
//appstatepos.index = appstatepos.index + 1;
}
else
print("error next");
}
),
],
)
],
)
]
)
),
),
)
),
);
}
}
4 changes: 3 additions & 1 deletion lib/ux/components/appBars/playerAppBar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ class PlayerAppBar extends StatelessWidget {
alignment: Alignment.centerLeft,
child: IconButton(
icon: Icon(Icons.arrow_back_ios),
onPressed: (){Navigator.pop(context);}
onPressed: (){
Navigator.of(context).pop();
}
),
)
),
Expand Down
Loading

0 comments on commit 871d694

Please sign in to comment.