Skip to content

Commit

Permalink
Version 0.1.0-nullsafety.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanheise committed Nov 28, 2020
1 parent 9636cbb commit 5b2ec16
Show file tree
Hide file tree
Showing 11 changed files with 281 additions and 362 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.1.0-nullsafety.0

* Support null safety.

## 0.0.9

* ARC fixes on iOS.
Expand Down
152 changes: 78 additions & 74 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'dart:math';
//import 'dart:math';

import 'package:audio_session/audio_session.dart';
import 'package:flutter/material.dart';
import 'package:just_audio/just_audio.dart' as ja;
//import 'package:just_audio/just_audio.dart' as ja;

void main() {
runApp(MyApp());
Expand All @@ -14,7 +14,7 @@ class MyApp extends StatefulWidget {
}

class _MyAppState extends State<MyApp> {
final _player = ja.AudioPlayer();
//final _player = ja.AudioPlayer();

@override
void initState() {
Expand All @@ -27,58 +27,61 @@ class _MyAppState extends State<MyApp> {
// Listen to audio interruptions and pause or duck as appropriate.
_handleInterruptions(audioSession);
// Use another plugin to load audio to play.
await _player.setUrl(
"https://s3.amazonaws.com/scifri-episodes/scifri20181123-episode.mp3");
//await _player.setUrl(
// "https://s3.amazonaws.com/scifri-episodes/scifri20181123-episode.mp3");
});
}

void _handleInterruptions(AudioSession audioSession) {
bool playInterrupted = false;
//bool playInterrupted = false;
audioSession.becomingNoisyEventStream.listen((_) {
_player.pause();
});
_player.playingStream.listen((playing) {
playInterrupted = false;
// Temporary as the just_audio 0.3.4 doesn't activate the audio session.
if (playing) {
audioSession.setActive(true);
}
print('PAUSE');
//_player.pause();
});
//_player.playingStream.listen((playing) {
// playInterrupted = false;
// // Temporary as the just_audio 0.3.4 doesn't activate the audio session.
// if (playing) {
// audioSession.setActive(true);
// }
//});
audioSession.interruptionEventStream.listen((event) {
if (event.begin) {
switch (event.type) {
case AudioInterruptionType.duck:
if (audioSession.androidAudioAttributes.usage ==
AndroidAudioUsage.game) {
_player.setVolume(_player.volume / 2);
}
playInterrupted = false;
break;
case AudioInterruptionType.pause:
case AudioInterruptionType.unknown:
if (_player.playing) {
_player.pause();
// Although pause is async and sets playInterrupted = false,
// this is done in the sync portion.
playInterrupted = true;
}
break;
}
} else {
switch (event.type) {
case AudioInterruptionType.duck:
_player.setVolume(min(1.0, _player.volume * 2));
playInterrupted = false;
break;
case AudioInterruptionType.pause:
if (playInterrupted) _player.play();
playInterrupted = false;
break;
case AudioInterruptionType.unknown:
playInterrupted = false;
break;
}
}
print('interruption begin: ${event.begin}');
print('interruption type: ${event.type}');
//if (event.begin) {
// switch (event.type) {
// case AudioInterruptionType.duck:
// if (audioSession.androidAudioAttributes.usage ==
// AndroidAudioUsage.game) {
// _player.setVolume(_player.volume / 2);
// }
// playInterrupted = false;
// break;
// case AudioInterruptionType.pause:
// case AudioInterruptionType.unknown:
// if (_player.playing) {
// _player.pause();
// // Although pause is async and sets playInterrupted = false,
// // this is done in the sync portion.
// playInterrupted = true;
// }
// break;
// }
//} else {
// switch (event.type) {
// case AudioInterruptionType.duck:
// _player.setVolume(min(1.0, _player.volume * 2));
// playInterrupted = false;
// break;
// case AudioInterruptionType.pause:
// if (playInterrupted) _player.play();
// playInterrupted = false;
// break;
// case AudioInterruptionType.unknown:
// playInterrupted = false;
// break;
// }
//}
});
}

Expand All @@ -90,32 +93,33 @@ class _MyAppState extends State<MyApp> {
title: const Text('audio_session example'),
),
body: Center(
child: StreamBuilder<ja.PlayerState>(
stream: _player.playerStateStream,
builder: (context, snapshot) {
final playerState = snapshot.data;
if (playerState?.processingState != ja.ProcessingState.ready) {
return Container(
margin: EdgeInsets.all(8.0),
width: 64.0,
height: 64.0,
child: CircularProgressIndicator(),
);
} else if (playerState?.playing == true) {
return IconButton(
icon: Icon(Icons.pause),
iconSize: 64.0,
onPressed: _player.pause,
);
} else {
return IconButton(
icon: Icon(Icons.play_arrow),
iconSize: 64.0,
onPressed: _player.play,
);
}
},
),
child: Placeholder(),
//child: StreamBuilder<ja.PlayerState>(
// stream: _player.playerStateStream,
// builder: (context, snapshot) {
// final playerState = snapshot.data;
// if (playerState?.processingState != ja.ProcessingState.ready) {
// return Container(
// margin: EdgeInsets.all(8.0),
// width: 64.0,
// height: 64.0,
// child: CircularProgressIndicator(),
// );
// } else if (playerState?.playing == true) {
// return IconButton(
// icon: Icon(Icons.pause),
// iconSize: 64.0,
// onPressed: _player.pause,
// );
// } else {
// return IconButton(
// icon: Icon(Icons.play_arrow),
// iconSize: 64.0,
// onPressed: _player.play,
// );
// }
// },
//),
),
),
);
Expand Down
Loading

0 comments on commit 5b2ec16

Please sign in to comment.