手机实时传输到电脑上的视频无法播放 50C

手机端:
package com.example.lenovo.viedoapp;

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

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

import java.io.File;
import java.io.IOException;

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;
/**
* 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();
           ;new Thread(new Send(videoFile)).start();
        } catch (IOException e) {

            e.printStackTrace();
        }
    } else if (v.getId() == but_stop.getId()) {
        send.flag=false;
        Toast.makeText(viedoApp.this, "完成", Toast.LENGTH_SHORT).show();
        mediaRecorder.stop();
        mediaRecorder.release();
        mediaRecorder = null;
        camera = Camera.open();
        try {
            camera.setPreviewDisplay(sfh);
            camera.setDisplayOrientation(90);
            camera.startPreview();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

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;

}

}

package com.example.lenovo.viedoapp;

import android.util.Log;

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

/**

  • Created by lenovo on 2017/4/16. */

class Send implements Runnable {
Socket socket = null;
OutputStream os = null;
File file = null;
FileInputStream fis = null;
byte[] bytes;
boolean flag = true;
DataOutputStream dos=null;
long d=0;
Send(File file) {
this.file = file;
}
String aa=new String("1");
public void connect() {
try {
socket = new Socket("192.168.1.106", 9991);
os = socket.getOutputStream();
dos = new DataOutputStream(os);
fis= new FileInputStream(file);

    } catch (IOException e) {
    }
    bytes = new byte[1024];
    int length=0;
        try {
            while(flag) {
                synchronized (aa) {
                    if ((length = fis.read(bytes, 0, bytes.length)) > 0) {
                        dos.write(bytes, 0, length);
                        dos.flush();
                    }
                }

            }
        } catch (IOException e) {
            e.printStackTrace();
        }

finally {
try {
dos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

public void run() {
    connect();
    file.delete();

}

}

电脑端:
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class bbbb{
public static void main(String[] args){
try {
ServerSocket ss=new ServerSocket(9991);
Socket socket=ss.accept();
System.out.print("hh");
new Thread(new oo(socket)).start();
} catch (IOException e) {
e.printStackTrace();
}
}
}

class oo implements Runnable{
;
Socket socket;
DataInputStream dis;
FileOutputStream fos;
oo(Socket socket){
this.socket=socket;
}
public void run() {
try {
dis=new DataInputStream(socket.getInputStream());
fos=new FileOutputStream(new File("C:/Users/lenovo/Desktop/test/video.mp4"));
byte[] bytes=new byte[1024];
int length=0;
while((length=dis.read(bytes,0,bytes.length))>0){
fos.write(bytes, 0, length);
fos.flush();
try {
new Thread().sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

    } catch (IOException e) {
        e.printStackTrace();
    }
    finally{
        if(fos!=null){      
            try {
                fos.flush();
                fos.close();
                System.out.print("接收完毕!");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

}
问题出在,虽然我能把手机实时采集到的视频传递给电脑,但是电脑接收以后的视频变小,并且无法播放,求求大神能帮忙一下!

3个回答

电脑接收以后的视频变小,并且无法播放,是不是文件格式、文件头损坏

qq_34316891
qq_34316891 但是我如果把视频先录制好,然后再发给电脑端,就完全正常啊,但是一边录制一边发送就会出现问题。麻烦大神帮我看一下,不胜感激啊!
接近 3 年之前 回复

不是二位说的问题,因为我之前是先把视频录制好之后再发送到电脑上就可以播放,但是现在一边录制一边传递视频到电脑上就会出问题,我怀疑是不是问题出在线程方面

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