0

In my Android app I am trying to record audio through the phone's mic, play it back, store it on Parse.com. The ultimate goal is to then allow other devices to stream the audio file.

I can successfully record the audio - here's how I do it: (the format is specified as .3gpp here, but .wav has been working for me too)

 mRecorder = new MediaRecorder();
 mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
 mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
 AudioRecordTest();
 mRecorder.setOutputFile(mFileName);
 mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

"mFileName" is set here:

 mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
 mFileName += "/audiorecordtest.3gpp";

I can also play the file back. That works fine. Then I try to save it to Parse as a Parse File, and attach it to a Parse Object. Like so:

 byte[] data = mFileName.getBytes();
 ParseUser currentUser = ParseUser.getCurrentUser();
 String usernameText = currentUser.getUsername();
 String mUploadName = usernameText + "_recording.3gpp";
 ParseFile file = new ParseFile(mUploadName, data);
 file.saveInBackground();
 ParseObject audioRecordings = new ParseObject("AudioRecording");
 audioRecordings.put("Submitter", usernameText);
 audioRecordings.put("AudioFile.3gpp", file);
 audioRecordings.put("PositiveScore", 0);
 audioRecordings.put("Negativescore", 0);
 audioRecordings.put("Parent", currentUser);
 audioRecordings.saveInBackground();

Now, I can see the Object and File on Parse.com's data browser. If I click on the audio file, I am taken to a audio player (so Parse recognizes it as audio, at least), but it won't play anything. So there's something wrong with the file.

Here's what I'm using to stream audio:

 mPlayer = new MediaPlayer();
 ParseQuery<ParseObject> query = ParseQuery.getQuery("AudioRecording");
 query.getInBackground("YkfHDl2aEG", new GetCallback<ParseObject>() {
      @Override
      public void done(ParseObject recording, com.parse.ParseException e) {
             if (e != null) { ... }
             else {
                  ParseFile audioFile = recording.getParseFile("audioFile");
                  ParseFile audioFile = recording.getParseFile("audioFile"); 
                  String audioFileURL = audioFile.getUrl();
                  mPlayer = new MediaPlayer();
                  mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                  try { mPlayer.setDataSource(audioFileURL); } 

Then, I play the sound. I know I've got that part right because if I replace audioFileURL with this "http://www.pocketjourney.com/downloads/pj/tutorials/audio.mp3", it works fine. It's the Parse.com URL that's breaking it. This is the logcat error:

 08-05 01:15:12.620: E/WVMExtractor(55): Failed to open libwvm.so
 08-05 01:15:12.620: E/MediaPlayer(875): error (1, -2147483648)

This is tough to debug because apparently it corresponds to this unhelpful error: MediaPlayer.MEDIA_ERROR_UNKNOWN. (according to this question Android MediaPlayer error (1, -2147483648))

Any advice would be appreciated! Thanks

Community
  • 1
  • 1
  • Have you tried getting it to log the URL then copy/pasting that into your browser to see if it likes the file? – Timothy Walters Aug 05 '14 at 07:22
  • @TimothyWalters Yes, viewing the file through Parse.com's Data Browser allows you to click on the file, which takes you to the URL for the file. This is where I see the broken audio player (Parse recognizes it as an audio file, but apparently it is an unplayable one). I have logged the URL through the app, and it matches up with the URL on Parse.com – David Emelianov Aug 05 '14 at 14:01
  • Could just be that you're missing the MIME type, the optional 3rd parameter when creating the ParseFile. – Timothy Walters Aug 05 '14 at 20:30

1 Answers1

1

I changed the way that I was getting the byte data from the audio recording. This is the code I used instead:

            File file = new File(mFileName);
                byte[] byteArray = new byte[(int) file.length()];
                try {
                    FileInputStream fileInputStream = new FileInputStream(file);
                    fileInputStream.read(byteArray);
                    for (int i = 0; i < byteArray.length; i++) {
                        System.out.print((char)byteArray[i]);
                    }
                } catch (FileNotFoundException e) {
                   System.out.println("File Not Found.");
                   e.printStackTrace();
                }
                catch (IOException e1) {
                   System.out.println("Error Reading The File.");
                   e1.printStackTrace();
                }

I also ended up recording my file as a .mp4 Doing this required the following changes in my code:

  mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
  mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);

I also had to change the file extensions to .mp4 instead of .3gpp

It works now but the sound is kind of crappy