简体   繁体   English

MediaPlayer.create()在Android 2.3.7上返回null

[英]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. 我正在开发一个应在从API 8到最新版本的设备上运行的应用程序。

Actually I'm dealing with Mediaplayer. 实际上,我正在处理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. 此代码在Android 4.4.2上完美运行,MediaPlayer.create()返回有效值,我可以毫无问题地使用Mediaplayer。

Unfortunately, MediaPlayer.create() returns null on Android 2.3.7. 不幸的是,MediaPlayer.create()在Android 2.3.7上返回null。 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. 这是我的问题,我没有在互联网上找到它可能导致此Android版本出现问题的原因,也没有使用方式上的差异。

Both tests have benn done on GenyMotion emulator as I don't have such an old Android device. 这两个测试都在GenyMotion模拟器上完成,因为我没有这么老的Android设备。

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. 编辑:所以我使用外壳程序adb验证了如果我“ chmod 777 myfile.mp3”,问题确实出在mp3文件权限上,我可以成功读取它。

My problem now is to know how to change permissions on Android 2.3 我现在的问题是知道如何更改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")); 但是osChgPerms.write((chmodString).getBytes(“ ASCII”)); generates an IOException: broken pipe. 生成IOException:管道断开。 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只能分配一定数量的MediaPlayer对象,您需要使用mediaPlayer.release()释放任何MediaPlayer对象。

  • Android supports only 8- and 16-bit linear PCM, so check you audio file. Android仅支持8位和16位线性PCM,因此请检查音频文件。 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. 仅在HONEYCOMB之前的设备上会发生此行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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