视频流从Android而不保存在SD卡上



我有代码:

private MediaRecorder recorder;
String hostname = "192.168.1.125";
int port = 1935;
Socket socket;
ParcelFileDescriptor pfd;
public void start()
    {
        try {
            socket = new Socket(InetAddress.getByName(hostname), port);
            pfd = ParcelFileDescriptor.fromSocket(socket);
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
  recorder.setOutputFile(pfd.getFileDescriptor()); 
  //  String filename = String.format("/sdcard/%d.mp4", System.currentTimeMillis());
  // 
  // recorder.setOutputFile(filename);
    try
        {
        recorder.prepare();
            recorder.start();
        }
        catch (IllegalStateException e)
        {
        e.printStackTrace();
        }
        catch (IOException e)
        {
        e.printStackTrace();
        }
    }

服务器端:

import java.io.DataInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
        public static void main(String[] args) 
        {
            try
                {
                System.out.println("create sock");
                ServerSocket svsock = new ServerSocket(1935);
                System.out.println("accept");
                Socket sock = svsock.accept();
                System.out.println("buffer read");
                FileOutputStream outFile = null;
    String filename = String.format("%d.mp4", System.currentTimeMillis());
                   try {
                                        outFile = new FileOutputStream(filename);
                                        System.out.println(filename);
                                } catch (IOException e1) {
                                        e1.printStackTrace();
                                }
                  InputStream is = new DataInputStream(sock.getInputStream());
                                byte[] byteBuffer = new byte[1024];
                                int allsize = 0;
                                while(sock.isConnected()) {
                                    int size = is.read(byteBuffer);
                                        if (size == -1){
                                                break;
                                        } else {
                                                outFile.write(byteBuffer, 0, size);
                                        }
                                        allsize += size;
                                }
                                System.out.println("close size=" + allsize);
                                outFile.close();
                                sock.close();
                }
                catch(Exception e)
                {
                        e.printStackTrace();
                }       
                System.out.println("endmain");
        }
}

我在安卓2.2.2上测试了它(HTC安静的辉煌),一切都很好。当我按下"启动"按钮时,服务器会创建文件并将数据从流记录到文件。此文件在VLC播放器等中正常播放后

但当我在Android 4.0.4(Galaxy S2)上测试它时,服务器会创建文件并记录流到文件的数据,但不会在VLC(以及其他播放器)中播放,并给我错误

mp4错误:mp4插件被丢弃(没有moov、foov、moof框)avcodec错误:无法打开�编解码器解复用错误:指定的事件对象句柄无效ps错误:无法查看主要错误:没有适合`file/:///C:/1345461283455.mp4'的解复用模块

我也试着把这个文件上传到youtube,但上传后youtube给了我一个错误,比如文件格式不受支持。

但Android 4.0.4(Galaxy S2)成功创建并播放文件时,我将其保存在手机内存(而不是流到服务器)

我认为问题可能在服务器端,或者在android 4.0.4上发生了变化。

请帮帮我。提前谢谢。

尝试这个

recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setOutputFile(pfd.getFileDescriptor()); 

我认为它应该能解决问题。试试看是否有帮助。。。

最新更新