0

I'm working on an application which supposed to run on devices from API 8 to latest.

Actually I'm dealing with Mediaplayer. the code is in a fragment and is simply:

MediaPlayer mediaPlayer = null; if (mediaPlayer = MediaPlayer.create(getActivity(), myAudioFileUri) != null) { . . . }

This code perfectly works on Android 4.4.2, MediaPlayer.create() returns a valid value and I can use Mediaplayer without problem.

Unfortunately, MediaPlayer.create() returns null on Android 2.3.7. this is my problem and I didn't find on Internet a reason why it could cause problem this Android version neither a difference in the way to use it.

Both tests have benn done on GenyMotion emulator as I don't have such an old Android device.

Edit: So I verified using the shell adb that the problem really comes from mp3 file permissions if I "chmod 777 myfile.mp3", I can succesfully read it.

My problem now is to know how to change permissions on Android 2.3

The code used to download the file from my remote server to copy it locally is the next one:

private Uri downloadFileFromURL(URL url, String fileName) {
    try {
      URLConnection conn = url.openConnection();
      HttpURLConnection httpConnection = conn instanceof HttpURLConnection ? (HttpURLConnection ) conn  : null;
    int responseCode = httpConnection.getResponseCode();

    if (responseCode == HttpURLConnection.HTTP_OK){

        int len, length = 0;
        byte[] buf = new byte[8192];
        InputStream is = httpConnection.getInputStream();
        File file = new File(getActivity().getApplicationContext().getFilesDir().getParentFile().getPath(), fileName);
        OutputStream os = new FileOutputStream(file);
        try {
          while((len = is.read(buf, 0, buf.length)) > 0) {
            os.write(buf, 0, len);
            length += len;
          }
          os.flush();
        }
        finally {
          is.close();
          os.close();
        }

        String chmodString = "chmod 777 " + getActivity().getApplicationContext().getFilesDir().getParentFile().getPath() +"/" + fileName;
        Process sh = Runtime.getRuntime().exec("su", null, new File("/system/bin/"));
        OutputStream osChgPerms = sh.getOutputStream();
        osChgPerms.write((chmodString).getBytes("ASCII"));
        osChgPerms.flush();
        osChgPerms.close();
        try {
            sh.waitFor();
        } catch (InterruptedException e) {
            Log.d("2ndGuide", "InterruptedException." + e);
        }

       return Uri.fromFile(file);
      }
    }
    catch(IOException e)
    {
        Log.d("2ndGuide", "IO Exception." + e);
    }
    return null;
}

But osChgPerms.write((chmodString).getBytes("ASCII")); generates an IOException: broken pipe. I suppose I didn't understand how to execute the command.

What's wrong? Regards,

fralbo
  • 2,534
  • 4
  • 41
  • 73

2 Answers2

0

I can point you 2 possible reasons behind that, not sure whether they can solve your issue.

  • Android can only allocate a certain amount of MediaPlayer objects, you need to release any MediaPlayer object by using mediaPlayer.release().

  • Android supports only 8- and 16-bit linear PCM, so check you audio file. More: Supported Media Formats

ridoy
  • 6,274
  • 2
  • 29
  • 60
  • Hello ridoy, thanks for your help but I get the error even on the first call. and according Supported Media formats, mp3 is supported from the begining in the same way so if it works in Android 4, it should work in android 2. I tried to use setDataSource instead of calling helper create() method. When I call start(), it continuously calls OnCompletion method tells me that I call start() in state 0, I didn't find in the doc a relationship between states and values. It also give me the error(-38,0) and again, I didn't find the signification. If you have some infos about it.... Thanks – fralbo Jul 16 '14 at 22:16
  • @AlainBo, -38 means invalid operation. You need to call mediaPlayer.start() in the onPrepared method by using a listener.For example see these: http://stackoverflow.com/questions/17724987/android-mediaplayer-error-38-0-stop-called-in-state-0, http://stackoverflow.com/questions/18482018/mediaplayer-error-38-0, http://stackoverflow.com/questions/9008770/media-player-called-in-state-0-error-38-0 – ridoy Jul 17 '14 at 03:57
  • The problem I don't understand is that in 2.3.7, the onCompletion method is call immediately after it starts! That's not the case in 4.4.2! – fralbo Jul 18 '14 at 18:50
  • I now have more infos I didn't take care about first. – fralbo Jul 20 '14 at 19:09
  • I now have more infos I didn't take care about first. So it works on 4.4.2 but on 2.3.7, Mediaplayer.prepare() returns error (1, -2147483648). So I found a possible explanation here found in an other thread http://www.weston-fl.com/blog/?p=2988. I effectively have possible wrong file permissions as my mp3 is -rw---------. But I'm running the app on API level 8 device so I'm not sure to be concern but above I cannot set the permissions on API8 but only on API9 so, if this is my problem, how can I do? – fralbo Jul 20 '14 at 20:37
  • Ok, but I don't want to change the target sdk as I need to run the application on Android 2.3 – fralbo Jul 21 '14 at 16:56
0

So in fact the problem clearly comes from the fact that the media files must be readable for everybody to be readable by the media player. This behaviour only occurs on pre HONEYCOMB devices.

fralbo
  • 2,534
  • 4
  • 41
  • 73