2017-06-20 2 views
0

私はオーディオファイルをマージするためにライブラリを使用しています。 rawのリソースに格納されている外部オーディオmp3ファイルがあります。生のフォルダからファイルを読み込むmp3オーディオファイルでcom.googlecode.mp4parserが失敗しますか?

:映画(失敗して)読み込む

InputStream is = context.getResources().openRawResource(R.raw.my_mp3_file); 
     OutputStream output = null; 
     try { 
      File file = new File(context.getFilesDir(), "silence.mp3"); 
      if(!file.exists()) { 
       file.createNewFile(); 
      } 
      output = new FileOutputStream(file); 
      byte[] buffer = new byte[4 * 1024]; // or other buffer size 
      int read; 

      while ((read = is.read(buffer)) != -1) { 
       output.write(buffer, 0, read); 
      } 
      output.flush(); 
      output.close(); 
      fileReference= file; 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (Exception e) { 
      e.printStackTrace(); // handle exception, define IOException and others 
     } finally { 
      try { 
       is.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 

コード:

if(fileReference.exists()) { 
        Movie m = new MovieCreator().build(fileReference.getAbsolutePath()); 
       } 

このファイルは私のコードで以下では、以下の例外が原因にマージに失敗しますこれを取得中にMovie m私のコードは例外をスローに失敗します:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List com.coremedia.iso.boxes.MovieBox.getBoxes(java.lang.Class)' on a null object reference 

一部のmp3ファイルがrawリソースファイルに失敗しますか?ここで何が間違っていますか?ここで

+0

でいますか? –

答えて

0

のみ.M4A拡張子を使用

String root = Environment.getExternalStorageDirectory().toString(); 
      String audio = root + "/" + "tests.m4a"; 
      String video = root + "/" + "output.mp4"; 
      String output = root + "/" + "aud_vid.mp4"; 
      mux(video, audio, output); 

、ここでオーディオとビデオをマージするための多くの研究 MP4Parser後の私の結論とソリューションは、任意の解決策を見つけた方法

public boolean mux(String videoFile, String audioFile, String outputFile) { 
    Movie video; 
    try { 
     video = new MovieCreator().build(videoFile); 
    } catch (RuntimeException e) { 
     e.printStackTrace(); 
     return false; 
    } catch (IOException e) { 
     e.printStackTrace(); 
     return false; 
    } 

    Movie audio; 
    try { 
     audio = new MovieCreator().build(audioFile); 
    } catch (IOException e) { 
     e.printStackTrace(); 
     return false; 
    } catch (NullPointerException e) { 
     e.printStackTrace(); 
     return false; 
    } 

    Track audioTrack = audio.getTracks().get(0); 
    video.addTrack(audioTrack); 

    Container out = new DefaultMp4Builder().build(video); 

    FileOutputStream fos; 
    try { 
     fos = new FileOutputStream(outputFile); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
     return false; 
    } 
    BufferedWritableFileByteChannel byteBufferByteChannel = new BufferedWritableFileByteChannel(fos); 
    try { 
     out.writeContainer(byteBufferByteChannel); 
     byteBufferByteChannel.close(); 
     fos.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
     return false; 
    } 
    return true; 
} 

private static class BufferedWritableFileByteChannel implements WritableByteChannel { 
    private static final int BUFFER_CAPACITY = 1000000; 

    private boolean isOpen = true; 
    private final OutputStream outputStream; 
    private final ByteBuffer byteBuffer; 
    private final byte[] rawBuffer = new byte[BUFFER_CAPACITY]; 

    private BufferedWritableFileByteChannel(OutputStream outputStream) { 
     this.outputStream = outputStream; 
     this.byteBuffer = ByteBuffer.wrap(rawBuffer); 
    } 

    @Override 
    public int write(ByteBuffer inputBuffer) throws IOException { 
     int inputBytes = inputBuffer.remaining(); 

     if (inputBytes > byteBuffer.remaining()) { 
      dumpToFile(); 
      byteBuffer.clear(); 

      if (inputBytes > byteBuffer.remaining()) { 
       throw new BufferOverflowException(); 
      } 
     } 

     byteBuffer.put(inputBuffer); 

     return inputBytes; 
    } 

    @Override 
    public boolean isOpen() { 
     return isOpen; 
    } 

    @Override 
    public void close() throws IOException { 
     dumpToFile(); 
     isOpen = false; 
    } 

    private void dumpToFile() { 
     try { 
      outputStream.write(rawBuffer, 0, byteBuffer.position()); 
     } catch (IOException e) { 
      throw new RuntimeException(e); 
     } 
    } 
} 
関連する問題