2011-01-25 637 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()); 
相關問題