-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.dart
156 lines (141 loc) · 4.5 KB
/
main.dart
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
import 'package:flutter/material.dart';
import 'package:flutter_audio_output/flutter_audio_output.dart';
import 'dart:async';
import 'package:logger/logger.dart';
import 'package:just_audio/just_audio.dart';
void main() => runApp(const MyApp());
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
late AudioPlayer _audioPlayer;
final logger = Logger();
late AudioInput _currentInput = const AudioInput("unknown", 0);
late List<AudioInput> _availableInputs = [];
bool isSpeaker = true;
@override
void initState() {
_audioPlayer = AudioPlayer();
_audioPlayer
.setAudioSource(AudioSource.uri(
Uri.parse('asset:///assets/Sunflower.mp3'),
))
.catchError((error) {
print("An error occured $error");
});
_audioPlayer.play();
init();
super.initState();
}
Future<void> init() async {
FlutterAudioOutput.setListener(() async {
logger.d("-----changed-------");
await _getInput();
setState(() {});
});
await _getInput();
if (!mounted) return;
setState(() {});
}
_getInput() async {
_currentInput = await FlutterAudioOutput.getCurrentOutput();
logger.w("current:$_currentInput");
_availableInputs = await FlutterAudioOutput.getAvailableInputs();
logger.w("available $_availableInputs");
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Padding(
padding: const EdgeInsets.all(10),
child: Column(
children: <Widget>[
Text(
"current output:${_currentInput.name} ${_currentInput.port}",
),
Divider(),
Expanded(
child: ListView.builder(
itemBuilder: (_, index) {
AudioInput input = _availableInputs[index];
return Row(
children: <Widget>[
Expanded(child: Text(input.name)),
Expanded(child: Text("${input.port}")),
],
);
},
itemCount: _availableInputs.length,
),
),
Container(
height: 400,
child: Row(
children: [
IconButton(
onPressed: () {
_audioPlayer.pause();
},
icon: Icon(Icons.pause_circle),
),
IconButton(
onPressed: () {
_audioPlayer.play();
},
icon: Icon(Icons.play_circle),
),
IconButton(
onPressed: () async {
await _getInput();
if (_currentInput.port == AudioPort.speaker) {
isSpeaker =
await FlutterAudioOutput.changeToReceiver();
setState(() {
isSpeaker = false;
});
print("change to Receiver :$isSpeaker");
} else {
isSpeaker =
await FlutterAudioOutput.changeToSpeaker();
setState(() {
isSpeaker = true;
});
print("change to Speaker :$isSpeaker");
}
},
icon: isSpeaker == false
? const Icon(
Icons.volume_off,
color: Colors.black,
)
: const Icon(
Icons.volume_up,
color: Colors.black,
),
),
],
),
)
],
),
),
),
);
}
}