Bug 27109 - Calls to Record return false on device but work on the simulator.
Summary: Calls to Record return false on device but work on the simulator.
Status: RESOLVED ANSWERED
Alias: None
Product: iOS
Classification: Xamarin
Component: General ()
Version: master
Hardware: Macintosh Mac OS
: Normal normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2015-02-16 13:40 UTC by Andy
Modified: 2015-02-17 12:06 UTC (History)
4 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and 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 Links.

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.

Related Links:
Status:
RESOLVED ANSWERED

Description Andy 2015-02-16 13:40:35 UTC
I'm using a Shared Project for iOS that is going to Record audio. It works on my iOS 8.1 simulator, but on the device, .Record always returns false. I have microphone access permission. I've done everything in my power to get it to record.

[assembly: Xamarin.Forms.Dependency (typeof (AudioController))]

namespace AudioLooper.iOS
{
	public class AudioController : IAudioController
	{
		private AVAudioRecorder recorder;
		private AVAudioPlayer player;

		private NSUrl url;
		private NSError error;

		public AudioController ()
		{
			Prepare ();
		}

		public bool Prepare()
		{
			//Declare string for application temp path and tack on the file extension
			const string fileName = "MyRecording.wav";
			string tmpDirPath = Path.GetTempPath ();
			string audioDirPath = tmpDirPath + "audio";
			string filePath = audioDirPath + "/" + fileName;
			url = new NSUrl (filePath);

			try
			{
				if (Directory.Exists (tmpDirPath) == true)
				{
					Directory.CreateDirectory (audioDirPath);
				}

				if(File.Exists(filePath) == false)
				{
					File.Create(filePath);
				}
			}
			catch(Exception ex)
			{
				Debug.WriteLine (ex.Message + ex.StackTrace);
			}

			Console.WriteLine("Audio File url: " + url);

			AudioSettings audioSettings = new AudioSettings
			{
				SampleRate = 44100.0,
				Format = AudioToolbox.AudioFormatType.LinearPCM,
				NumberChannels = 2,
				LinearPcmBitDepth = 16,
			};

			//Set recorder parameters
			recorder = AVAudioRecorder.Create(url, audioSettings, out error);
			if (error != null)
			{
				Debug.WriteLine (error.Code);
			}

			if (recorder == null)
			{
				Debug.WriteLine ("Recorder is null");
			}

			recorder.EncoderError += (object sender, AVErrorEventArgs e) => 
			{
				Debug.WriteLine ("Encoder Error:" + e.Error.ToString());
			};

			recorder.BeginInterruption += (object sender, EventArgs e) => 
			{
				Debug.WriteLine ("Interruption:" + e.ToString());
			};

			recorder.EndInterruption += (object sender, EventArgs e) => 
			{
				Debug.WriteLine ("End Interruption:" + e.ToString());
			};

			recorder.FinishedRecording += (object sender, AVStatusEventArgs e) => 
			{
				Debug.WriteLine("Finished Recording:" + e.Status.ToString());
			};

			//Set Recorder to Prepare To Record
			bool prepared = recorder.PrepareToRecord();
			if (prepared == false)
			{
				Debug.WriteLine ("Recorder failed to prepare");
			}

			return prepared;
		}

		public bool StartRecording()
		{
			if (player != null)
			{
				player.Stop ();
				player.Dispose ();
			}

			if (recorder == null)
			{
				Debug.WriteLine ("Must call prepare first");
				return false;
			}

			//Set Recorder to Prepare To Record
			bool prepared = recorder.PrepareToRecord();
			if (prepared == false)
			{
				Debug.WriteLine ("Recorder failed to prepare");
				return false;
			}

			bool success = recorder.RecordAt (0.0);
			if (success == false)
			{
				Debug.WriteLine ("Recorder failed to record:");
				if (error != null)
				{
					Debug.WriteLine("error:" + error.GetDebugDescription());
				}
			}

			return success;
		}
        }
}
Comment 1 Rolf Bjarne Kvinge [MSFT] 2015-02-16 13:45:32 UTC
Can you attach a complete project we can use to reproduce this?
Comment 2 Andy 2015-02-16 15:08:25 UTC
Yes, I have provided a link to my project in Dropbox because the project was too large to attach to this bug directly. It basically has 1 button that calls record. When you press that button, you will notice that the call to record fails when you are using a device.

https://dl.dropboxusercontent.com/u/10428475/Submission_Xamarin_Bug_27109.zip
Comment 3 Rajneesh Kumar 2015-02-17 03:50:07 UTC
I have checked this issue and able to reproduce this. To reproduce this issue I have followed the steps and instruction provided in bug description.

Steps I followed:

1. Open attached test case in XS provided in comment 2.
2. Build ad deploy it on device, it deploy successfully.
3. Click on record button.
4. In application output you will get the following:
"Recorder failed to record:"

Screencast(iPhone): http://www.screencast.com/t/kFvPohqKZ

I also observed that its working fine on simulator. Here is the screencast for the same:
http://www.screencast.com/t/3aM0QJAdt2

Ide Logs: https://gist.github.com/Rajneesh360Logica/bde641ced8d076ee305b
Application Output: https://gist.github.com/Rajneesh360Logica/eaf150c9afb46b8a118d
Build Output: https://gist.github.com/Rajneesh360Logica/f8cabe1df9bedf289394
Device logs: https://gist.github.com/Rajneesh360Logica/b0a4b932bbc989ce079a
iOS Designer Logs: https://gist.github.com/Rajneesh360Logica/c4603b23dac35530f61a

Environment Info:

Xamarin.Forms Version: 1.3.4.6331-pre4

=== Xamarin Studio ===

Version 5.7.2 (build 1)
Installation UUID: 011d70a5-dede-428b-ab04-ef451c2e539d
Runtime:
	Mono 3.12.0 ((detached/de2f33f)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 312000076

=== Apple Developer Tools ===

Xcode 6.1 (6604)
Build 6A1052d

=== Xamarin.Mac ===

Version: 1.12.0.6 (Business Edition)

=== Xamarin.Android ===

Version: 4.20.0.28 (Business Edition)
Android SDK: /Users/MM/Desktop/android-sdk-macosx
	Supported Android versions:
		2.1    (API level 7)
		2.2    (API level 8)
		2.3    (API level 10)
		3.1    (API level 12)
		4.0    (API level 14)
		4.0.3  (API level 15)
		4.1    (API level 16)
		4.2    (API level 17)
		4.3    (API level 18)
		4.4    (API level 19)
		4.4.87 (API level 20)
		5.0    (API level 21)
Java SDK: /usr
java version "1.7.0_65"
Java(TM) SE Runtime Environment (build 1.7.0_65-b17)
Java HotSpot(TM) 64-Bit Server VM (build 24.65-b04, mixed mode)

=== Xamarin.iOS ===

Version: 8.6.2.19 (Business Edition)
Hash: d508c8e
Branch: 
Build date: 2015-02-15 22:17:46-0500

=== Build Information ===

Release ID: 507020001
Git revision: 103486e2547553077836a5ba20a973487b983830
Build date: 2015-02-13 11:56:36-05
Xamarin addins: 8dd5b934e86ef0595c022dd3930fd40e3376ab4c

=== Operating System ===

Mac OS X 10.9.5
Darwin MacMini.local 13.4.0 Darwin Kernel Version 13.4.0
    Sun Aug 17 19:50:11 PDT 2014
    root:xnu-2422.115.4~1/RELEASE_X86_64 x86_64
Comment 4 Rolf Bjarne Kvinge [MSFT] 2015-02-17 07:56:08 UTC
You must initialize an audio session before trying to record [1]. Add this at the top of your Prepare method and it works:

	var audioSession = AVAudioSession.SharedInstance ();
	var err = audioSession.SetCategory (AVAudioSessionCategory.PlayAndRecord);
	if(err != null) {
		Console.WriteLine ("audioSession: {0}", err);
		return false;
	}
	err = audioSession.SetActive (true);
	if(err != null ){
		Console.WriteLine ("audioSession: {0}", err);
		return false;
	}

[1] http://stackoverflow.com/a/9706731/183422
Comment 5 Andy 2015-02-17 11:33:55 UTC
That worked! I would purport that this issue is not resolved until the recipe at http://developer.xamarin.com/recipes/ios/media/sound/record_sound/ is updated.
Comment 6 Rolf Bjarne Kvinge [MSFT] 2015-02-17 12:06:49 UTC
Thanks, I've notified our documentation team about it.