7

I'm trying to record audio via MediaRecorder class in Android, save it in a file and then play it with a MediaPlayer.

Here is where I choose the file where I want to save my audio file:

currentDate = Calendar.getInstance().getTime();
condensedDate = currentDate.toString().replaceAll("\\s", "")
fileName = getExternalCacheDir().getAbsolutePath();
fileName += File.separator + condensedDate + ".3gp";

and here I set it as the output file for MediaRecorder

Recorder.setOutputFile(fileName);

Then, in another Activity I use MediaPlayer to play that file audio:

MediaPlayer player = new MediaPlayer();
try {
     player.setDataSource(fileName);
     player.prepare();
     player.start();
} catch (IOException e) {
        e.printStackTrace();
}

This is where problems begin, nothing is played and this is my Log:

09-09 14:31:38.887 1522-26066/? E/FileSource: Failed to open file '/storage/emulated/0/Android/data/com.mycompany.dbmeter.pro/cache/SunSep0914:31:19GMT+00:002018.3gp'. (Permission denied)
09-09 14:31:38.887 1522-26066/? E/GenericSource: Failed to create data source!
09-09 14:31:38.887 25996-26064/com.mycompany.dbmeter.pro E/MediaPlayerNative: error (1, -2147483648)

This is strange because I followed step by step the official sample code that can be found here.

I tried making file world readable as suggested in this stack overflow post.

I also tried what was suggested here but nothing has changed.

What am I doing wrong?

Michele
  • 326
  • 8
  • 18
  • do you have `READ_EXTERNAL_STORAGE` in your manifrst and did you allowed it in your app? seems this cause to permission – Beginner Sep 10 '18 at 09:02
  • No I don't but I don't think I need it beacuse I save and read other files there without any problem.. Moreover, now it works.. Any idea why that file name could have caused the problem? (see my own answer) – Michele Sep 10 '18 at 09:15

2 Answers2

5

I solved the problem, for some reason the name of the file I was using caused that error.

I used to get a date, convert it to a string and eliminate all spaces; something like this:

SunSep0914:31:19GMT+00:002018.3gp

Now, while debugging my app I changed it to test.3gp and it works like a charm...

Michele
  • 326
  • 8
  • 18
  • I ran into the same problem, your solution worked, but I still want to use ":" in my file name, did you end up finding another solution? – Punpuf Feb 13 '19 at 16:38
  • 2
    I ended up changing ":" to "-" because it was the easiest solution for what I needed to do. I think ":" could be one of those characters that you simply cannot use in a file name – Michele Feb 13 '19 at 20:33
  • 1
    just found out the same in Huawei P20. There were no problems in any other device I tested on (Galaxy, Pixel, Xiomi) only in Huawei. Would have been good to know that you cannot use a colon : as a file name in Huawei. – Maverick Meerkat Feb 24 '19 at 16:19
0

Did you add permissions in your Manifest file

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

Source: https://developer.android.com/guide/topics/media/mediarecorder

Sambit Mallick
  • 155
  • 4
  • 14
  • I have permission to record audio, but not to write external storage. In that sample they don't use permission to write external storage and I'm getting the same error even when I try to save the file in a directory private to my app - getFilesDir() - where I definetely not need permission to write external storage – Michele Sep 09 '18 at 15:31
  • Correct me if I'm wrong! Isn't the private to your app directory exist in your phone storage? Moreover above android 6.0 you have to check permission in run time. – Sambit Mallick Sep 09 '18 at 15:47
  • I check record audio permission on runtime. The directory exists because I use it to store and retrieve correctly other stuff – Michele Sep 09 '18 at 16:14