- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试开发保存视频的应用程序,并且该视频可以在电脑上观看。我在手机上使用android,在pc上使用java。 pc 内容在服务器端,传输是通过套接字进行的。我的问题似乎是我可以录制视频,但 pc 端应用程序无法重现发送的视频。`我向您展示了我用于设置 MediaRecorder 的代码:
public void prepareVideoRecorder(Camera mCamera, ParcelFileDescriptor pfd,
SurfaceHolder mHolder) {
if (mCamera == null) {
mCamera = safeCameraOpen(mCamera);
}
if (mMediaRecorder == null) {
mMediaRecorder = new MediaRecorder();
mCamera.stopPreview();
// Step 1: unlock and set camera to MediaRecorder;
mCamera.unlock();
mMediaRecorder.setCamera(mCamera);
}
// Step 2: Set sources:
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
//mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
// Step 3:Set a CamcorderProfile (APi level 8 or higher)
mMediaRecorder.setProfile(CamcorderProfile
.get(CamcorderProfile.QUALITY_HIGH));
// Step 4: Set output file
mMediaRecorder.setOutputFile(pfd.getFileDescriptor());
// Step 5: Set the preview output
mMediaRecorder.setPreviewDisplay(mHolder.getSurface());
try {
mMediaRecorder.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
看来是对的。然后用 xuggler 开发应该播放视频的 pc 端,应用程序停止在:
if (container.open(inputstream, null) < 0) {
throw new IllegalArgumentException("could not open inpustream");
}
这是下一个 java 类的一部分:
public class imagePnl extends JPanel {
URL medialocator = null;
BufferedImage image;
private Player player;
private DataSource ds = null;
private String mobileLocation = "socket://localhost:1234";
// private ByteArrayDataSource byteDs = null;
private InputStream inputStream = null;
IContainerFormat format;
public imagePnl() {
}
public void setVideo(InputStream inputstream) {
// Let's make sure that we can actually convert video pixel formats.
if (!IVideoResampler
.isSupported(IVideoResampler.Feature.FEATURE_COLORSPACECONVERSION)) {
throw new RuntimeException("you must install the GPL version"
+ " of Xuggler (with IVideoResampler support) for "
+ "this demo to work");
}
IContainer container = IContainer.make();
if (container.open(inputstream, null) < 0) {
throw new IllegalArgumentException("could not open inpustream");
}
// query how many streams the call to open found
int numStreams = container.getNumStreams();
// and iterate through the streams to find the first video stream
int videoStreamId = -1;
IStreamCoder videoCoder = null;
for (int i = 0; i < numStreams; i++) {
// Find the stream object
IStream stream = container.getStream(i);
// Get the pre-configured decoder that can decode this stream;
IStreamCoder coder = stream.getStreamCoder();
if (coder.getCodecType() == ICodec.Type.CODEC_TYPE_VIDEO) {
videoStreamId = i;
videoCoder = coder;
break;
}
}
if (videoStreamId == -1) {
throw new RuntimeException("could not find video stream");
}
/*
* Now we have found the video stream in this file. Let's open up our
* decoder so it can do work.
*/
if (videoCoder.open() < 0) {
throw new RuntimeException(
"could not open video decoder for container");
}
IVideoResampler resampler = null;
if (videoCoder.getPixelType() != IPixelFormat.Type.BGR24) {
// if this stream is not in BGR24, we're going to need to
// convert it. The VideoResampler does that for us.
resampler = IVideoResampler.make(videoCoder.getWidth(),
videoCoder.getHeight(), IPixelFormat.Type.BGR24,
videoCoder.getWidth(), videoCoder.getHeight(),
videoCoder.getPixelType());
if (resampler == null) {
throw new RuntimeException(
"could not create color space resampler.");
}
}
/*
* Now, we start walking through the container looking at each packet.
*/
IPacket packet = IPacket.make();
long firstTimestampInStream = Global.NO_PTS;
long systemClockStartTime = 0;
while (container.readNextPacket(packet) >= 0) {
/*
* Now we have a packet, let's see if it belongs to our video stream
*/
if (packet.getStreamIndex() == videoStreamId) {
/*
* We allocate a new picture to get the data out of Xuggler
*/
IVideoPicture picture = IVideoPicture.make(
videoCoder.getPixelType(), videoCoder.getWidth(),
videoCoder.getHeight());
try {
int offset = 0;
while (offset < packet.getSize()) {
System.out
.println("VideoManager.decode(): decode one image");
/*
* Now, we decode the video, checking for any errors.
*/
int bytesDecoded = videoCoder.decodeVideo(picture,
packet, offset);
if (bytesDecoded < 0) {
throw new RuntimeException(
"got error decoding video");
}
offset += bytesDecoded;
/*
* Some decoders will consume data in a packet, but will
* not be able to construct a full video picture yet.
* Therefore you should always check if you got a
* complete picture from the decoder
*/
if (picture.isComplete()) {
System.out
.println("VideoManager.decode(): image complete");
IVideoPicture newPic = picture;
/*
* If the resampler is not null, that means we
* didn't get the video in BGR24 format and need to
* convert it into BGR24 format.
*/
if (resampler != null) {
// we must resample
newPic = IVideoPicture
.make(resampler.getOutputPixelFormat(),
picture.getWidth(),
picture.getHeight());
if (resampler.resample(newPic, picture) < 0) {
throw new RuntimeException(
"could not resample video");
}
}
if (newPic.getPixelType() != IPixelFormat.Type.BGR24) {
throw new RuntimeException(
"could not decode video as BGR 24 bit data");
}
/**
* We could just display the images as quickly as we
* decode them, but it turns out we can decode a lot
* faster than you think.
*
* So instead, the following code does a poor-man's
* version of trying to match up the frame-rate
* requested for each IVideoPicture with the system
* clock time on your computer.
*
* Remember that all Xuggler IAudioSamples and
* IVideoPicture objects always give timestamps in
* Microseconds, relative to the first decoded item.
* If instead you used the packet timestamps, they
* can be in different units depending on your
* IContainer, and IStream and things can get hairy
* quickly.
*/
if (firstTimestampInStream == Global.NO_PTS) {
// This is our first time through
firstTimestampInStream = picture.getTimeStamp();
// get the starting clock time so we can hold up
// frames until the right time.
systemClockStartTime = System
.currentTimeMillis();
} else {
long systemClockCurrentTime = System
.currentTimeMillis();
long millisecondsClockTimeSinceStartofVideo = systemClockCurrentTime
- systemClockStartTime;
// compute how long for this frame since the
// first frame in the stream.
// remember that IVideoPicture and IAudioSamples
// timestamps are always in MICROSECONDS,
// so we divide by 1000 to get milliseconds.
long millisecondsStreamTimeSinceStartOfVideo = (picture
.getTimeStamp() - firstTimestampInStream) / 1000;
final long millisecondsTolerance = 50; // and we
// give
// ourselfs
// 50 ms
// of
// tolerance
final long millisecondsToSleep = (millisecondsStreamTimeSinceStartOfVideo - (millisecondsClockTimeSinceStartofVideo + millisecondsTolerance));
if (millisecondsToSleep > 0) {
try {
Thread.sleep(millisecondsToSleep);
} catch (InterruptedException e) {
// we might get this when the user
// closes the dialog box, so just return
// from the method.
return;
}
}
}
// And finally, convert the BGR24 to an Java
// buffered image
BufferedImage javaImage = Utils
.videoPictureToImage(newPic);
// and display it on the Java Swing window
setImage(javaImage);
// if (listener != null) {
// listener.imageUpdated(javaImage);
// }
}
} // end of while
} catch (Exception exc) {
exc.printStackTrace();
}
} else {
/*
* This packet isn't part of our video stream, so we just
* silently drop it.
*/
do {
} while (false);
}
}
/*
* Technically since we're exiting anyway, these will be cleaned up by
* the garbage collector... but because we're nice people and want to be
* invited places for Christmas, we're going to show how to clean up.
*/
if (videoCoder != null) {
videoCoder.close();
videoCoder = null;
}
if (container != null) {
container.close();
container = null;
}
// byteDs = new ByteArrayDataSource(bytes, "video/3gp");
// ToolFactory.makere byteDs
// .getOutputStream();
// Manager.createPlayer(byteD);
// Player mediaPlayer = Manager.createRealizedPlayer(new
// MediaLocator(mobileLocation));
// Component video = mediaPlayer.getVisualComponent();
// Component control = mediaPlayer.getControlPanelComponent();
// if (video != null) {
// add(video, BorderLayout.CENTER);
// }
// add(control, BorderLayout.SOUTH);
// mediaPlayer.start();
// } catch (IOException | NoPlayerException | CannotRealizeException ex)
// {
// Logger.getLogger(imagePnl.class.getName()).log(Level.SEVERE, null,
// ex);
// }
paint(getGraphics());
}
public void setImage(BufferedImage image) {
this.image = (BufferedImage) image;
paint(getGraphics());
}
@Override
public void paintComponent(Graphics g) {
// super.paintComponent(g);
// Graphics2D g2d = (Graphics2D) g;
//
// g2d.drawImage(image, 0, 0, null);
// explicitly specify width (w) and height (h)
g.drawImage(image, 10, 10, this.getWidth(), this.getHeight(), this);
}
}在此行停止应用程序时,不会显示任何错误,但应用程序也不会在 pc 端显示视频。
我希望你能帮助我。该项目用于学习目的。提前致谢,法兰
最佳答案
如果你想从安卓流式传输视频,你应该使用像RTSP这样的流媒体协议(protocol)。或 RTP .使用 TCP 套接字将不起作用,因为 header 信息在通过套接字接收的所有数据包中都不可用。请看Spydroid .
关于java - 像网络摄像头一样使用安卓手机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15361679/
我正在编写一个具有以下签名的 Java 方法。 void Logger(Method method, Object[] args); 如果一个方法(例如 ABC() )调用此方法 Logger,它应该
我是 Java 新手。 我的问题是我的 Java 程序找不到我试图用作的图像文件一个 JButton。 (目前这段代码什么也没做,因为我只是得到了想要的外观第一的)。这是我的主课 代码: packag
好的,今天我在接受采访,我已经编写 Java 代码多年了。采访中说“Java 垃圾收集是一个棘手的问题,我有几个 friend 一直在努力弄清楚。你在这方面做得怎么样?”。她是想骗我吗?还是我的一生都
我的 friend 给了我一个谜语让我解开。它是这样的: There are 100 people. Each one of them, in his turn, does the following
如果我将使用 Java 5 代码的应用程序编译成字节码,生成的 .class 文件是否能够在 Java 1.4 下运行? 如果后者可以工作并且我正在尝试在我的 Java 1.4 应用程序中使用 Jav
有关于why Java doesn't support unsigned types的问题以及一些关于处理无符号类型的问题。我做了一些搜索,似乎 Scala 也不支持无符号数据类型。限制是Java和S
我只是想知道在一个 java 版本中生成的字节码是否可以在其他 java 版本上运行 最佳答案 通常,字节码无需修改即可在 较新 版本的 Java 上运行。它不会在旧版本上运行,除非您使用特殊参数 (
我有一个关于在命令提示符下执行 java 程序的基本问题。 在某些机器上我们需要指定 -cp 。 (类路径)同时执行java程序 (test为java文件名与.class文件存在于同一目录下) jav
我已经阅读 StackOverflow 有一段时间了,现在我才鼓起勇气提出问题。我今年 20 岁,目前在我的家乡(罗马尼亚克卢日-纳波卡)就读 IT 大学。足以介绍:D。 基本上,我有一家提供簿记应用
我有 public JSONObject parseXML(String xml) { JSONObject jsonObject = XML.toJSONObject(xml); r
我已经在 Java 中实现了带有动态类型的简单解释语言。不幸的是我遇到了以下问题。测试时如下代码: def main() { def ks = Map[[1, 2]].keySet()
一直提示输入 1 到 10 的数字 - 结果应将 st、rd、th 和 nd 添加到数字中。编写一个程序,提示用户输入 1 到 10 之间的任意整数,然后以序数形式显示该整数并附加后缀。 public
我有这个 DownloadFile.java 并按预期下载该文件: import java.io.*; import java.net.URL; public class DownloadFile {
我想在 GUI 上添加延迟。我放置了 2 个 for 循环,然后重新绘制了一个标签,但这 2 个 for 循环一个接一个地执行,并且标签被重新绘制到最后一个。 我能做什么? for(int i=0;
我正在对对象 Student 的列表项进行一些测试,但是我更喜欢在 java 类对象中创建硬编码列表,然后从那里提取数据,而不是连接到数据库并在结果集中选择记录。然而,自从我这样做以来已经很长时间了,
我知道对象创建分为三个部分: 声明 实例化 初始化 classA{} classB extends classA{} classA obj = new classB(1,1); 实例化 它必须使用
我有兴趣使用 GPRS 构建车辆跟踪系统。但是,我有一些问题要问以前做过此操作的人: GPRS 是最好的技术吗?人们意识到任何问题吗? 我计划使用 Java/Java EE - 有更好的技术吗? 如果
我可以通过递归方法反转数组,例如:数组={1,2,3,4,5} 数组结果={5,4,3,2,1}但我的结果是相同的数组,我不知道为什么,请帮助我。 public class Recursion { p
有这样的标准方式吗? 包括 Java源代码-测试代码- Ant 或 Maven联合单元持续集成(可能是巡航控制)ClearCase 版本控制工具部署到应用服务器 最后我希望有一个自动构建和集成环境。
我什至不知道这是否可能,我非常怀疑它是否可能,但如果可以,您能告诉我怎么做吗?我只是想知道如何从打印机打印一些文本。 有什么想法吗? 最佳答案 这里有更简单的事情。 import javax.swin
我是一名优秀的程序员,十分优秀!