Search code examples
androidxamarinxamarin.androidvideo-capturexamarin-studio

Issues with basic video recording


So I followed the default recipe for a starter video recording app, but I am running into errors that I cannot figure out. The error I am getting is on recorder.Prepare(); it is saying /0/test.mp4: Open Failed: ENOENT (no such file or directory) Now I will paste the code I below, error is line 45. Also I have set permissions correctly as

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.CAPTURE_SECURE_VIDEO_OUTPUT" />
<uses-permission android:name="android.permission.CAPTURE_VIDEO_OUTPUT" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

I assumed this was fine as I added the "CAMERA and RECORD_AUDIO" from the recpie and then I added the other three from googling this issue prior to posting.

using System;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Media;

namespace CameraTest
{
    [Activity (Label = "CameraTest", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        MediaRecorder recorder;

        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);

            SetContentView (Resource.Layout.Main);
            var record = FindViewById<Button> (Resource.Id.Record);
            var stop = FindViewById<Button> (Resource.Id.Stop);
            var play = FindViewById<Button> (Resource.Id.Play);       
            var video = FindViewById<VideoView> (Resource.Id.SampleVideoView);
            Android.OS.Environment.ExternalStorageDirectory.SetWritable(true);
            string path = Android.OS.Environment.ExternalStorageDirectory.Name + "/test.mp4";


            // Set our view from the "main" layout resource
            // Get our button from the layout resource,
            // and attach an event to it
            record.Click += delegate {
                video.StopPlayback ();

                recorder = new MediaRecorder ();
                recorder.SetVideoSource (VideoSource.Camera); 
                recorder.SetAudioSource (AudioSource.Mic);              
                recorder.SetOutputFormat (OutputFormat.Default);
                recorder.SetVideoEncoder (VideoEncoder.Default); 
                recorder.SetAudioEncoder (AudioEncoder.Default);      
                recorder.SetOutputFile (path);       
                recorder.SetPreviewDisplay (video.Holder.Surface);         
                recorder.Prepare ();
                recorder.Start ();   } ;

            stop.Click += delegate {
                if (recorder != null) {
                    recorder.Stop ();
                    recorder.Release ();
                }
            };

            play.Click += delegate {
                var uri = Android.Net.Uri.Parse (path);        
                video.SetVideoURI (uri);
                video.Start ();   
            };


        }
        protected override void OnDestroy ()
        {
            base.OnDestroy ();

            if (recorder != null) {
                recorder.Release ();
                recorder.Dispose ();
                recorder = null;
            }
        }
    }
}

Solution

  • Change

     string path = Android.OS.Environment.ExternalStorageDirectory.Name + "/test.mp4";
    

    To:

    string path = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath + "/test.mp4";
    

    Your original code only uses the directory name not the full path as the output location.