Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IOS audio overlaps!. #92

Open
franquicidad opened this issue Jan 10, 2023 · 1 comment
Open

IOS audio overlaps!. #92

franquicidad opened this issue Jan 10, 2023 · 1 comment

Comments

@franquicidad
Copy link

franquicidad commented Jan 10, 2023

Hello for your contribution ! great work! .Can you help me with this issue, I am using a GNav bottom nav bar the android app works fine and it is on production however the IOS app I click on the another tab on the bottom nav bar and the audio overlaps from the audio on the main Screen. I was thinking it was something on the state but if it works on android thennn .....

Here is my main code.

void main() {
runApp(const MyApp());
}

class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);

// This widget is the root of your application.
@OverRide
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
theme: ThemeData(
useMaterial3: true,
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Divina Voluntad'),
);
}
}

class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);

final String title;

@OverRide
State createState() => _MyHomePageState();
}

class _MyHomePageState extends State {
late PodPlayerController _controller;
int _currentIndex = 0;

@OverRide
void initState() {
_playvideo();
_getNameByIndex(_currentIndex);
super.initState();
}

@OverRide
void deactivate() {
_controller.pause();
super.deactivate();
}

@OverRide
void dispose() {
_controller.dispose();
super.dispose();
}

final screens = [PasionCristo(), NovenaNavidad(), LibroDelCielo()];
final colors = [Colors.blue, Colors.red, Colors.indigo];
final appText = ['Divina Voluntad', 'Novena de Navidad', 'Libro del cielo'];

@OverRide
Widget build(BuildContext context) {
return Scaffold(
bottomNavigationBar: Container(
decoration: BoxDecoration(),
child: Container(
padding: EdgeInsets.symmetric(horizontal: 10),
margin: EdgeInsets.only(bottom: 10),
child: GNav(
color: colors[_currentIndex],
tabBackgroundColor: colors[_currentIndex],
gap: 5,
textStyle: TextStyle(
fontSize: 18, color: Colors.white, fontWeight: FontWeight.w700),
selectedIndex: _currentIndex,
tabBorderRadius: 10,
padding: EdgeInsets.symmetric(vertical: 15, horizontal: 20),
onTabChange: (index) => {
setState(() => { _currentIndex = index})
},
tabs: [
GButton(
icon: Icons.add,
text: 'Pasion de Cristo',
iconActiveColor: Colors.white,
textColor: Colors.white,
),
GButton(
icon: Icons.ac_unit_outlined,
text: 'Novena Navidad',
iconActiveColor: Colors.white,
textColor: Colors.white,
),
GButton(
icon: Icons.cloud,
text: 'Libro del Cielo',
iconActiveColor: Colors.white,
textColor: Colors.white,
),
],
),
),
),
appBar: AppBar(
backgroundColor: colors[_currentIndex],
titleTextStyle: TextStyle(
color: Colors.white,
fontSize: 22,
fontWeight: FontWeight.w700,
),
title: Text(appText[_currentIndex]),
centerTitle: true,
),
body: screens[_currentIndex],
);
}

void _playvideo({
int index = 0,
bool init = false,
}) {
_controller = PodPlayerController(
playVideoFrom: PlayVideoFrom.youtube(pasion[index].url.toString()),
podPlayerConfig: const PodPlayerConfig(
autoPlay: true,
isLooping: false,
))
..initialise();
if (!init) {
_controller.pause();
}
}

String _getNameByIndex(int currentIndex) {
var methodCurrentIndex = currentIndex;
return pasion[methodCurrentIndex].name.toString();
}
}

I use this libraries

cupertino_icons: ^1.0.2
pod_player: ^0.1.0
google_fonts: ^3.0.1
google_nav_bar: ^5.0.6

@Lamia-Abdullah
Copy link

Lamia-Abdullah commented Jul 15, 2023

Use the “ didUpdateWidget ” method is a lifecycle method available in StatefulWidget.
Inside the method you have access to the (oldWidget) before it was updated, allowing you to compare the oldwidget with the current widget to Update the _playVideo();

  @override
  void didUpdateWidget(MyHomePage oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (oldWidget.id  != widget.id) {
      _playVideo(); // Update the video player when the widget updates

    }
  }

and Make sure to using a "ValueKey" with the video ID, the PlayVideo widget will be rebuilt whenever the video changes. This will ensure that the previous video is paused " before loading a new one ", preventing the audio overlap issue.

class ViewVideo extends StatelessWidget {
  const ViewVideo({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return PlayVideo(
      key: ValueKey(value.Id), // Use ValueKey to uniquely identify the PlayVideo widget
      id: value.Id,
    );
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants