簡體   English   中英

Android使用mediamuxer mediacodec和mediaextractor(沒有mp4parser)合並mp4和aac文件

[英]Android merging mp4 and aac file using mediamuxer mediacodec and mediaextractor (without mp4parser)

我正在錄制音頻,然后嘗試合並aac音頻和mp4視頻文件(靜音,沒有音頻),並共享合並的mp4文件。 合並的mp4文件的共享適用於三星J2和小米,但mp4文件不能在聯想,Micromax和其他手機中播放。測試結果如下: https//docs.google.com/spreadsheets/d/1eeJEM- v-smEUzY-bSxwIwFVOsAbv6KT2u3Kz3jdOb8o / edit?usp = sharing我無法理解問題是由於共享或由於不正確的多路復用以及問題的原因造成的。 請幫忙。

recordAudio.java中存在共享代碼,如下所示:

public void shareVroom(View view) {
//    Toast.makeText(this, "Share feature is temporarily disabled", android.widget.Toast.LENGTH_LONG).show();

//    Toast.makeText(this, "Share feature is enabled", android.widget.Toast.LENGTH_LONG).show();
        // Code commented for UAT

        try {
            MediaMultiplexer mediaMultiplexer = new MediaMultiplexer();
            mediaMultiplexer.startMuxing(this);

            Toast.makeText(this, "in share",Toast.LENGTH_SHORT).show();
            String shareableFileName = "";

            Intent intentShareFile = new Intent(Intent.ACTION_SEND);
            shareableFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
            shareableFileName += getString(R.string.vroom_video_output_file_name);
            File fileWithinMyDir = new File(shareableFileName);
            Uri videoUri=Uri.parse(shareableFileName);
            if (fileWithinMyDir.exists()) {
                intentShareFile.setType("video/mp4");
                intentShareFile.putExtra(Intent.EXTRA_STREAM, videoUri);
                intentShareFile.putExtra(Intent.EXTRA_SUBJECT, "Listen to my VROOM");
                intentShareFile.putExtra(Intent.EXTRA_TEXT, "Vroom attached");
                startActivity(Intent.createChooser(intentShareFile, "Share your Vroom with"));
            }

        } catch (IllegalStateException e) {
            e.printStackTrace();
            Log.e("tag", e.getMessage(), e);
            Toast.makeText(this, "could not shared"+e.getMessage(),Toast.LENGTH_SHORT).show();
        }


        //TODO:Use event to identify if muxing is done

    }

多路復用代碼:

public class MediaMultiplexer {
    private static final int MAX_SAMPLE_SIZE = 256 * 1024;

    public void startMuxing(Context context) {
        MediaMuxer muxer = null;
        MediaFormat VideoFormat = null;
        Resources mResources = context.getResources();
        int sourceVideo = R.raw.vid;
        String outputVideoFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
        outputVideoFileName += context.getString(R.string.vroom_video_output_file_name);
        try {
            muxer = new MediaMuxer(outputVideoFileName, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
        } catch (IOException e) {
            e.printStackTrace();
        }
        MediaExtractor extractorVideo = new MediaExtractor();
        try {
            AssetFileDescriptor srcVideoFd = mResources.openRawResourceFd(sourceVideo);
            extractorVideo.setDataSource(srcVideoFd.getFileDescriptor(), srcVideoFd.getStartOffset(), srcVideoFd.getLength());
            int tracks = extractorVideo.getTrackCount();
            for (int i = 0; i < tracks; i++) {
                MediaFormat mf = extractorVideo.getTrackFormat(i);
                String mime = mf.getString(MediaFormat.KEY_MIME);
                if (mime.startsWith("video/")) {
                    extractorVideo.selectTrack(i);
                    VideoFormat = extractorVideo.getTrackFormat(i);
                    break;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        MediaExtractor extractorAudio = new MediaExtractor();
        try {
            String audioFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
            audioFileName += context.getString(R.string.vroom_audio_file_name);
            extractorAudio.setDataSource(audioFileName);
            int tracks = extractorAudio.getTrackCount();
//            Toast.makeText(context, "No of tracks::::" + String.valueOf(tracks), Toast.LENGTH_SHORT).show();
            extractorAudio.selectTrack(0);

            MediaFormat AudioFormat = extractorAudio.getTrackFormat(0);
            int audioTrackIndex = muxer.addTrack(AudioFormat);
            int videoTrackIndex = muxer.addTrack(VideoFormat);

            boolean sawEOS = false;
            boolean sawAudioEOS = false;
            int bufferSize = MAX_SAMPLE_SIZE;
            ByteBuffer dstBuf = ByteBuffer.allocate(bufferSize);
            int offset = 100;
            MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
            muxer.start();

            while (!sawEOS) {
                bufferInfo.offset = offset;
                bufferInfo.size = extractorVideo.readSampleData(dstBuf, offset);
                if (bufferInfo.size < 0) {
                    sawEOS = true;
                    bufferInfo.size = 0;
                } else {
                    bufferInfo.presentationTimeUs = extractorVideo.getSampleTime();
                    bufferInfo.flags = extractorVideo.getSampleFlags();
                    int trackIndex = extractorVideo.getSampleTrackIndex();
                    muxer.writeSampleData(videoTrackIndex, dstBuf, bufferInfo);
                    extractorVideo.advance();
                }
            }
            ByteBuffer audioBuf = ByteBuffer.allocate(bufferSize);
            while (!sawAudioEOS) {
                bufferInfo.offset = offset;
                bufferInfo.size = extractorAudio.readSampleData(audioBuf, offset);
                if (bufferInfo.size < 0) {
                    sawAudioEOS = true;
                    bufferInfo.size = 0;
                } else {
                    bufferInfo.presentationTimeUs = extractorAudio.getSampleTime();
                    bufferInfo.flags = extractorAudio.getSampleFlags();
                    int trackIndex = extractorAudio.getSampleTrackIndex();
                    muxer.writeSampleData(audioTrackIndex, audioBuf, bufferInfo);
                    extractorAudio.advance();
                }
            }
            muxer.stop();
            muxer.release();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

}

將音頻輸出格式和編碼器更改為AMR_NB

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM