qq_34316891 2017-04-22 05:52 采纳率: 100%
浏览 2214
已结题

手机实时录制实时传送到电脑端的视频无法播放

手机端:
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();
}
}
}
}
实时传送到电脑端的视频无法播放,并且也变小了,但是如果我先录制好视频然后再点击发送,那么传送到电脑端的视频就很正常,麻烦哪一位能看懂的,抽几分钟帮我看一下,在下不胜感激

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 素材场景中光线烘焙后灯光失效
    • ¥15 请教一下各位,为什么我这个没有实现模拟点击
    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 保护模式-系统加载-段寄存器