Invalid State Of Audio Recorder (Bad FilePath?)









up vote
0
down vote

favorite












I have a recorder I'm trying to make to test it out and see how it works. However when I run it I get the error:



E/MediaRecorder: start called in an invalid state: 4


So I did some googlejitzu and figured out that my file path is wrong. This is my code:



final MediaRecorder recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile("Test");

System.out.println("Hello");
try
recorder.prepare();
catch (IOException e)
e.printStackTrace();


recorder.start();
CountDownTimer countDownTimer = new CountDownTimer(30000, 1000)
@Override
public void onTick(long l)



@Override
public void onFinish()
recorder.stop();

.start();


So I guess I was wondering how to make a file path directory on the phone for the audio to be saved to.



The Error is:



 11-11 01:49:48.275 23703-23703/com.example.arege.dayatalisten W/System.err: java.io.FileNotFoundException: Test: open failed: EROFS (Read-only file system)
11-11 01:49:48.276 23703-23703/com.example.arege.dayatalisten W/System.err: at libcore.io.IoBridge.open(IoBridge.java:455)
at java.io.RandomAccessFile.<init>(RandomAccessFile.java:247)
at java.io.RandomAccessFile.<init>(RandomAccessFile.java:128)
at android.media.MediaRecorder.prepare(MediaRecorder.java:834)
at com.example.arege.dayatalisten.ListeningToTheWorld.onStartCommand(ListeningToTheWorld.java:47)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3432)
at android.app.ActivityThread.-wrap21(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1633)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6316)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
Caused by: android.system.ErrnoException: open failed: EROFS (Read-only file system)
11-11 01:49:48.277 23703-23703/com.example.arege.dayatalisten W/System.err: at libcore.io.Posix.open(Native Method)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:187)
at libcore.io.IoBridge.open(IoBridge.java:441)
... 13 more


I have both required permissions.










share|improve this question























  • Why are you catching the error from recorder.prepare() and just printing it and going ahead with recorder.start() ? If prepare() throws a exception, then the Media recorder is in a invalid state. Can you see what kind of exception in thrown my prepare? if it does, please post the stacktrace. Also make sure you have requested and received permission to write to external storage.
    – Abhijith Shivaswamy
    Nov 11 at 6:33











  • So I did the recorder.start() like that because thats how it is done in the documentation, so I was trying to be like that. I found the stack error - and posted it. Thank you for guiding me!
    – Gorgochef
    Nov 11 at 6:52














up vote
0
down vote

favorite












I have a recorder I'm trying to make to test it out and see how it works. However when I run it I get the error:



E/MediaRecorder: start called in an invalid state: 4


So I did some googlejitzu and figured out that my file path is wrong. This is my code:



final MediaRecorder recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile("Test");

System.out.println("Hello");
try
recorder.prepare();
catch (IOException e)
e.printStackTrace();


recorder.start();
CountDownTimer countDownTimer = new CountDownTimer(30000, 1000)
@Override
public void onTick(long l)



@Override
public void onFinish()
recorder.stop();

.start();


So I guess I was wondering how to make a file path directory on the phone for the audio to be saved to.



The Error is:



 11-11 01:49:48.275 23703-23703/com.example.arege.dayatalisten W/System.err: java.io.FileNotFoundException: Test: open failed: EROFS (Read-only file system)
11-11 01:49:48.276 23703-23703/com.example.arege.dayatalisten W/System.err: at libcore.io.IoBridge.open(IoBridge.java:455)
at java.io.RandomAccessFile.<init>(RandomAccessFile.java:247)
at java.io.RandomAccessFile.<init>(RandomAccessFile.java:128)
at android.media.MediaRecorder.prepare(MediaRecorder.java:834)
at com.example.arege.dayatalisten.ListeningToTheWorld.onStartCommand(ListeningToTheWorld.java:47)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3432)
at android.app.ActivityThread.-wrap21(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1633)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6316)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
Caused by: android.system.ErrnoException: open failed: EROFS (Read-only file system)
11-11 01:49:48.277 23703-23703/com.example.arege.dayatalisten W/System.err: at libcore.io.Posix.open(Native Method)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:187)
at libcore.io.IoBridge.open(IoBridge.java:441)
... 13 more


I have both required permissions.










share|improve this question























  • Why are you catching the error from recorder.prepare() and just printing it and going ahead with recorder.start() ? If prepare() throws a exception, then the Media recorder is in a invalid state. Can you see what kind of exception in thrown my prepare? if it does, please post the stacktrace. Also make sure you have requested and received permission to write to external storage.
    – Abhijith Shivaswamy
    Nov 11 at 6:33











  • So I did the recorder.start() like that because thats how it is done in the documentation, so I was trying to be like that. I found the stack error - and posted it. Thank you for guiding me!
    – Gorgochef
    Nov 11 at 6:52












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a recorder I'm trying to make to test it out and see how it works. However when I run it I get the error:



E/MediaRecorder: start called in an invalid state: 4


So I did some googlejitzu and figured out that my file path is wrong. This is my code:



final MediaRecorder recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile("Test");

System.out.println("Hello");
try
recorder.prepare();
catch (IOException e)
e.printStackTrace();


recorder.start();
CountDownTimer countDownTimer = new CountDownTimer(30000, 1000)
@Override
public void onTick(long l)



@Override
public void onFinish()
recorder.stop();

.start();


So I guess I was wondering how to make a file path directory on the phone for the audio to be saved to.



The Error is:



 11-11 01:49:48.275 23703-23703/com.example.arege.dayatalisten W/System.err: java.io.FileNotFoundException: Test: open failed: EROFS (Read-only file system)
11-11 01:49:48.276 23703-23703/com.example.arege.dayatalisten W/System.err: at libcore.io.IoBridge.open(IoBridge.java:455)
at java.io.RandomAccessFile.<init>(RandomAccessFile.java:247)
at java.io.RandomAccessFile.<init>(RandomAccessFile.java:128)
at android.media.MediaRecorder.prepare(MediaRecorder.java:834)
at com.example.arege.dayatalisten.ListeningToTheWorld.onStartCommand(ListeningToTheWorld.java:47)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3432)
at android.app.ActivityThread.-wrap21(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1633)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6316)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
Caused by: android.system.ErrnoException: open failed: EROFS (Read-only file system)
11-11 01:49:48.277 23703-23703/com.example.arege.dayatalisten W/System.err: at libcore.io.Posix.open(Native Method)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:187)
at libcore.io.IoBridge.open(IoBridge.java:441)
... 13 more


I have both required permissions.










share|improve this question















I have a recorder I'm trying to make to test it out and see how it works. However when I run it I get the error:



E/MediaRecorder: start called in an invalid state: 4


So I did some googlejitzu and figured out that my file path is wrong. This is my code:



final MediaRecorder recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile("Test");

System.out.println("Hello");
try
recorder.prepare();
catch (IOException e)
e.printStackTrace();


recorder.start();
CountDownTimer countDownTimer = new CountDownTimer(30000, 1000)
@Override
public void onTick(long l)



@Override
public void onFinish()
recorder.stop();

.start();


So I guess I was wondering how to make a file path directory on the phone for the audio to be saved to.



The Error is:



 11-11 01:49:48.275 23703-23703/com.example.arege.dayatalisten W/System.err: java.io.FileNotFoundException: Test: open failed: EROFS (Read-only file system)
11-11 01:49:48.276 23703-23703/com.example.arege.dayatalisten W/System.err: at libcore.io.IoBridge.open(IoBridge.java:455)
at java.io.RandomAccessFile.<init>(RandomAccessFile.java:247)
at java.io.RandomAccessFile.<init>(RandomAccessFile.java:128)
at android.media.MediaRecorder.prepare(MediaRecorder.java:834)
at com.example.arege.dayatalisten.ListeningToTheWorld.onStartCommand(ListeningToTheWorld.java:47)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3432)
at android.app.ActivityThread.-wrap21(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1633)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6316)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
Caused by: android.system.ErrnoException: open failed: EROFS (Read-only file system)
11-11 01:49:48.277 23703-23703/com.example.arege.dayatalisten W/System.err: at libcore.io.Posix.open(Native Method)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:187)
at libcore.io.IoBridge.open(IoBridge.java:441)
... 13 more


I have both required permissions.







java android-studio






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 6:51

























asked Nov 11 at 5:48









Gorgochef

305




305











  • Why are you catching the error from recorder.prepare() and just printing it and going ahead with recorder.start() ? If prepare() throws a exception, then the Media recorder is in a invalid state. Can you see what kind of exception in thrown my prepare? if it does, please post the stacktrace. Also make sure you have requested and received permission to write to external storage.
    – Abhijith Shivaswamy
    Nov 11 at 6:33











  • So I did the recorder.start() like that because thats how it is done in the documentation, so I was trying to be like that. I found the stack error - and posted it. Thank you for guiding me!
    – Gorgochef
    Nov 11 at 6:52
















  • Why are you catching the error from recorder.prepare() and just printing it and going ahead with recorder.start() ? If prepare() throws a exception, then the Media recorder is in a invalid state. Can you see what kind of exception in thrown my prepare? if it does, please post the stacktrace. Also make sure you have requested and received permission to write to external storage.
    – Abhijith Shivaswamy
    Nov 11 at 6:33











  • So I did the recorder.start() like that because thats how it is done in the documentation, so I was trying to be like that. I found the stack error - and posted it. Thank you for guiding me!
    – Gorgochef
    Nov 11 at 6:52















Why are you catching the error from recorder.prepare() and just printing it and going ahead with recorder.start() ? If prepare() throws a exception, then the Media recorder is in a invalid state. Can you see what kind of exception in thrown my prepare? if it does, please post the stacktrace. Also make sure you have requested and received permission to write to external storage.
– Abhijith Shivaswamy
Nov 11 at 6:33





Why are you catching the error from recorder.prepare() and just printing it and going ahead with recorder.start() ? If prepare() throws a exception, then the Media recorder is in a invalid state. Can you see what kind of exception in thrown my prepare? if it does, please post the stacktrace. Also make sure you have requested and received permission to write to external storage.
– Abhijith Shivaswamy
Nov 11 at 6:33













So I did the recorder.start() like that because thats how it is done in the documentation, so I was trying to be like that. I found the stack error - and posted it. Thank you for guiding me!
– Gorgochef
Nov 11 at 6:52




So I did the recorder.start() like that because thats how it is done in the documentation, so I was trying to be like that. I found the stack error - and posted it. Thank you for guiding me!
– Gorgochef
Nov 11 at 6:52












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










You are trying to write to a internal storage to which your app does not have access. It's not a rooted device.



You need to write your file to the External storage.



Use either: getFilesDir() or getExternalFilesDir() (on Context or Activity) to get the directory and write the file to that location.



Psuedocode:



File file = new File(context.getFilesDir(), "Test");
FileOutputStream outputStream = new FileOutputStream(file);
outputStream.write("contents".getBytes());
outputStream.close();





share|improve this answer




















  • So I used: File file = new File(this.getFilesDir(), "Test"); recorder.setOutputFile(file); However this says that I need an API of 26 to use this method. Is there a work around for this for an API like 21?
    – Gorgochef
    Nov 11 at 7:31







  • 1




    on which method are you getting this message?
    – Abhijith Shivaswamy
    Nov 11 at 7:32










  • recorder.setOutputFile(file); Specifically. It seems like it just wants me to pass a string name into it
    – Gorgochef
    Nov 11 at 7:34






  • 1




    You'll need to either use setOutputFile(String path) or setOutputFile(FileDescriptor), you can use setOutputFile(file.getAbsolutePath())
    – Abhijith Shivaswamy
    Nov 11 at 7:38










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53246190%2finvalid-state-of-audio-recorder-bad-filepath%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










You are trying to write to a internal storage to which your app does not have access. It's not a rooted device.



You need to write your file to the External storage.



Use either: getFilesDir() or getExternalFilesDir() (on Context or Activity) to get the directory and write the file to that location.



Psuedocode:



File file = new File(context.getFilesDir(), "Test");
FileOutputStream outputStream = new FileOutputStream(file);
outputStream.write("contents".getBytes());
outputStream.close();





share|improve this answer




















  • So I used: File file = new File(this.getFilesDir(), "Test"); recorder.setOutputFile(file); However this says that I need an API of 26 to use this method. Is there a work around for this for an API like 21?
    – Gorgochef
    Nov 11 at 7:31







  • 1




    on which method are you getting this message?
    – Abhijith Shivaswamy
    Nov 11 at 7:32










  • recorder.setOutputFile(file); Specifically. It seems like it just wants me to pass a string name into it
    – Gorgochef
    Nov 11 at 7:34






  • 1




    You'll need to either use setOutputFile(String path) or setOutputFile(FileDescriptor), you can use setOutputFile(file.getAbsolutePath())
    – Abhijith Shivaswamy
    Nov 11 at 7:38














up vote
1
down vote



accepted










You are trying to write to a internal storage to which your app does not have access. It's not a rooted device.



You need to write your file to the External storage.



Use either: getFilesDir() or getExternalFilesDir() (on Context or Activity) to get the directory and write the file to that location.



Psuedocode:



File file = new File(context.getFilesDir(), "Test");
FileOutputStream outputStream = new FileOutputStream(file);
outputStream.write("contents".getBytes());
outputStream.close();





share|improve this answer




















  • So I used: File file = new File(this.getFilesDir(), "Test"); recorder.setOutputFile(file); However this says that I need an API of 26 to use this method. Is there a work around for this for an API like 21?
    – Gorgochef
    Nov 11 at 7:31







  • 1




    on which method are you getting this message?
    – Abhijith Shivaswamy
    Nov 11 at 7:32










  • recorder.setOutputFile(file); Specifically. It seems like it just wants me to pass a string name into it
    – Gorgochef
    Nov 11 at 7:34






  • 1




    You'll need to either use setOutputFile(String path) or setOutputFile(FileDescriptor), you can use setOutputFile(file.getAbsolutePath())
    – Abhijith Shivaswamy
    Nov 11 at 7:38












up vote
1
down vote



accepted







up vote
1
down vote



accepted






You are trying to write to a internal storage to which your app does not have access. It's not a rooted device.



You need to write your file to the External storage.



Use either: getFilesDir() or getExternalFilesDir() (on Context or Activity) to get the directory and write the file to that location.



Psuedocode:



File file = new File(context.getFilesDir(), "Test");
FileOutputStream outputStream = new FileOutputStream(file);
outputStream.write("contents".getBytes());
outputStream.close();





share|improve this answer












You are trying to write to a internal storage to which your app does not have access. It's not a rooted device.



You need to write your file to the External storage.



Use either: getFilesDir() or getExternalFilesDir() (on Context or Activity) to get the directory and write the file to that location.



Psuedocode:



File file = new File(context.getFilesDir(), "Test");
FileOutputStream outputStream = new FileOutputStream(file);
outputStream.write("contents".getBytes());
outputStream.close();






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 at 6:56









Abhijith Shivaswamy

3115




3115











  • So I used: File file = new File(this.getFilesDir(), "Test"); recorder.setOutputFile(file); However this says that I need an API of 26 to use this method. Is there a work around for this for an API like 21?
    – Gorgochef
    Nov 11 at 7:31







  • 1




    on which method are you getting this message?
    – Abhijith Shivaswamy
    Nov 11 at 7:32










  • recorder.setOutputFile(file); Specifically. It seems like it just wants me to pass a string name into it
    – Gorgochef
    Nov 11 at 7:34






  • 1




    You'll need to either use setOutputFile(String path) or setOutputFile(FileDescriptor), you can use setOutputFile(file.getAbsolutePath())
    – Abhijith Shivaswamy
    Nov 11 at 7:38
















  • So I used: File file = new File(this.getFilesDir(), "Test"); recorder.setOutputFile(file); However this says that I need an API of 26 to use this method. Is there a work around for this for an API like 21?
    – Gorgochef
    Nov 11 at 7:31







  • 1




    on which method are you getting this message?
    – Abhijith Shivaswamy
    Nov 11 at 7:32










  • recorder.setOutputFile(file); Specifically. It seems like it just wants me to pass a string name into it
    – Gorgochef
    Nov 11 at 7:34






  • 1




    You'll need to either use setOutputFile(String path) or setOutputFile(FileDescriptor), you can use setOutputFile(file.getAbsolutePath())
    – Abhijith Shivaswamy
    Nov 11 at 7:38















So I used: File file = new File(this.getFilesDir(), "Test"); recorder.setOutputFile(file); However this says that I need an API of 26 to use this method. Is there a work around for this for an API like 21?
– Gorgochef
Nov 11 at 7:31





So I used: File file = new File(this.getFilesDir(), "Test"); recorder.setOutputFile(file); However this says that I need an API of 26 to use this method. Is there a work around for this for an API like 21?
– Gorgochef
Nov 11 at 7:31





1




1




on which method are you getting this message?
– Abhijith Shivaswamy
Nov 11 at 7:32




on which method are you getting this message?
– Abhijith Shivaswamy
Nov 11 at 7:32












recorder.setOutputFile(file); Specifically. It seems like it just wants me to pass a string name into it
– Gorgochef
Nov 11 at 7:34




recorder.setOutputFile(file); Specifically. It seems like it just wants me to pass a string name into it
– Gorgochef
Nov 11 at 7:34




1




1




You'll need to either use setOutputFile(String path) or setOutputFile(FileDescriptor), you can use setOutputFile(file.getAbsolutePath())
– Abhijith Shivaswamy
Nov 11 at 7:38




You'll need to either use setOutputFile(String path) or setOutputFile(FileDescriptor), you can use setOutputFile(file.getAbsolutePath())
– Abhijith Shivaswamy
Nov 11 at 7:38

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53246190%2finvalid-state-of-audio-recorder-bad-filepath%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

Museum of Modern and Contemporary Art of Trento and Rovereto