admel_audio_ads 0.0.1
admel_audio_ads: ^0.0.1 copied to clipboard
A Flutter plugin for audio playback with banner display functionality
example/lib/main.dart
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:admel_audio_ads/admel_audio_ads.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final _pluginTestPlugin = AdmelAudioAds();
bool _isPlaying = false;
double _currentPosition = 0.0;
double _duration = 0.0;
double _progress = 0.0;
double _volume = 0.5;
// AutoRefresh related variables (now handled by native plugin)
bool _autoRefreshEnabled = true;
int _autoRefreshIntervalSeconds = 30;
// Plugin initialization state
bool _isPluginInitialized = false;
// Timer for updating audio status
Timer? _statusUpdateTimer;
@override
void initState() {
super.initState();
// Don't initialize plugin automatically
}
@override
void dispose() {
_statusUpdateTimer?.cancel();
super.dispose();
}
Future<void> initializePlugin() async {
try {
// Initialize plugin with default configuration
await _pluginTestPlugin.initializePlugin(
slotid: 'adslot01',
mediaid: 'media01',
position: 'top-left',
offsetX: 50.0,
offsetY: 50.0,
);
// Enable auto-refresh with default settings
await _pluginTestPlugin.setAutoRefresh(
enabled: _autoRefreshEnabled,
interval: _autoRefreshIntervalSeconds,
);
if (mounted) {
setState(() {
_isPluginInitialized = true;
});
// Start updating audio status
_startStatusUpdates();
}
} catch (e) {
print('Error initializing plugin: $e');
}
}
void _startStatusUpdates() {
// Cancel existing timer if any
_statusUpdateTimer?.cancel();
// Update status immediately
_updateAudioStatus();
// Set up periodic updates
_statusUpdateTimer =
Timer.periodic(const Duration(milliseconds: 500), (timer) {
if (!mounted || !_isPluginInitialized) {
timer.cancel();
return;
}
_updateAudioStatus();
});
}
Future<void> _updateAudioStatus() async {
if (!_isPluginInitialized) return;
try {
final isPlaying = await _pluginTestPlugin.isPlaying();
final currentPosition = await _pluginTestPlugin.getCurrentPosition();
final duration = await _pluginTestPlugin.getDuration();
final progress = await _pluginTestPlugin.getProgress();
final volume = await _pluginTestPlugin.getCurrentVolume();
if (mounted) {
setState(() {
_isPlaying = isPlaying;
_currentPosition = currentPosition;
_duration = duration;
_progress = progress;
_volume = volume;
});
}
} catch (e) {
print('Error updating audio status: $e');
}
}
void _toggleAutoRefresh() async {
setState(() {
_autoRefreshEnabled = !_autoRefreshEnabled;
});
print('Auto refresh toggled: $_autoRefreshEnabled');
// Update native plugin auto-refresh setting
await _pluginTestPlugin.setAutoRefresh(
enabled: _autoRefreshEnabled,
interval: _autoRefreshIntervalSeconds,
);
}
void _setAutoRefreshInterval(int intervalSeconds) async {
setState(() {
_autoRefreshIntervalSeconds = intervalSeconds;
});
// Update native plugin auto-refresh interval
await _pluginTestPlugin.setAutoRefresh(
enabled: _autoRefreshEnabled,
interval: _autoRefreshIntervalSeconds,
);
}
Future<void> stopAudioAndHideBanner() async {
try {
print('Manually stopping audio and hiding banner');
await _pluginTestPlugin.stopAudioAndHideBanner();
// Update status immediately after stopping
await _updateAudioStatus();
} catch (e) {
print('Error stopping audio: $e');
}
}
Future<void> setVolume(double volume) async {
try {
await _pluginTestPlugin.setVolume(volume);
} catch (e) {
print('Error setting volume: $e');
}
}
String formatTime(double seconds) {
final minutes = (seconds / 60).floor();
final remainingSeconds = (seconds % 60).floor();
return '${minutes.toString().padLeft(2, '0')}:${remainingSeconds.toString().padLeft(2, '0')}';
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin Test Example'),
backgroundColor: Colors.blue,
),
body: SingleChildScrollView(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Card(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Plugin Control',
style: Theme.of(context).textTheme.titleLarge,
),
const SizedBox(height: 16),
Row(
children: [
Icon(
_isPluginInitialized
? Icons.check_circle
: Icons.warning,
color: _isPluginInitialized
? Colors.green
: Colors.orange,
),
const SizedBox(width: 8),
Text(
_isPluginInitialized
? 'Plugin Initialized'
: 'Plugin Not Initialized',
style: TextStyle(
color: _isPluginInitialized
? Colors.green
: Colors.orange,
fontWeight: FontWeight.bold,
),
),
],
),
const SizedBox(height: 16),
ElevatedButton.icon(
onPressed:
_isPluginInitialized ? null : initializePlugin,
icon: const Icon(Icons.power_settings_new),
label: const Text('Initialize Plugin'),
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue,
foregroundColor: Colors.white,
minimumSize: const Size(double.infinity, 45),
),
),
if (!_isPluginInitialized) ...[
const SizedBox(height: 8),
const Text(
'Initialize the plugin before using other features',
style: TextStyle(fontSize: 12, color: Colors.grey),
),
],
],
),
),
),
const SizedBox(height: 16),
Card(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Audio Controls',
style: Theme.of(context).textTheme.titleLarge,
),
const SizedBox(height: 16),
ElevatedButton.icon(
onPressed: _isPlaying ? stopAudioAndHideBanner : null,
icon: const Icon(Icons.stop),
label: const Text('Stop Audio'),
style: ElevatedButton.styleFrom(
backgroundColor: Colors.red,
foregroundColor: Colors.white,
),
),
],
),
),
),
const SizedBox(height: 16),
Card(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Audio Status',
style: Theme.of(context).textTheme.titleLarge,
),
const SizedBox(height: 16),
Row(
children: [
Icon(
_isPlaying ? Icons.play_circle : Icons.pause_circle,
color: _isPlaying ? Colors.green : Colors.grey,
),
const SizedBox(width: 8),
Text(_isPlaying ? 'Playing' : 'Stopped'),
],
),
const SizedBox(height: 8),
if (_duration > 0) ...[
Text(
'Time: ${formatTime(_currentPosition)} / ${formatTime(_duration)}'),
const SizedBox(height: 8),
LinearProgressIndicator(
value: _progress,
backgroundColor: Colors.grey[300],
valueColor:
const AlwaysStoppedAnimation<Color>(Colors.blue),
),
],
],
),
),
),
const SizedBox(height: 16),
Card(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Volume Control',
style: Theme.of(context).textTheme.titleLarge,
),
const SizedBox(height: 16),
Row(
children: [
const Icon(Icons.volume_down),
Expanded(
child: Slider(
value: _volume,
min: 0.0,
max: 1.0,
divisions: 20,
label: '${(_volume * 100).round()}%',
onChanged: (value) {
setState(() {
_volume = value;
});
setVolume(value);
},
),
),
const Icon(Icons.volume_up),
],
),
Text('Current Volume: ${(_volume * 100).round()}%'),
],
),
),
),
const SizedBox(height: 16),
Card(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Auto Refresh Settings',
style: Theme.of(context).textTheme.titleLarge,
),
const SizedBox(height: 16),
Row(
children: [
Switch(
value: _autoRefreshEnabled,
onChanged: (value) => _toggleAutoRefresh(),
),
const SizedBox(width: 8),
Text(_autoRefreshEnabled
? 'Auto Refresh: ON'
: 'Auto Refresh: OFF'),
],
),
const SizedBox(height: 16),
Text(
'Auto Refresh Interval: ${_autoRefreshIntervalSeconds}s'),
Slider(
value: _autoRefreshIntervalSeconds.toDouble(),
min: 5,
max: 120,
divisions: 23,
label: '${_autoRefreshIntervalSeconds}s',
onChanged: (value) {
_setAutoRefreshInterval(value.round());
},
),
const SizedBox(height: 8),
],
),
),
),
const SizedBox(height: 16),
Card(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Instructions',
style: Theme.of(context).textTheme.titleLarge,
),
const SizedBox(height: 8),
const Text(
'1. Tap "Initialize Plugin" to set up the audio ads system\n'
'2. Once initialized, the plugin will automatically start playing ads\n'
'3. The banner will appear with a progress ring showing audio playback\n'
'4. Tap the banner to open the target URL\n'
'5. Set device volume to 0 to see the mute banner\n'
'6. Use the volume slider to control system volume\n'
'7. Toggle Auto Refresh to enable/disable automatic banner re-display\n'
'8. Adjust auto refresh interval (5s - 120s) to control banner re-display frequency',
style: TextStyle(fontSize: 14),
),
],
),
),
),
],
),
),
),
);
}
}