Skip to content

Commit

Permalink
audio
Browse files Browse the repository at this point in the history
  • Loading branch information
salma basem committed Aug 31, 2022
1 parent 8d6c805 commit be9af47
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 13 deletions.
Binary file added assets/audio/backgroundAudio.mp3
Binary file not shown.
Binary file added assets/audio/gameover.wav
Binary file not shown.
Binary file added assets/audio/hit.mp3
Binary file not shown.
Binary file added assets/audio/point.mp3
Binary file not shown.
15 changes: 15 additions & 0 deletions lib/Constant/constant.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,21 @@ bool isPlaying = false;
Duration duration = Duration.zero;
Duration position = Duration.zero;


/////////////////// GameOver audio
final audio2 = AudioPlayer();
bool isPlaying2 = false;
Duration duration2 = Duration.zero;
Duration position2 = Duration.zero;



//////////////////// Point audio
final audio3 = AudioPlayer();
bool isPlaying3 = false;
Duration duration3 = Duration.zero;
Duration position3 = Duration.zero;

/// to decide which page that class [Button] will navigate
enum Navigation{ home, settings, leaderBord }

Expand Down
93 changes: 85 additions & 8 deletions lib/Layouts/Pages/page_home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ class HomePage extends StatefulWidget {
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {

@override
void initState(){
super.initState();

setAudio();

audioPlayer.onPlayerStateChanged.listen((state) {
Expand All @@ -41,26 +41,79 @@ class _HomePageState extends State<HomePage> {
});
});

//////////////////// audio2
setAudio2();
audio2.onAudioPositionChanged.listen((state) {
setState(() {
isPlaying2 = state == PlayerState.PLAYING;
});
});

audio2.onDurationChanged.listen((newDuration) {
setState(() {
duration2 = newDuration;
});
});

audio2.onAudioPositionChanged.listen((newPosition) {
setState(() {
position2 = newPosition ;
});
});

///////////////
//////////////////// audio3
setAudio3();
audio3.onAudioPositionChanged.listen((state) {
setState(() {
isPlaying3 = state == PlayerState.PLAYING;
});
});

audio3.onDurationChanged.listen((newDuration) {
setState(() {
duration3 = newDuration;
});
});

audio3.onAudioPositionChanged.listen((newPosition) {
setState(() {
position3 = newPosition ;
});
});
}

Future setAudio() async{
audioPlayer.setReleaseMode(ReleaseMode.LOOP);

// String url ='https://www.youtube.com/watch?v=qCQOrHxktcA';
//audioPlayer.setUrl(url);
final player = AudioCache(prefix: 'assets/audio/');
final url = await player.load('Bones.mp3');
final url = await player.load('backgroundAudio.mp3');

audioPlayer.setUrl(url.path,isLocal: true);
}
Future setAudio2() async{
audio2.setReleaseMode(ReleaseMode.RELEASE);
final p2 = AudioCache(prefix: 'assets/audio/');
final url2 = await p2.load('gameover.wav');

audio2.setUrl(url2.path,isLocal: true);
}

Future setAudio3() async{
audio3.setReleaseMode(ReleaseMode.RELEASE);
final p3 = AudioCache(prefix: 'assets/audio/');
final url3 = await p3.load('point.mp3');

audio3.setUrl(url3.path,isLocal: true);
}

@override

void dispose(){
audioPlayer.dispose();
audio2.dispose();
audio3.dispose();
super.dispose();

}


@override
Widget build(BuildContext context) {
Expand Down Expand Up @@ -120,6 +173,8 @@ class _HomePageState extends State<HomePage> {
time = 0;
initialHeight = yAxis;
});


}

//Start Game Function:
Expand All @@ -130,6 +185,7 @@ class _HomePageState extends State<HomePage> {
height = gravity * time * time + velocity * time;
setState(() {
yAxis = initialHeight - height;

});
/* < Barriers Movements > */
setState(() {
Expand All @@ -147,8 +203,10 @@ class _HomePageState extends State<HomePage> {
}
});
if (birdIsDead()) {

timer.cancel();
_showDialog();

}
time += 0.032;
});
Expand All @@ -160,6 +218,10 @@ class _HomePageState extends State<HomePage> {
} else {
setState(() {
if (SCORE == TOP_SCORE) {
if (isPlaying3 == false)
{
audio3.resume();
}
TOP_SCORE++;
// TODO: add the Top score to Database
write(1, TOP_SCORE);
Expand All @@ -175,6 +237,11 @@ class _HomePageState extends State<HomePage> {
bool birdIsDead() {
// Screen
if (yAxis > 1.26 || yAxis < -1.1) {
/// Game Over Audio Plays in case bird dies
if (isPlaying2 == false)
{
audio2.resume();
}
return true;
}

Expand All @@ -184,6 +251,10 @@ class _HomePageState extends State<HomePage> {
(barrierX[i] + (barrierWidth)) >= birdWidth &&
(yAxis <= -1 + barrierHeight[i][0] ||
yAxis + birdHeight >= 1 - barrierHeight[i][1])) {
if (isPlaying2== false)
{
audio2.resume();
}
return true;
}
}
Expand All @@ -205,6 +276,7 @@ class _HomePageState extends State<HomePage> {

// TODO: Alert Dialog with 2 options (try again, exit)
void _showDialog() {

showDialog(
context: context,
builder: (context) {
Expand All @@ -218,8 +290,10 @@ class _HomePageState extends State<HomePage> {
),
actionsPadding: EdgeInsets.only(right: 8, bottom: 8),
content: Container(

child: Lottie.asset("assets/pics/loss.json",
fit: BoxFit.cover),

),
actions: [
ElevatedButton(
Expand All @@ -228,7 +302,10 @@ class _HomePageState extends State<HomePage> {
),
child: Text("Exit",
style: TextStyle(color: Colors.white, fontSize: 17)),
onPressed: (){
onPressed: () async {
if (isPlaying == true) {
await audioPlayer.pause();
}
resetGame();
Navigator.push(context, MaterialPageRoute(builder: (context) => StartScreen(),));
},
Expand Down
64 changes: 60 additions & 4 deletions lib/Layouts/Pages/page_settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:flappy_bird/Database/database.dart';
import 'package:flappy_bird/Layouts/Pages/page_start_screen.dart';
import 'package:flutter/material.dart';
import '../../Constant/constant.dart';
import 'package:audioplayers/audioplayers.dart';

class Settings extends StatefulWidget {
const Settings({Key? key}) : super(key: key);
Expand All @@ -12,6 +13,48 @@ class Settings extends StatefulWidget {
State<Settings> createState() => _SettingsState();
}
class _SettingsState extends State<Settings> {

@override
void initState(){
super.initState();

setAudio();

audioPlayer.onPlayerStateChanged.listen((state) {
setState(() {
isPlaying = state == PlayerState.PLAYING;
});
});

audioPlayer.onDurationChanged.listen((newDuration) {
setState(() {
duration = newDuration;
});
});

audioPlayer.onAudioPositionChanged.listen((newPosition) {
setState(() {
position = newPosition ;
});
});

}

Future setAudio() async{
audioPlayer.setReleaseMode(ReleaseMode.LOOP);
final player = AudioCache(prefix: 'assets/audio/');
final url = await player.load('backgroundAudio.mp3');

audioPlayer.setUrl(url.path,isLocal: true);
}


@override

void dispose(){
audioPlayer.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
Expand Down Expand Up @@ -84,11 +127,21 @@ class _SettingsState extends State<Settings> {
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
GestureDetector(onTap: (){
//TODO: play Music
GestureDetector(onTap: () async
{
//TODO: play Music
if (isPlaying == false)
{
await audioPlayer.resume();
}

},child: Icon(Icons.music_note_rounded,size: 40,)),
GestureDetector(onTap: (){
GestureDetector(onTap: () async
{
//TODO: play Music
if (isPlaying == true) {
await audioPlayer.pause();
}
},child: Icon(Icons.music_off_rounded,size: 40)),
],
),
Expand Down Expand Up @@ -120,7 +173,10 @@ class _SettingsState extends State<Settings> {
style: ElevatedButton.styleFrom(
primary: Colors.cyan.shade300,
),
onPressed: (){
onPressed: () async {
if (isPlaying == true) {
await audioPlayer.pause();
}
writeBackground(0, im);
Navigator.push(context, MaterialPageRoute(builder: (context) => StartScreen(),),);
}, child: Text("Apply",style: TextStyle(fontFamily: "Magic4",fontSize: 30),) ),
Expand Down
9 changes: 8 additions & 1 deletion lib/Layouts/Widgets/widget_Button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,14 @@ class Button extends StatelessWidget {
alignment: Alignment.center,
child: icon
),
onTap: (){
onTap: ()async{
if (isPlaying == false)
{
await audioPlayer.resume();
}
else{
await audioPlayer.pause();
}
if(type == Navigation.home.name){
Navigator.push(
context,
Expand Down

0 comments on commit be9af47

Please sign in to comment.