简体   繁体   中英

MediaPlayer.create() returns null on Android 2.3.7

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,

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM