flutter_mpmediaplayer 1.0.0
flutter_mpmediaplayer: ^1.0.0 copied to clipboard
A plugin to interface with MPMediaPlayer on iOS/iPadOS.
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_mpmediaplayer/flutter_mpmediaplayer.dart';
import 'src/library_tabs.dart';
import 'src/widgets.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'FlutterMPMediaPlayer Example',
theme: ThemeData(colorSchemeSeed: Colors.deepPurple),
home: const HomePage(),
);
}
}
/// Gates the library behind `authorize` and `authorizationStatus`.
class HomePage extends StatefulWidget {
const HomePage({super.key});
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
AuthorizationStatus? _status;
String? _error;
bool _busy = false;
bool _browsing = false;
@override
void initState() {
super.initState();
_refreshStatus();
}
Future<void> _refreshStatus() async {
setState(() {
_busy = true;
_error = null;
});
try {
final status = await FlutterMPMediaPlayer.authorizationStatus;
if (!mounted) return;
setState(() {
_status = status;
_busy = false;
_browsing = status == AuthorizationStatus.authorized;
});
} on PlatformException catch (e) {
if (!mounted) return;
setState(() {
_error = '${e.code}: ${e.message ?? 'no message'}';
_busy = false;
});
}
}
Future<void> _authorize() async {
setState(() {
_busy = true;
_error = null;
});
try {
final status = await FlutterMPMediaPlayer.authorize();
if (!mounted) return;
setState(() {
_status = status;
_busy = false;
_browsing = status == AuthorizationStatus.authorized;
});
} on PlatformException catch (e) {
if (!mounted) return;
setState(() {
_error = '${e.code}: ${e.message ?? 'no message'}';
_busy = false;
});
}
}
@override
Widget build(BuildContext context) {
if (_browsing) {
return LibraryTabs(onSignOut: () => setState(() => _browsing = false));
}
final authorized = _status == AuthorizationStatus.authorized;
return Scaffold(
appBar: AppBar(title: const Text('FlutterMPMediaPlayer Example App')),
body: Center(
child: Padding(
padding: const EdgeInsets.all(24),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const Icon(Icons.library_music, size: 64),
const SizedBox(height: 16),
Text('Authorization status: ${_status?.name ?? 'unknown'}'),
const SizedBox(height: 16),
// The progress bar takes the place of hiding the buttons, so the
// controls never jump around while a call is in flight.
SizedBox(
height: 4,
width: 200,
child: _busy ? const LinearProgressIndicator() : null,
),
const SizedBox(height: 16),
FilledButton(
onPressed: _busy ? null : _authorize,
child: const Text('Authorize'),
),
const SizedBox(height: 8),
OutlinedButton(
onPressed: _busy ? null : _refreshStatus,
child: const Text('Refresh status'),
),
if (authorized) ...[
const SizedBox(height: 8),
TextButton(
onPressed: () => setState(() => _browsing = true),
child: const Text('Browse library'),
),
],
if (_error != null) ...[
const SizedBox(height: 16),
ErrorView(message: _error!, onRetry: _refreshStatus),
],
],
),
),
),
);
}
}