Android传输基于socket传输视频文件到pc

我写了一个录制视频的app然后把录制的视频传送到pc端,但是传输到pc端的视频文件变大,而且无法播放,传输方式是DataOutputStream,具体代码如下;

package com.example.lenovo.viedoapp;

import android.app.Activity;
import android.hardware.Camera;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;

import com.google.android.gms.common.api.GoogleApiClient;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;

import static com.example.lenovo.viedoapp.R.id.surfaceView;

public class viedoApp extends Activity implements View.OnClickListener, SurfaceHolder.Callback {
Button but_start;
Button but_stop;
Button but_send;
SurfaceView sfv;
SurfaceHolder sfh;
Camera camera;
MediaRecorder mediaRecorder;
File videoFile;
File aduioFile;
Send send;
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_viedo_app);
    but_start = (Button) findViewById(R.id.but_start);
    but_start.setOnClickListener(this);
    but_stop = (Button) findViewById(R.id.but_stop);
    but_stop.setOnClickListener(this);
    but_send=(Button)findViewById(R.id.but_send);
    but_send.setOnClickListener(this);
    sfv = (SurfaceView) findViewById(surfaceView);
    sfh = sfv.getHolder();
    sfh.addCallback(this);
}

public void onClick(View v) {
    if (v.getId() == but_start.getId()) {
        camera.stopPreview();
        camera.release();
        camera = null;
        mediaRecorder = new MediaRecorder();
        mediaRecorder.setPreviewDisplay(sfh.getSurface());
        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
        mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        mediaRecorder.setVideoSize(320, 240);
        mediaRecorder.setVideoFrameRate(15);
        mediaRecorder.setOrientationHint(90);
        videoFile = new File(getExternalCacheDir(), "video.mp4");
        mediaRecorder.setOutputFile(videoFile.getAbsolutePath());
        try {
            mediaRecorder.prepare();
            mediaRecorder.start();
            //send = new Send(videoFile);
            //new Thread(send).start();

        } catch (IOException e) {

            e.printStackTrace();
        }
    } else if (v.getId() == but_stop.getId()) {
        //send.flag = false;
        mediaRecorder.stop();
        mediaRecorder.release();
        mediaRecorder = null;
        camera = Camera.open();
        try {
            camera.setPreviewDisplay(sfh);
            camera.setDisplayOrientation(90);
            camera.startPreview();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    else if(v.getId()==but_send.getId()){
        send = new Send(videoFile);
        new Thread(send).start();

       // Toast.makeText(viedoApp.this, "完成", Toast.LENGTH_SHORT).show();
    }
}

public void surfaceCreated(SurfaceHolder holder) {

    camera = Camera.open();

    try {
        camera.setPreviewDisplay(holder);
        camera.setDisplayOrientation(90);
        camera.startPreview();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

}

public void surfaceDestroyed(SurfaceHolder holder) {
    camera.stopPreview();
    camera.release();
    camera = null;
    sfv = null;
    sfh = null;
    mediaRecorder = null;

}

}

class Send implements Runnable {
    Socket socket = null;
    OutputStream os = null;
    File file = null;
    FileInputStream fis = null;
    byte[] bytes;
    boolean flag = true;
    DataOutputStream dos=null;
    Send(File file) {
        this.file = file;
    }

    public void connect() {
        try {
            socket = new Socket("192.168.1.108", 9991);
            os = socket.getOutputStream();
           dos = new DataOutputStream(os);
           fis=   new FileInputStream(file);
            String a = file.getAbsolutePath();
           // Log.d("Send", a);

        } catch (IOException e) {
        }
        bytes = new byte[1024];
        long a=0;
        String b=null;
        try {
            while ( fis.read(bytes, 0, bytes.length)>0) {
                dos.write(bytes,0,bytes.length);
                dos.flush();
                a=(a+bytes.length);
            }
            b=a+"";
            Log.v("Send",b);
           // Log.v("Send",a.toString());
            System.out.print(b);
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally{
            if(fis!=null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(dos!=null){
                try {
                    dos.flush();
                    dos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
            }
    }

    public void run() {
        connect();
    }

}

求求懂得大神给我解答一下,急需

2个回答

哪里 有问题呢???

qq_34316891
qq_34316891 就是把手机录制的视频传输到pc端时,视频无法播放而文件也变大了。
3 年多之前 回复

我写了一个录制视频的app然后把录制的视频传送到pc端,但是传输到pc端的视频文件变大,而且无法播放

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐