Advertisement
Guest User

Untitled

a guest
Jul 7th, 2015
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.33 KB | None | 0 0
  1. songOutPath = Environment.getExternalStorageDirectory()+"/My Folder/song.mp4";
  2. muxer = new MediaMuxer(songOutPath, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
  3. mTrackIndex = muxer.addTrack(format);
  4. muxer.start();
  5. mMuxerStarted = true;
  6.  
  7. MediaCodec.BufferInfo info = new MediaCodec.BufferInfo();
  8. final long timeoutUs = 5000;
  9. boolean sawInputEOS = false;
  10. boolean sawOutputEOS = false;
  11. int noOutputCounter = 0;
  12.  
  13. MediaCodec.BufferInfo info2 = new MediaCodec.BufferInfo();
  14. final long timeoutUs2 = 5000;
  15. boolean sawInputEOS2 = false;
  16. boolean sawOutputEOS2 = false;
  17. int noOutputCounter2 = 0;
  18.  
  19. while (!sawOutputEOS && !sawOutputEOS2 && noOutputCounter < 50 && noOutputCounter2 < 50) {
  20. noOutputCounter++;
  21. if (!sawInputEOS) {
  22. int inputBufferIndex = codec.dequeueInputBuffer(timeoutUs);
  23. if (inputBufferIndex >= 0) {
  24. ByteBuffer buffer = codecInputBuffers[inputBufferIndex];
  25. int sampleSize = extractor.readSampleData(buffer, 0);
  26. long presentationTimeUs = 0;
  27. if (sampleSize < 0) {
  28. sawInputEOS = true;
  29. sampleSize = 0;
  30. } else {
  31. presentationTimeUs = extractor.getSampleTime();
  32. }
  33. codec.queueInputBuffer(inputBufferIndex, 0, sampleSize,
  34. presentationTimeUs,
  35. sawInputEOS ? MediaCodec.BUFFER_FLAG_END_OF_STREAM : 0);
  36. if (!sawInputEOS) {
  37. extractor.advance();
  38. }
  39. }
  40. }
  41. noOutputCounter2++;
  42. if (!sawInputEOS2) {
  43. int inputBufferIndex2 = codec2.dequeueInputBuffer(timeoutUs2);
  44. if (inputBufferIndex2 >= 0) {
  45. ByteBuffer buffer2 = codecInputBuffers2[inputBufferIndex2];
  46. int sampleSize2 = extractor2.readSampleData(buffer2, 0);
  47. long presentationTimeUs2 = 0;
  48. if (sampleSize2 < 0) {
  49. sawInputEOS2 = true;
  50. sampleSize2 = 0;
  51. } else {
  52. presentationTimeUs2 = extractor2.getSampleTime();
  53. }
  54. codec2.queueInputBuffer(inputBufferIndex2, 0, sampleSize2,
  55. presentationTimeUs2,
  56. sawInputEOS2 ? MediaCodec.BUFFER_FLAG_END_OF_STREAM : 0);
  57. if (!sawInputEOS2) {
  58. extractor2.advance();
  59. }
  60. }
  61. }
  62.  
  63. int outputBufferIndex = codec.dequeueOutputBuffer(info, timeoutUs);
  64. int outputBufferIndex2 = codec2.dequeueOutputBuffer(info2, timeoutUs2);
  65. if (outputBufferIndex >= 0) {
  66. if (info.size > 0) {
  67. noOutputCounter = 0;
  68. }
  69. if (info2.size > 0) {
  70. noOutputCounter2 = 0;
  71. }
  72. ByteBuffer buffer = codecOutputBuffers[outputBufferIndex];
  73. ByteBuffer buffer2 = codecOutputBuffers2[outputBufferIndex2];
  74.  
  75. shortArrayOne = new short[info.size/2];
  76. shortArrayTwo = new short[info2.size/2];
  77.  
  78. buffer.order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(shortArrayOne);
  79. buffer.clear();
  80. buffer2.order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(shortArrayTwo);
  81. buffer2.clear();
  82.  
  83. shortArrayThree = mix(shortArrayOne, shortArrayTwo);
  84. if(shortArrayThree.length > 0){
  85. //audioTrack.write(shortArrayThree,0,shortArrayThree.length);
  86. ByteBuffer byteBuf = ByteBuffer.allocate(2*shortArrayThree.length);
  87. int index;
  88. for(index = 0; index != shortArrayThree.length; index++)
  89. {
  90. byteBuf.putShort(shortArrayThree[index]);
  91. }
  92. muxer.writeSampleData(mTrackIndex, byteBuf, info);
  93. }
  94.  
  95. codec.releaseOutputBuffer(outputBufferIndex, false);
  96. codec2.releaseOutputBuffer(outputBufferIndex2, false);
  97. if ((info.flags & MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0) {
  98. sawOutputEOS = true;
  99. }
  100. if ((info2.flags & MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0) {
  101. sawOutputEOS2 = true;
  102. }
  103. } else if (outputBufferIndex == MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED) {
  104. codecOutputBuffers = codec.getOutputBuffers();
  105. if (outputBufferIndex2 == MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED) {
  106. codecOutputBuffers2 = codec2.getOutputBuffers();
  107. }
  108. }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement