GetAudio Plugin for MvvmCross - ios

I'm developing an plugin for MvvmCross to GetAudio from the device. But I'm getting an error in the Touch implementation. I've already took a look at here, here and here.
But nothing fixed my problem.
Well, so far I have:
var audioDelegate = new AudioDelegate();
audioDelegate.AudioAvailable = ProcessAudio;
mediaPicker = new MPMediaPickerController();
mediaPicker.Delegate = audioDelegate;
mediaPicker.AllowsPickingMultipleItems = false;
modalHost.PresentModalViewController (mediaPicker, true);
To start the picker from audio, where AudioDelegate is:
private class AudioDelegate : MPMediaPickerControllerDelegate
{
public EventHandler<MvxAudioRecorderEventArgs> AudioAvailable;
public override void MediaItemsPicked (MPMediaPickerController sender, MPMediaItemCollection mediaItemCollection)
{
if (mediaItemCollection.Count < 1)
{
return;
}
MvxAudioRecorderEventArgs eventArg = new MvxAudioRecorderEventArgs (mediaItemCollection.Items [0].AssetURL);
AudioAvailable (this, eventArg);
}
}
And then in ProcessAudio:
private void ProcessMedia(object sender, UIImagePickerMediaPickedEventArgs e)
{
var assetURL = e.MediaUrl;
NSDictionary dictionary = null;
var assetExtension = e.MediaUrl.Path.Split ('.') [1];
var songAsset = new AVUrlAsset (assetURL, dictionary);
var exporter = new AVAssetExportSession (songAsset, AVAssetExportSession.PresetPassthrough.ToString ());
exporter.OutputFileType = (assetExtension == "mp3") ? "com.apple.quicktime-movie" : AVFileType.AppleM4A;
var manager = new NSFileManager ();
var count = 0;
string filePath = null;
do
{
var extension = "mov";//( NSString *)UTTypeCopyPreferredTagWithClass(( CFStringRef)AVFileTypeQuickTimeMovie, kUTTagClassFilenameExtension);
var fileNameNoExtension = "AUD_" + Guid.NewGuid ().ToString ();
var fileName = string.Format ("{0}({1})", fileNameNoExtension, count);
filePath = Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments) + "/";
filePath = filePath + fileName + "." + extension;
count++;
} while (manager.FileExists (filePath));
var outputURL = new NSUrl (filePath);//should be something in objective C... => [NSURL fileURLWithPath:filePath];
exporter.OutputUrl = outputURL;
exporter.ExportAsynchronously ( () =>
{
var exportStatus = exporter.Status;
switch (exportStatus)
{
case AVAssetExportSessionStatus.Unknown:
case AVAssetExportSessionStatus.Completed:
{
//remove the .mov from file and make it available for callback mediaAvailable()...
break;
}
case AVAssetExportSessionStatus.Waiting:
case AVAssetExportSessionStatus.Cancelled:
case AVAssetExportSessionStatus.Exporting:
case AVAssetExportSessionStatus.Failed:
default:
{
var exportError = exporter.Error;
if(assumeCancelled != null)
{
assumeCancelled();
}
break;
}
}
});
mediaPicker.DismissViewController(true, () => { });
modalHost.NativeModalViewControllerDisappearedOnItsOwn();
}
But it is always with Status.Failed with error:
LocalizedDescription: The operation could not be completed
Description: Error Domain=AVFoundationErrorDomain Code=-11800
"The operation could not be completed" UserInfo=0x1476cce0
{NSLocalizedDescription=The operation could not be completed,
NSUnderlyingError=0x1585da90 "The operation couldn’t be completed.
(OSStatus error -12780.)", NSLocalizedFailureReason=An unknown error
occurred (-12780)}
Can anyone help me?
Thanks in regard,

Found the solution. As I thought the error was in the outputURL file.
Changed it to:
var count = 0;
string filePath = null;
do
{
var extension = "mp3.mov";//( NSString *)UTTypeCopyPreferredTagWithClass(( CFStringRef)AVFileTypeQuickTimeMovie, kUTTagClassFilenameExtension);
var fileNameNoExtension = "AUD_" + Guid.NewGuid ().ToString ();
var fileName = (count == 0) ? fileNameNoExtension : string.Format ("{0}({1})", fileNameNoExtension, count);
filePath = NSBundle.MainBundle.BundlePath + "/../tmp/" + fileName; /* HERE WAS THE FIRST PROBLEM, USE NSBUNDLE... */
filePath = filePath + fileName + "." + extension;
count++;
} while (manager.FileExists (filePath));
var outputURL = NSUrl.FromFilename(filePath); /* HERE WAAS THE SECOND PROBLEM, CREATE IT WITH FROMFILENAME INSTEAD OF NEW... */
And then in the export, just remove the .mov extension...
var withoutMOVPath = outputURL.Path.Remove(outputURL.Path.Length - 4);
NSError error = null;
manager.Move (outputURL.Path, withoutMOVPath, out error);
if(error != null && assumeCancelled != null)
{
assumeCancelled();
return;
}
var mediaStream = new FileStream (withoutMOVPath, FileMode.Open);
mediaAvailable (mediaStream);
break;

Related

Youtube Livestream Api LiveChatMessages List

Im trying to get the Messages from a Youtube Livestream, works, but i dont get new Messages. The NextPageToken is included.
Sometimes i get new messages, but it takes arround 5-10min.
Youtube Chat Sending works also fine.
Any Idea?
This is from the Docs: https://developers.google.com/youtube/v3/live/docs/liveChatMessages/list
private async Task GetMessagesAsync(string liveChatId, string nextPageToken, long? pollingIntervalMillis)
{
liveChatId = "EiEKGFVDVUQ3WGNXTk92SlpvaHFMM3dZTi1uZxIFL2xpdmU";
if (!updatingChat)
{
if (!string.IsNullOrEmpty(liveChatId))
{
newMessages = true;
var chatMessages = youTubeService.LiveChatMessages.List(liveChatId, "id,snippet,authorDetails");
var chatResponse = await chatMessages.ExecuteAsync();
PageInfo pageInfo = chatResponse.PageInfo;
newMessages = false;
if (pageInfo.TotalResults.HasValue)
{
if (!prevCount.Equals(pageInfo.TotalResults.Value))
{
prevCount = pageInfo.TotalResults.Value;
newMessages = true;
}
}
if (newMessages)
{
Messages = new List<YouTubeMessage>();
foreach (var chatMessage in chatResponse.Items)
{
string messageId = chatMessage.Id;
string displayName = chatMessage.AuthorDetails.DisplayName;
string displayMessage = chatMessage.Snippet.DisplayMessage;
string NextPagetoken = chatResponse.NextPageToken;
YouTubeMessage message = new YouTubeMessage(messageId, displayName, displayMessage);
if (!Messages.Contains(message))
{
Messages.Add(message);
string output = "[" + displayName + "]: " + displayMessage;
Console.WriteLine(time + output);
}
}
}
await GetMessagesAsync(liveChatId, chatResponse.NextPageToken, chatResponse.PollingIntervalMillis);
}
}
updatingChat = false;
await Task.Delay(100);
}
public async Task YouTubeChatSend(string message)
{
try
{
LiveChatMessage liveMessage = new LiveChatMessage();
liveMessage.Snippet = new LiveChatMessageSnippet()
{
LiveChatId = "EiEKGFVDVUQ3WGNXTk92SlpvaHFMM3dZTi1uZxIFL2xpdmU",
Type = "textMessageEvent",
TextMessageDetails = new LiveChatTextMessageDetails() { MessageText = message }
};
var insert = this.youTubeService.LiveChatMessages.Insert(liveMessage, "snippet");
var response = await insert.ExecuteAsync();
if (response != null)
{
}
}
catch
{
Console.WriteLine("Failed to chat send");
}
}

UIImagePickerController crashesdue to memory issues in ios 10

I get random memory crashes with no memory warning. I have updated my ios to 10.0.2, my Xamarin to 6.2. I added camera usage description and photo library usage description, as suggested.
I have the following code in my FinishedPickingMedia callback
public async override void FinishedPickingMedia(UIImagePickerController picker, NSDictionary info)
{
try
{
// determine what was selected, video or image
bool isImage = false;
switch (info[UIImagePickerController.MediaType].ToString())
{
case "public.image":
isImage = true;
break;
case "public.video":
break;
}
if (isImage)
{
UIImage originalImage = info[UIImagePickerController.OriginalImage] as UIImage;
if (originalImage != null)
{
UIImageOrientation OrIn = originalImage.Orientation;
Debug.WriteLine("scaling image");
var originalImage1 = await Task.Run(() => ScaleAndRotateImage.ScaleAndRotateImageView(originalImage, OrIn));
if (originalImage1 != null)
{
var Data = originalImage1.AsJPEG(0.0f);
if (Data != null)
{
UIImage resizedImage = UIImage.LoadFromData(Data);
if (originalImage != null)
{
originalImage.Dispose();
originalImage = null;
}
originalImage1.Dispose();
originalImage1 = null;
Data.Dispose();
Data = null;
GC.Collect();
#if DEBUG
var Process1 = Process.GetCurrentProcess();
if (Process1 != null)
{
Debug.WriteLine(string.Format("memory allocated by FinishedPickingMedia = {0}", Process1.WorkingSet64));
}
#endif
camController.ShowPhoto(resizedImage);
}
if (info != null)
{
info.Dispose();
info = null;
}
GC.Collect();
}
}
}
}
catch (Exception ex)
{
Debug.WriteLine("Exception Occured in FinishedPickingMedia method due to " + ex.Message);
}
finally
{
// dismiss the picker
picker.DismissModalViewController(true);
picker.Dispose();
picker = null;
GC.Collect();
}
}
This seams to happen randomly, my application needs to capture more than 200 photos (archive) I researched on the internet and this seams to be an issue with iOS 10 and this control

Xamarin ios - referenceUrl from UIImagePickerController always null

I'm selecting an image form the gallery using the UIImagePickerController. After selecting an image, I would like to update the real image file path on a text field.
Can I get the file path from the referenceUrl? The referenceUrl in my case always returns null.
protected void Handle_FinishedPickingMedia (object sender, UIImagePickerMediaPickedEventArgs e)
{
try{
//determine what was selected, video or image
bool isImage = false;
switch(e.Info [UIImagePickerController.MediaType].ToString()) {
case "public.image":
Console.WriteLine("Image selected");
isImage = true;
break;
}
// get common info (shared between images and video)
NSUrl referenceURL = e.Info[new NSString("UIImagePickerControllerReferenceUrl")] as NSUrl;
if (referenceURL != null)
Console.WriteLine("Url:"+referenceURL.ToString ());
// if it was an image, get the other image info
if(isImage) {
// get the original image
UIImage originalImage = e.Info[UIImagePickerController.OriginalImage] as UIImage;
if(originalImage != null) {
// do something with the image
new Thread(new System.Threading.ThreadStart(() => {
Thread.Sleep(350);
BeginInvokeOnMainThread (() => {
var tag = ((UIButton)sender).Tag;
//UIButton senderButton = (UIButton)sender;
switch(tag)
{
case 0:
// do something here
break;
case 1:
// do something here
break;
});
})).Start();
}
}
// dismiss the picker
imagePicker.DismissModalViewController (true);
}catch(Exception ex)
{
ShowAlert ("Failed !", "Unable to select image", "");
Console.WriteLine(ex.Message + ex.StackTrace);
}
}
For anyone else facing this issue, the solution was a simple typo.I printed out the details of the NSDictionary and noticed that the "Url" part of the
UIImagePickerControllerReferenceUrl was all caps. This is what worked for me.
Change this line:
NSUrl referenceURL = e.Info[new NSString("UIImagePickerControllerReferenceUrl")] as NSUrl;
To this :
NSUrl referenceURL = e.Info[new NSString("UIImagePickerControllerReferenceURL")] as NSUrl;
To get the filename of the selected image, I added the AssestsLibrary and used that to extract the necessary metadata.
Here's my full implementation:
protected void Handle_FinishedPickingMedia (object sender, UIImagePickerMediaPickedEventArgs e)
{
try{
//determine what was selected, video or image
bool isImage = false;
switch(e.Info [UIImagePickerController.MediaType].ToString()) {
case "public.image":
Console.WriteLine("Image selected");
isImage = true;
break;
}
// get common info (shared between images and video)
NSUrl referenceURL = e.Info[new NSString("UIImagePickerControllerReferenceURL")] as NSUrl;
if (referenceURL != null)
Console.WriteLine("Url:"+referenceURL.ToString ());
ALAssetsLibrary assetsLibrary = new ALAssetsLibrary();
assetsLibrary.AssetForUrl(referenceURL,delegate (ALAsset asset){
ALAssetRepresentation representation = asset.DefaultRepresentation;
if (representation == null)
{
return;
}else{
string fileName = representation.Filename;
Console.WriteLine("Image Filename :" + fileName);
}
},delegate(NSError error) {
Console.WriteLine ("User denied access to photo Library... {0}", error);
});
// if it was an image, get the other image info
if(isImage) {
// get the original image
UIImage originalImage = e.Info[UIImagePickerController.OriginalImage] as UIImage;
if(originalImage != null) {
// do something with the image
new Thread(new System.Threading.ThreadStart(() => {
Thread.Sleep(350);
BeginInvokeOnMainThread (() => {
var tag = ((UIButton)sender).Tag;
//UIButton senderButton = (UIButton)sender;
switch(tag)
{
case 0:
// do something here
break;
case 1:
// do something here
break;
});
})).Start();
}
}
// dismiss the picker
imagePicker.DismissModalViewController (true);
}catch(Exception ex)
{
ShowAlert ("Failed !", "Unable to select image", "");
Console.WriteLine(ex.Message + ex.StackTrace);
}
}

Trimming video with Monotouch fails with "The operation could not be completed"

I am trying to trim a video to 5 seconds programmatically. Here is my implementation.
AVAssetExportSession exportSession= new AVAssetExportSession(videoAsset,AVAssetExportSession.PresetLowQuality.ToString());
int SystemVersion = Convert.ToInt16(UIDevice.CurrentDevice.SystemVersion.Split('.')[0]);
string filename;
if (SystemVersion >= 8)
{
var documents = NSFileManager.DefaultManager.GetUrls(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomain.User)[0].Path;
filename = Path.Combine(documents, "trimmed.mov");
}
else
{
var documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); // iOS 7 and earlier
filename = Path.Combine(documents, "trimmed.mov");
}
outputUrl=new NSUrl(filename);
exportSession.OutputUrl = outputUrl;
CMTime start = new CMTime((long)1, 1);
CMTime duration = new CMTime((long)5, 1);
CMTimeRange range = new CMTimeRange();
range.Start=start;
range.Duration=duration;
exportSession.TimeRange = range;
exportSession.OutputFileType = AVFileType.QuickTimeMovie;
ExportTrimmedVideo( exportSession);
async void ExportTrimmedVideo(AVAssetExportSession exportSession)
{
await exportSession.ExportTaskAsync ();
if (exportSession.Status == AVAssetExportSessionStatus.Completed) {
InvokeOnMainThread (() => {
new UIAlertView ("Export Sucess", "Video is trimmed", null, "O K").Show ();
});
}
else
{
InvokeOnMainThread (() => {
new UIAlertView ("Export Falure", exportSession.Error.Description, null, "O K").Show ();
});
}
}
But in completion I am getting a Filed Status. Full NSError Description is as follows
Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo=0x7cebcf80 {NSLocalizedDescription=The operation could not be completed, NSUnderlyingError=0x7cb08410 "The operation couldn’t be completed. (OSStatus error -12105.)", NSLocalizedFailureReason=An unknown error occurred (-12105)}
What am I possibly doing wrong ?
Edit
I have referred apple's documentation on Trimming video and have modified the above code with no positive effect as below.
var compatiblePresets= AVAssetExportSession.ExportPresetsCompatibleWithAsset(videoAsset).ToList();
var preset="";
if(compatiblePresets.Contains("AVAssetExportPresetLowQuality"))
{
preset="AVAssetExportPresetLowQuality";
}
else
{
preset=compatiblePresets.FirstOrDefault();
}
AVAssetExportSession exportSession= new AVAssetExportSession(videoAsset,preset);
int SystemVersion = Convert.ToInt16(UIDevice.CurrentDevice.SystemVersion.Split('.')[0]);
string filename;
if (SystemVersion >= 8)
{
var documents = NSFileManager.DefaultManager.GetUrls(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomain.User)[0].Path;
filename = Path.Combine(documents, "trimmed.mov");
}
else
{
var documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); // iOS 7 and earlier
filename = Path.Combine(documents, "trimmed.mov");
}
outputUrl=new NSUrl(filename);
exportSession.OutputUrl = outputUrl;
exportSession.OutputFileType = AVFileType.QuickTimeMovie;
CMTime start = new CMTime((long)1, 600);
CMTime duration = new CMTime((long)5, 600);
CMTimeRange range = new CMTimeRange();
range.Start=start;
range.Duration=duration;
exportSession.TimeRange = range;
ExportTrimmedVideo( exportSession);
Try this code below. I modified exportSession.OutputUrl and how you initialize your CMTimeRange. Are you trimming it down to a 4 second clip?
var compatiblePresets= AVAssetExportSession.ExportPresetsCompatibleWithAsset(videoAsset).ToList();
var preset="";
if(compatiblePresets.Contains("AVAssetExportPresetLowQuality"))
{
preset="AVAssetExportPresetLowQuality";
}
else
{
preset=compatiblePresets.FirstOrDefault();
}
using (var exportSession = new AVAssetExportSession(videoAsset, preset))
{
int SystemVersion = Convert.ToInt16(UIDevice.CurrentDevice.SystemVersion.Split('.')[0]);
string filename;
if (SystemVersion >= 8)
{
var documents = NSFileManager.DefaultManager.GetUrls(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomain.User)[0].Path;
filename = Path.Combine(documents, "trimmed.mov");
}
else
{
var documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); // iOS 7 and earlier
filename = Path.Combine(documents, "trimmed.mov");
}
exportSession.OutputUrl = NSUrl.FromFilename(filename);
exportSession.OutputFileType = AVFileType.QuickTimeMovie;
var range = new CMTimeRange();
range.Start = CMTime.FromSeconds (1, videoAsset.Duration.TimeScale);
range.Duration = CMTime.FromSeconds (5, videoAsset.Duration.TimeScale);
exportSession.TimeRange = range;
}
ExportTrimmedVideo( exportSession);

monotouch ios - audioplayer returns nil when initializing

Okay, I have this problem for almost a month now. Some audio files are not playing. It just returns an error like this:
Could not initialize an instance of the type 'AVFoundation.AVAudioPlayer': the native 'initWithContentsOfURL:error:' method returned nil.
Here's the code initialising the AudioPlayer:
NSData data = new NSData();
if (AppSession.IsConnected ()) {/*Just a checker if connection is available or not*/
if (uri != null && uri.ToString().Length > 0) {
data = await LoadData (uri);
}
} else { data = null; }
string saveFile = FolderPath(uri, "playthrough");
data.Save(saveFile, false);
NSUrl fileUrl = NSUrl.FromString(saveFile);
audioplayer = AVAudioPlayer.FromUrl(fileUrl);
audioplayer.NumberOfLoops = 0;
audioplayer.Volume = 1.5f;
audioplayer.PrepareToPlay();
audioplayer.Play();
For LoadData:
public static async Task<NSData> LoadData (NSUrl url)
{
NSData data = new NSData ();
if (url.ToString ().Contains ("http")) {
var httpClient = new HttpClient ();
Task<byte[]> contentsTask = httpClient.GetByteArrayAsync (url.ToString ());
var contents = await contentsTask;
data = NSData.FromArray(contents);
} else { data = NSData.FromUrl (url); }
return data;
}
For FolderPath:
public static string FolderPath(NSUrl url, string fileName)
{
string[] dotSplitter = url.ToString ().Split (new char[]{ '.' }, 4);
string ext = "";
if (dotSplitter.Length == 4) {
switch (dotSplitter [3]) {
case "wav": ext = ".wav"; break;
case "mp3": ext = ".mp3"; break;
case "3gpp": ext = ".3gpp"; break;
case "mp4": ext = ".mp4"; break;
}
} else {
switch (dotSplitter [0]) {
case "wav": ext = ".wav"; break;
case "mp3": ext = ".mp3"; break;
case "3gpp": ext = ".3gpp"; break;
case "mp4": ext = ".mp4"; break;
}
}
return Path.Combine(TMPDir(), fileName + ext);
}
And here are the files I'm using to test the audio:
http://files.parsetfss.com/6ea4a3c5-a4e2-463f-8374-247d5db0fbd5/tfss-c7db3001-b7b0-465d-b59b-233c1fe568ec-filtered_427201531308PM_song.wav
http://files.parsetfss.com/6ea4a3c5-a4e2-463f-8374-247d5db0fbd5/tfss-c4426fba-ea52-4764-9fb6-6b9f10aba89f-filtered_27042015154318_song.wav
So, yeah. I've done dozens of research, google, experiment and tears for this, but I ended up with no result at all. Any solutions for this?
The full exception you should get is:
System.Exception : Could not initialize an instance of the type 'MonoTouch.AVFoundation.AVAudioPlayer': the native 'initWithContentsOfURL:error:' method returned nil.
It is possible to ignore this condition by setting MonoTouch.ObjCRuntime.Class.ThrowOnInitFailure to false.
Following the above advice (use the overload accepting an NSError) and the one from the exception message you get something like:
MonoTouch.ObjCRuntime.Class.ThrowOnInitFailure = false;
NSError error;
using (var url = NSUrl.FromString ("http://files.parsetfss.com/6ea4a3c5-a4e2-463f-8374-247d5db0fbd5/tfss-c7db3001-b7b0-465d-b59b-233c1fe568ec-filtered_427201531308PM_song.wav"))
using (var player = AVAudioPlayer.FromUrl (url, out error)) {
Console.WriteLine (error);
}
You'll get:
The operation couldn’t be completed. (OSStatus error 2003334207.)
which is a very common error for AVAudioPlayer. I also get the same error if you use an NSData (local file). However AVPlayer loads the URL just fine...

Resources