基本视频录制的问题

时间:2014-11-21 01:49:15

标签: android xamarin xamarin.android video-capture xamarin-studio

所以我遵循了初学者视频录制应用程序的默认配方,但我遇到了一些我无法弄清楚的错误。我得到的错误是在recorder.Prepare();它是说/0/test.mp4:打开失败:ENOENT(没有这样的文件或目录)现在我将粘贴下面的代码,错误是第45行。此外,我已正确设置权限

<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" />

我认为这很好,因为我从recpie添加了“CAMERA和RECORD_AUDIO”,然后我在发布之前通过Google搜索来添加其他三个。

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;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

更改

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

要:

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

您的原始代码仅使用目录名称而非完整路径作为输出位置。