2011-01-25 640 views
27

我正在开发视频流应用程序,并且在使用FileDescriptor调用set setDataSource时出现卡住现象。 我希望我的应用程序在下载视频时播放视频,因此一旦我获得了最少的字节数,我就会将这些字节移动到另一个文件,以便它可以在原始文件中下载时在另一个文件中播放。调用setDataSource(FileDescriptor)方法时发生异常(失败:状态= 0x80000000)

所以,我检查,如果我能启动媒体在线播放每一个数据包是这样的:

if (mediaPlayer == null) { 
        // Only create the MediaPlayer once we have the minimum 
        // buffered data 
        if (totalKbRead >= INTIAL_KB_BUFFER) { 
         try { 
          startMediaPlayer(); 
         } catch (Exception e) { 
          Log.e(getClass().getName(), 
            "Error copying buffered conent.", e); 
         } 
        } 
       } else if (mediaPlayer.getDuration() 
         - mediaPlayer.getCurrentPosition() <= 1000) { 
        transferBufferToMediaPlayer(); 
       } 
} 

这是startMediaPlayer方法代码:

private void startMediaPlayer() { 
     try { 
      File bufferedFile = new File(context.getCacheDir(), "playingMedia" 
        + (counter++) + ".dat"); 

      // bufferedFile is the one that'll be played 
      moveFile(downloadingMediaFile, bufferedFile); 

      mediaPlayer = createMediaPlayer(bufferedFile); 

      mediaPlayer.start(); 

      playButton.setEnabled(true); 
     } catch (IOException e) { 
      Log.e(getClass().getName(), "Error initializing the MediaPlayer.", 
        e); 
      return; 
} 

我谨用下面的代码文件:

public void moveFile(File oldLocation, File newLocation) throws IOException { 

     if (oldLocation.exists()) { 
      BufferedInputStream reader = new BufferedInputStream(
        new FileInputStream(oldLocation)); 
      BufferedOutputStream writer = new BufferedOutputStream(
        new FileOutputStream(newLocation, false)); 
      try { 
       byte[] buff = new byte[8192]; 
       int numChars; 
       while ((numChars = reader.read(buff, 0, buff.length)) != -1) { 
        writer.write(buff, 0, numChars); 
       } 
      } catch (IOException ex) { 
       throw new IOException("IOException when transferring " 
         + oldLocation.getPath() + " to " 
         + newLocation.getPath()); 
      } finally { 
       try { 
        if (reader != null) { 
         writer.flush(); 
         writer.close(); 
         reader.close(); 
        } 
       } catch (IOException ex) { 
        Log.e(getClass().getName(), 
          "Error closing files when transferring " 
            + oldLocation.getPath() + " to " 
            + newLocation.getPath()); 
       } 
      } 
     } else { 
      throw new IOException(
        "Old location does not exist when transferring " 
          + oldLocation.getPath() + " to " 
          + newLocation.getPath()); 
     } 
    } 
} 

我终于在这里创建了MediaPlayer对象:

private MediaPlayer createMediaPlayer(File mediaFile) throws IOException { 

     if(mediaPlayer != null){ 
      mediaPlayer.release(); 
     } 

     MediaPlayer mPlayer = new MediaPlayer(); 
     mPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() { 
      public boolean onError(MediaPlayer mp, int what, int extra) { 
       Log.e(getClass().getName(), "Error in MediaPlayer: (" + what 
         + ") with extra (" + extra + ")"); 
       return false; 
      } 
     }); 

     // It appears that for security/permission reasons, it is better to pass 
     // a FileDescriptor rather than a direct path to the File. 
     // Also I have seen errors such as "PVMFErrNotSupported" and 
     // "Prepare failed.: status=0x1" if a file path String is passed to 
     // setDataSource(). 
     FileInputStream fis = new FileInputStream(mediaFile); 

     mPlayer.reset(); 
     FileDescriptor fd = fis.getFD(); 
     mPlayer.setDataSource(fd);  // IM GETTING THE EXCEPTION HERE 
     mPlayer.setDisplay(mHolder); 
     mPlayer.prepare(); 
     return mPlayer; 
    } 

这是excepction我得到:

01-25 16:03:15.663: ERROR/org.pfc.utils.StreamingMediaPlayer(2229): Error initializing the MediaPlayer. 
01-25 16:03:15.663: ERROR/org.pfc.utils.StreamingMediaPlayer(2229): java.io.IOException: setDataSourceFD failed.: status=0x80000000 
01-25 16:03:15.663: ERROR/org.pfc.utils.StreamingMediaPlayer(2229):  at android.media.MediaPlayer.setDataSource(Native Method) 
01-25 16:03:15.663: ERROR/org.pfc.utils.StreamingMediaPlayer(2229):  at android.media.MediaPlayer.setDataSource(MediaPlayer.java:854) 
01-25 16:03:15.663: ERROR/org.pfc.utils.StreamingMediaPlayer(2229):  at org.pfc.utils.StreamingMediaPlayer.createMediaPlayer(StreamingMediaPlayer.java:266) 
01-25 16:03:15.663: ERROR/org.pfc.utils.StreamingMediaPlayer(2229):  at org.pfc.utils.StreamingMediaPlayer.startMediaPlayer(StreamingMediaPlayer.java:226) 
01-25 16:03:15.663: ERROR/org.pfc.utils.StreamingMediaPlayer(2229):  at org.pfc.utils.StreamingMediaPlayer.access$4(StreamingMediaPlayer.java:203) 
01-25 16:03:15.663: ERROR/org.pfc.utils.StreamingMediaPlayer(2229):  at org.pfc.utils.StreamingMediaPlayer$2.run(StreamingMediaPlayer.java:183) 
01-25 16:03:15.663: ERROR/org.pfc.utils.StreamingMediaPlayer(2229):  at android.os.Handler.handleCallback(Handler.java:587) 
01-25 16:03:15.663: ERROR/org.pfc.utils.StreamingMediaPlayer(2229):  at android.os.Handler.dispatchMessage(Handler.java:92) 
01-25 16:03:15.663: ERROR/org.pfc.utils.StreamingMediaPlayer(2229):  at android.os.Looper.loop(Looper.java:144) 
01-25 16:03:15.663: ERROR/org.pfc.utils.StreamingMediaPlayer(2229):  at android.app.ActivityThread.main(ActivityThread.java:4937) 
01-25 16:03:15.663: ERROR/org.pfc.utils.StreamingMediaPlayer(2229):  at java.lang.reflect.Method.invokeNative(Native Method) 
01-25 16:03:15.663: ERROR/org.pfc.utils.StreamingMediaPlayer(2229):  at java.lang.reflect.Method.invoke(Method.java:521) 
01-25 16:03:15.663: ERROR/org.pfc.utils.StreamingMediaPlayer(2229):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
01-25 16:03:15.663: ERROR/org.pfc.utils.StreamingMediaPlayer(2229):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
01-25 16:03:15.663: ERROR/org.pfc.utils.StreamingMediaPlayer(2229):  at dalvik.system.NativeStart.main(Native Method) 

我一直在整个上午呆在这里,我真的发现该错误的信息。有些人告诉我要使用文件路径,但是我在注释中提到了另一个异常(在FileInputStream创建的正上方)。

我真的失去了这里,任何帮助将是非常赞赏

回答

21

好吧,我已经到了这样的结论:这样的错误:

准备失败:状态=为0x1(调用时制备())

setDataSourceFD失败:状态= 0x80000000的(当CA灌装setDataSourceFD())

都与文件格式,可能意味着该文件不完整,损坏或类似的东西...

正如我在后链接this,我已经发现了一个流式传输的特定视频(尽管我使用setDataSource,而不是setDataSourceFD),但它不适用于大多数视频。

0

从我已阅读的文件中,某些视频文件格式的文件末尾有“标题”信息。因此,您的FD必须支持查找功能才能从文件末尾获取“标题”。我怀疑你的输入文件到媒体播放器失败时,它寻求文件的“结束”。

我们正在进一步研究相同的问题吗?

肖恩

0

在我的情况下从wav文件转换成mp3解决了这一例外,状态= 0x80000000的

+0

我在MP3文件中获取同样的错误。如何解决它? – 2017-03-17 07:08:40

33

不要忘记许可

<uses-permission android:name="android.permission.INTERNET" /> 
+0

非常有用。谢谢 !! – rajneesh 2013-04-24 16:47:11

0

在我的情况下,问题是因为beasy的sdcard当设备作为外置存储器挂载到PC时,检查文件是否可用解决了问题。也许它可以帮助某人

1

具有相同的错误,并阅读了上述文件格式的答案,我放弃了试图用我的.mov文件设置数据源,而是用我的Android Telefon Camera创建了一个视频,它给了我一个.mp4文件。 我把这个放在目录Pictures /中。 这工作 - 我cound setDataSource没有错误。 我希望这对某人有用。

File mediaStorageDir = new   File(Environment.getExternalStoragePublicDirectory(
       Environment.DIRECTORY_PICTURES),  "MyDirectoryUnderPictures"); 
File mediaFile_mp4_android; 
mediaFile_mp4_android = new File(mediaStorageDir.getPath() 
        + File.separator 
        + "mp4_test" 
        + ".mp4"); //video taken with android camera 
String filePath_mp4_android = String.valueOf(mediaFile_mp4_android); 
File file_mp4_android = new File(filePath_mp4_android); 
Uri contentUri = Uri.fromFile(file_mp4_android); 
MediaMetadataRetriever mmr = new MediaMetadataRetriever(); 
mmr.setDataSource(String.valueOf(contentUri)); 
0

如果你的目标棉花糖或更高,请确保您所请求的Manifest.permission.WRITE_EXTERNAL_STORAGE权限正常。我尝试了许多不同的解决方案,其中包括另一个库,它是MediaMetadataRetriever的替代品,但事实证明,我的一个代码路径没有请求适当的权限。

0

从obb扩展文件加载视频时,我遇到了同样的问题。 我固定它通过更换:

mPlayer.setDataSource(fd); 

有:

mPlayer.setDataSource(fis.getFileDescriptor(),fis.getStartOffset(),fis.getLength()); 
相关问题