AdWidget stops camera preview on Android
See original GitHub issueThis plugin is currently useless due to this issue sadly if you want to show banner ads in a camera app.
Plugin Version
0.13.1
Steps to Reproduce
- Run
flutter create bug
. - Add
camera
andgoogle_mobile_ads
to pubspec.yaml and runflutter pub get
. - Add the
<meta-data ... />
tag to AndroidManifest.xml, following the instruction written in the README of this package. - Paste the code below into main.dart and launch the app on an Android device by
flutter run
.
Code
import 'package:flutter/material.dart';
import 'package:camera/camera.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart';
late final List<CameraDescription> cameras;
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
cameras = await availableCameras();
runApp(const App());
}
class App extends StatefulWidget {
const App();
@override
AppState createState() => AppState();
}
class AppState extends State<App> {
late final CameraController _controller;
late final BannerAd _bannerAd;
var _isAdReady = false;
@override
void initState() {
super.initState();
_controller = CameraController(cameras[0], ResolutionPreset.max)
..initialize().then((_) {
if (mounted) {
setState(() {});
}
});
_loadBannerAd();
}
@override
void dispose() {
_controller.dispose();
_bannerAd.dispose();
super.dispose();
}
Future<void> _loadBannerAd() async {
await MobileAds.instance.initialize();
_bannerAd = BannerAd(
size: AdSize.banner,
request: const AdRequest(nonPersonalizedAds: false),
adUnitId: 'ca-app-pub-3940256099942544/6300978111',
listener: BannerAdListener(
onAdLoaded: (ad) {
setState(() => _isAdReady = true);
},
onAdFailedToLoad: (ad, _) {
_isAdReady = false;
ad.dispose();
},
),
)..load();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Stack(
children: [
if (_controller.value.isInitialized) CameraPreview(_controller),
if (_isAdReady)
Align(
alignment: Alignment.bottomCenter,
child: SizedBox(
width: double.infinity,
height: _bannerAd.size.height.toDouble(),
child: AdWidget(ad: _bannerAd),
),
),
],
),
),
);
}
}
Expected results:
The camera preview is not affected, whether a banner ad is being displayed or not.
Actual results:
The camera preview stops when a banner ad is shown. It starts again when the ad is removed.
Logs
flutter analyze
Analyzing ads_bug...
No issues found! (ran in 2.3s)
flutter doctor -v
[√] Flutter (Channel stable, 2.2.2, on Microsoft Windows [Version 10.0.19042.1055], locale ja-JP)
• Flutter version 2.2.2 at D:\tool\dev\flutter
• Framework revision d79295af24 (13 days ago), 2021-06-11 08:56:01 -0700
• Engine revision 91c9fc8fe0
• Dart version 2.13.3
[√] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
• Android SDK at D:\tool\dev\Android\sdk
• Platform android-30, build-tools 30.0.3
• ANDROID_SDK_ROOT = D:\tool\dev\Android\sdk
• Java binary at: D:\tool\dev\java\jdk8u292-b10\bin\java
• Java version OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_292-b10)
• All Android licenses accepted.
[√] Chrome - develop for the web
• Chrome at C:\Program Files (x86)\Google\Chrome\Application\chrome.exe
[√] Visual Studio - develop for Windows (Visual Studio Community 2019 16.7.4)
• Visual Studio at D:\tool\dev\Microsoft\Microsoft Visual Studio\2019\Community
• Visual Studio Community 2019 version 16.7.30517.126
• Windows 10 SDK version 10.0.18362.0
[!] Android Studio (not installed)
• Android Studio not found; download from https://developer.android.com/studio/index.html
(or visit https://flutter.dev/docs/get-started/install/windows#android-setup for detailed instructions).
[√] IntelliJ IDEA Ultimate Edition (version 2021.1)
• IntelliJ at D:\tool\dev\JetBrains\apps\IDEA-U\ch-0\211.6693.111
• Flutter plugin version 57.0.5
• Dart plugin version 211.7233
[√] VS Code, 64-bit edition (version 1.56.2)
• VS Code at C:\Program Files\Microsoft VS Code
• Flutter extension version 3.23.0
[√] Connected device (3 available)
• Windows (desktop) • windows • windows-x64 • Microsoft Windows [Version 10.0.19042.1055]
• Chrome (web) • chrome • web-javascript • Google Chrome 91.0.4472.114
• Edge (web) • edge • web-javascript • Microsoft Edge 91.0.864.54
! Doctor found issues in 1 category.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:7
Top Results From Across the Web
Adwidget google_mobbile_ads flutter banner hangs camera ...
I am able to display the ad successfully, but the camera completely hangs(in emulator, oneplus5, samsung m31). In the emulator, I am unable...
Read more >Camera preview | Android Developers
Multi-window mode constrains camera apps to a portion of the screen, scaling the camera preview regardless of device orientation. Multi-display ...
Read more >Add widgets on iPhone - Apple Support
To view widgets, swipe right from the left edge of the Home Screen or the Lock Screen, then scroll up ... When you...
Read more >How to Use Google Photos Memories Widget on Android and ...
Google Photos memories widget lets you see old photos on your phone's home screen. Here's how to use the widget on Android and...
Read more >Not able to see widgets on OnePlus 8t after updating to ...
After updating my op8t to Android 12 based stable oxygen os I'm not able to find ... Im also facing this issue.. but...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi @kaboc, Thanks for filing the issue I am able to replicate the issue using the latest ads plugin
0.13.4
when the ad is added in the widget tree the camera freezes with a black screen and the ad loads, And removing the ads the app camera works fine.Output
code sample
flutter doctor -v
This issue is potentially a duplicate of https://github.com/googleads/googleads-mobile-flutter/issues/32
This issue is gone if the plugin is replaced with a forked one written in another issue page. It appears that the fork uses
AndroidView
instead ofPlatformViewLink
.