2016-09-01 16 views
0

私はthis recipeに従ってXamarinで音声を録音しようとしています。これはXamarin.Formsソリューションですが、今のところ私はAndroid向けに開発しています。私はC#/。NET開発者だと言わねばならず、これはAndroidとの最初の連絡です。私はMicTest.Droidプロジェクトで「サービス」フォルダを作成し、私はそこに私のサービスを置く:Xamarinで音声を録音する "開けませんでした:EACCES(許可が拒否されました)"

[assembly: Dependency(typeof(MicrophoneService))] 
namespace MicTest.Droid.Services 
{ 
    public class MicrophoneService : IMicrophoneService 
    { 
     public string FileName { get; private set; } 
     public string FilePath { get { return Path.Combine("data", $"{this.FileName}.3gpp"); } } 

     private MediaRecorder recorder; 

     public MicrophoneService() { } 

     public void StartRecording(string fileName) 
     { 
      try 
      { 
       this.FileName = fileName; 
       //bool exist = Directory.Exists("data"); 

       this.recorder = new MediaRecorder(); 
       this.recorder.SetAudioSource(AudioSource.Mic); 
       this.recorder.SetOutputFormat(OutputFormat.ThreeGpp); 
       this.recorder.SetAudioEncoder(AudioEncoder.AmrNb); 
       this.recorder.SetOutputFile(this.FilePath); 
       this.recorder.Prepare(); // It crashes here 
       this.recorder.Start(); 
      } 
      catch (Exception ex) 
      { 
       Console.WriteLine(ex.Message); 
       Console.WriteLine(ex.StackTrace); 
      } 
     } 

     public void StopRecording() 
     { 
      try 
      { 
       this.recorder.Stop(); 
       this.recorder.Reset(); 
      } 
      catch (Exception ex) 
      { 
       Console.WriteLine(ex.Message); 
       Console.WriteLine(ex.StackTrace); 
      } 
     } 
    } 
} 

IMicrophoneServiceインタフェースは、共有プロジェクトである:

namespace MicTest.Services 
{ 
    public interface IMicrophoneService 
    { 
     void StartRecording(string fileName); 
     void StopRecording(); 
    } 
} 

そして、これはexcetionです:

data/2016-08-31 16.22.58.3gpp: open failed: EACCES(Permission denied) 
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() [0x0000c] in /Users/builder/data/lanes/2923/52635947/source/mono/external/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143 
    at Java.Interop.JniEnvironment+InstanceMethods.CallVoidMethod (JniObjectReference instance, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x00084] in /Users/builder/data/lanes/3340/4e275588/source/Java.Interop/src/Java.Interop/Java.Interop/JniEnvironment.g.cs:11643 
    at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeVirtualVoidMethod (System.String encodedMember, IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) [0x00031] in /Users/builder/data/lanes/3340/4e275588/source/Java.Interop/src/Java.Interop/Java.Interop/JniPeerMembers.JniInstanceMethods_Invoke.cs:26 
    at Android.Media.MediaRecorder.Prepare() [0x00000] in /Users/builder/data/lanes/3340/4e275588/source/monodroid/src/Mono.Android/platforms/android-23/src/generated/Android.Media.MediaRecorder.cs:476 
    at MicTest.Droid.Services.MicrophoneService.StartRecording(System.String fileName) [0x0006f] in F:\Probak\Xamarin\MicTest\MicTest\MicTest.Droid\Services\MicrophoneService.cs:53  --- End of managed exception stack trace --- 
java.io.FileNotFoundException: data/2016-08-31 16.22.58.3gpp: open failed: EACCES(Permission denied) 
    at libcore.io.IoBridge.open(IoBridge.java:456) 
    at java.io.RandomAccessFile.<init>(RandomAccessFile.java:117) 
    at java.io.RandomAccessFile.<init>(RandomAccessFile.java:149) 
    at android.media.MediaRecorder.prepare(MediaRecorder.java:750) 
    at md5b60ffeb829f638581ab2bb9b1a7f4f3f.ButtonRenderer_ButtonClickListener.n_onClick(Native Method) 
    at md5b60ffeb829f638581ab2bb9b1a7f4f3f.ButtonRenderer_ButtonClickListener.onClick(ButtonRenderer_ButtonClickListener.java:30) 
    at android.view.View.performClick(View.java:4763) 
    at android.view.View$PerformClick.run(View.java:19821) 
    at android.os.Handler.handleCallback(Handler.java:739) 
    at android.os.Handler.dispatchMessage(Handler.java:95) 
    at android.os.Looper.loop(Looper.java:135) 
    at android.app.ActivityThread.main(ActivityThread.java:5272) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704) 
Caused by: android.system.ErrnoException: open failed: EACCES(Permission denied) 
    at libcore.io.Posix.open(Native Method) 
    at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186) 
    at libcore.io.IoBridge.open(IoBridge.java:442) 
    ... 15 more 

最後に、これはマニフェストファイルです(将来は位置情報サービスを使用したい)。

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="auto"> 
    <uses-sdk android:minSdkVersion="15" /> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
    <uses-permission android:name="android.permission.RECORD_AUDIO" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
    <application android:label="$safeprojectname$"></application> 
</manifest> 

「データ」フォルダを使用できるかどうかわかりません。Android.OS.Environment.DirectoryDownloadsのような他のフォルダを同じ結果で試しました。私は何かを忘れていますか?

答えて

0

有効なあなたの権限にアプリのキャッシュディレクトリに保存してくださいすることができます

var fileName = "audio.3gpp"; 
fileName = Path.Combine(Android.Content.Context.CacheDir.Path, fileName) 
this.FileName = fileName; 
+0

は答えてくれてありがとう、しかし、温首相は、私は 'Android.Content.Context.CacheDir.Path'を使用して、このエラーが表示されます。 **非静的フィールド、メソッド、またはプロパティ 'Context.CacheDir'にオブジェクト参照が必要** ** – Jon

+0

Android.Content.Context.CacheDirの代わりに 'Android.App.Application.Context.CacheDir.Path'を使用する.Path'は動作します(同じものがあるかどうかはわかりませんが)、今は 'this.recorder.Stop();'行に 'Java.Lang.IllegalStateException'があります。 – Jon

+0

@Jon 'Android.App.Application .....'はうまく動作します(これはサービス内にあることに気付かず、利用可能なアプリケーションコンテキストがありません)、 '.Stop'の' IllegalStateException' MediaRecorderのフローチャート@https://developer.android.com/reference/android/media/MediaRecorder.html – SushiHangover

関連する問題