Notice (2018-05-24): bugzilla.xamarin.com is now in
Please join us on
Visual Studio Developer Community and in the
Mono organizations on
GitHub to continue tracking issues. Bugzilla will remain
available for reference in read-only mode. We will continue to work
on open Bugzilla bugs, copy them to the new locations
as needed for follow-up, and add the new items under Related
Our sincere thanks to everyone who has contributed on this bug
tracker over the years. Thanks also for your understanding as we
make these adjustments and improvements for the future.
Please create a new report on
Developer Community or GitHub with
your current version information, steps to reproduce, and relevant error
messages or log files if you are hitting an issue that looks similar to
this resolved bug and you do not yet see a matching new report.
I display a live camera preview in my Xamarin.iOS app using the AVFoundation infrastructure.
Using XCode Instruments, I see that my app uses about 10% of the 'Audio CPU Actvity' once the preview is displayed. Even if the preview is closed again, the app keeps on using the 'Audio CPU Activity'.
My app uses the camera only for short periods. However, because this activity remains high, I believe my app is draining the iPhone's battery (which is a complaint that I get from the users and which is the reason why I started investigating this).
To reproduce this:
1) Create an Xamarin.iOS app that displays a live camera preview.
Example 1 (Xamarin.iOS): https://github.com/pierceboggan/xamarin-blog-samples/tree/master/ios/avfoundation-camera/4.%20Completed%20Camera%20Control
Example 2 (Xamarin.Forms): https://github.com/xamarin/xamarin-forms-samples/tree/master/CustomRenderers/ContentPage
In both samples, you need to add this in info.plist (otherwise they will crash):
2) Deploy the app on an iPhone
3) Start XCode Instruments, select 'Energy Usage Log' and profile the app
4) You will see that, from the moment that the camera is opened, the app uses ~10% 'Audio CPU Activity'. If the camera closes, this remains the case.
After some further investigation, the issues seems to be caused by a bug in both sample projects that I've linked to.
It can be solved by adding this line in the Dispose() method of the view:
So no bug in Xamarin, but maybe the sample code should be updated?
I was actually commenting the same thing and got a mid air collision because of your last comment.
You/we could also call AVCaptureSession's `StopRunning()`.
I've seen people complain (against Apple) about the fact AVCaptureSession always record audio several times because they don't always care about the audio, but as far as I know there's no way around that.
Also example 1 has the camera always on so it's normal that Audio CPU Activity doesn't go back to 0.
Note: Audio CPU Activity stops when you quit the app.
That said I agree our samples are incorrect and I'll make sure we fix them.
Now I want to emphasize that Xamarin.iOS is a thin layer on top of the native iOS frameworks and what you experienced here was a native iOS behavior and was due to how the APIs were used.
The broader developer community would have been the best and quickest place for troubleshooting help on this issue.
Posting a question on Stack Overflow  or the Xamarin Forums  would have provided this help.
In case it might also be a helpful reference, you can find some general recommendations on the best places to ask various kinds of questions about Xamarin on: