Functioning music player features

Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
2026-04-29 03:20:20 +00:00
co-authored by Copilot
parent 357e6e17cc
commit 34476a56da
9 changed files with 161 additions and 98 deletions
+79 -83
View File
@@ -1,5 +1,7 @@
import 'package:flutter/material.dart';
import 'streaming_audio_business_object.dart';
void main() {
runApp(const MyApp());
}
@@ -7,116 +9,110 @@ void main() {
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
title: 'Split Screen Player',
theme: ThemeData(
// This is the theme of your application.
//
// TRY THIS: Try running your application with "flutter run". You'll see
// the application has a purple toolbar. Then, without quitting the app,
// try changing the seedColor in the colorScheme below to Colors.green
// and then invoke "hot reload" (save your changes or press the "hot
// reload" button in a Flutter-supported IDE, or press "r" if you used
// the command line to start the app).
//
// Notice that the counter didn't reset back to zero; the application
// state is not lost during the reload. To reset the state, use hot
// restart instead.
//
// This works for code too, not just values: Most code changes can be
// tested with just a hot reload.
colorScheme: .fromSeed(seedColor: Colors.deepPurple),
colorScheme: ColorScheme.fromSeed(seedColor: Colors.blueGrey),
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
home: const MainPage(),
debugShowCheckedModeBanner: false,
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final String title;
class MainPage extends StatefulWidget {
const MainPage({super.key});
@override
State<MyHomePage> createState() => _MyHomePageState();
State<MainPage> createState() => _MainPageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
class _MainPageState extends State<MainPage> {
final StreamingAudioBusinessObject _audio = StreamingAudioBusinessObject();
@override
void initState() {
super.initState();
_audio.addListener(_handleAudioStateChanged);
}
void _handleAudioStateChanged() {
if (!mounted) {
return;
}
void _incrementCounter() {
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
_counter++;
// Rebuild when playback state changes from the player.
});
}
Future<void> _play() async {
await _audio.play();
}
Future<void> _stop() async {
await _audio.pause();
}
@override
void dispose() {
_audio.removeListener(_handleAudioStateChanged);
_audio.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold(
appBar: AppBar(
// TRY THIS: Try changing the color here to a specific color (to
// Colors.amber, perhaps?) and trigger a hot reload to see the AppBar
// change color while the other colors stay the same.
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
body: SafeArea(
child: Column(
// Column is also a layout widget. It takes a list of children and
// arranges them vertically. By default, it sizes itself to fit its
// children horizontally, and tries to be as tall as its parent.
//
// Column has various properties to control how it sizes itself and
// how it positions its children. Here we use mainAxisAlignment to
// center the children vertically; the main axis here is the vertical
// axis because Columns are vertical (the cross axis would be
// horizontal).
//
// TRY THIS: Invoke "debug painting" (choose the "Toggle Debug Paint"
// action in the IDE, or press "p" in the console), to see the
// wireframe for each widget.
mainAxisAlignment: .center,
children: [
const Text('You have pushed the button this many times:'),
Text(
'$_counter',
style: Theme.of(context).textTheme.headlineMedium,
Expanded(
flex: 9,
child: Padding(
padding: const EdgeInsets.all(12),
child: Container(
width: double.infinity,
decoration: BoxDecoration(
border: Border.all(color: Colors.black26, width: 2),
borderRadius: BorderRadius.circular(8),
color: Colors.white,
),
),
),
),
Expanded(
flex: 1,
child: Container(
width: double.infinity,
padding: const EdgeInsets.symmetric(horizontal: 16),
color: Colors.black87,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: _audio.isPlaying ? null : _play,
child: const Text('Play'),
),
const SizedBox(width: 12),
ElevatedButton(
onPressed: _audio.isPlaying ? _stop : null,
child: const Text('Stop'),
),
const SizedBox(width: 16),
Text(
_audio.isPlaying ? 'Playing' : 'Stopped',
style: const TextStyle(color: Colors.white),
),
],
),
),
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
),
);
}
}
+48
View File
@@ -0,0 +1,48 @@
import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:just_audio/just_audio.dart';
class StreamingAudioBusinessObject extends ChangeNotifier {
StreamingAudioBusinessObject({
this.streamUrl = 'https://kryz.out.airtime.pro/kryz_a',
}) {
_playerStateSubscription = _player.playerStateStream.listen((state) {
final isCurrentlyPlaying =
state.playing && state.processingState != ProcessingState.completed;
if (_isPlaying != isCurrentlyPlaying) {
_isPlaying = isCurrentlyPlaying;
notifyListeners();
}
});
}
final String streamUrl;
final AudioPlayer _player = AudioPlayer();
late final StreamSubscription<PlayerState> _playerStateSubscription;
bool _isSourceLoaded = false;
bool _isPlaying = false;
bool get isPlaying => _isPlaying;
Future<void> play() async {
if (!_isSourceLoaded) {
await _player.setUrl(streamUrl);
_isSourceLoaded = true;
}
await _player.play();
}
Future<void> pause() async {
await _player.pause();
}
@override
void dispose() {
_playerStateSubscription.cancel();
_player.dispose();
super.dispose();
}
}