android视频,听到声音但没有视频
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2184364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
android video, hear sound but no video
提问by slytron
I have tried several different examples but I cannot get any video to show. I hear sound but no video. I thought maybe I just had a incorrect video format so I downloaded a fiat commercial in 3gp format and still no joy. I am using the Eclipse Java EE with android sdk and my app targets the 1.5 sdk (Api Level 3) without google api. Could someone please post a link to a video known to play in android or point out my problem with the code. I have tried all I could think of .. with/without prepare .. different layouts etc.
我尝试了几个不同的示例,但无法显示任何视频。我听到声音但没有视频。我想也许我的视频格式不正确,所以我下载了 3gp 格式的法定广告,但仍然没有任何乐趣。我将 Eclipse Java EE 与 android sdk 一起使用,我的应用程序针对 1.5 sdk(Api Level 3)而没有 google api。有人可以发布一个链接到已知可在 android 中播放的视频或指出我的代码问题。我已经尝试了所有我能想到的……有/没有准备……不同的布局等。
Here is my onCreate in activity
这是我在活动中的 onCreate
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.setContentView(R.layout.video_test);
SurfaceView v = (SurfaceView) findViewById(R.id.surface_video);
SurfaceHolder holder = v.getHolder();
// Set the transparency
//getWindow().setFormat(PixelFormat.UNKNOWN);
// Set a size for the video screen
//holder.addCallback(this);
holder.setFixedSize(400,300);
MediaPlayer mp = MediaPlayer.create(this, R.raw.fiat);
mp.setDisplay(holder);
//mp.setAudioStreamType(2);
try {
//mp.prepare();
mp.start();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
her is the layout in video_test.xml
她是 video_test.xml 中的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<SurfaceView android:id="@+id/surface_video"
android:layout_width="250px"
android:layout_height="250px">
</SurfaceView>
<LinearLayout
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:padding="10dip"
>
</LinearLayout>
</LinearLayout>
> Blockquote
> 块报价
采纳答案by CommonsWare
If you are using the emulator, it may not work. The emulator lacks the hardware acceleration available in Android devices. For example, on a 2.6GHz Core 2 Duo, I sometimes can get a video to play back, but not always. On slower machines, video playback never works. On a 2.5GHz Core 2 Quad, I always get video playback. That being said, I usually test video playback on actual devices.
如果您使用的是模拟器,它可能无法工作。模拟器缺乏 Android 设备中可用的硬件加速。例如,在 2.6GHz Core 2 Duo 上,我有时可以播放视频,但并非总是如此。在速度较慢的机器上,视频播放永远不起作用。在 2.5GHz Core 2 Quad 上,我总能播放视频。话虽如此,我通常会在实际设备上测试视频播放。
I am also uncertain if video playback works from raw resources. I strongly encourage you to try using a video from a file on the SD card first.
我也不确定视频播放是否可以使用原始资源。我强烈建议您先尝试使用 SD 卡上文件中的视频。
With respect to videos that definitely work, I know that "Documentaries and You" and "Music for our Grandchildren" from herework in their MP4 forms.
对于影片,绝对是工作,我知道,“纪录片和你”,并从“音乐为我们的孙子”这里在他们的MP4形式的工作。
回答by wizemanold
You class must implement SurfaceHolder.Callback and call methods setDisplay, prepare and etc only after you get in surfaceCreated. Also you may need to change type of surface holder to SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS. Like this:
只有在您进入surfaceCreated 之后,您的课程才必须实现SurfaceHolder.Callback 并调用方法setDisplay、prepare 等。此外,您可能需要将表面支架的类型更改为 SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS。像这样:
import android.view.SurfaceHolder.Callback;
public class TestActivity extends Activity implements Callback {
// ...
@Override
public void onCreate(Bundle savedInstanceState) {
// ...
mSurfaceView = (SurfaceView)findViewById(R.id.yousurfaceview);
holder = mSurfaceView.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
mp.setDisplay(holder);
mp.setDataSource(somesource);
mp.prepare();
mp.start();
// etc...
} catch (IOException e) {
} catch (IllegalArgumentException e) {
} catch (IllegalStateException e) {
}
}
// ...
}
If you try it in OnCreate you got nothing as result, because Surface is not yet created...
如果您在 OnCreate 中尝试它,您将一无所获,因为 Surface 尚未创建...
回答by Alex W
You can get a full example by looking at the Android ApiDemo sample application (look at the example under 'Media'): http://developer.android.com/resources/samples/ApiDemos/index.html
您可以通过查看 Android ApiDemo 示例应用程序(查看“媒体”下的示例)来获得完整示例:http: //developer.android.com/resources/samples/ApiDemos/index.html
As for a sample movie to use with the demo, I've used links YouTube's mobile site with success.
至于与演示一起使用的示例电影,我成功地使用了 YouTube 移动网站的链接。
回答by speedplane
I ran into the same problem. I was able to fix it by changing SurfaceView to VideoView in the XML layout file. Keep all the rest of the code the same. That worked for me, don't know why.
我遇到了同样的问题。我能够通过在 XML 布局文件中将 SurfaceView 更改为 VideoView 来修复它。保持所有其余代码相同。这对我有用,不知道为什么。
回答by user4137900
This is an old problem but my answer may help someone.
这是一个老问题,但我的回答可能对某人有所帮助。
To support old versions of Android you need to add this line :
要支持旧版本的 Android,您需要添加以下行:
videoHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
回答by Chetan Patel
Whether a given video file will play in a given player depends on three things:
给定的视频文件能否在给定的播放器中播放取决于三件事:
- The video container format (file type).
- The codec the video (and potentially audio) streams encoding;
your player's support for that combination of container format and codec
- The codec and player/device support for it is almost certainly the cause of the inconsistent results you've seen. (A codec, if you didn't know, is basically a repeatable mathematical formula that tells your system how to turn bits and bytes packed into a file into moving pictures(and back again, for that matter))
- 视频容器格式(文件类型)。
- 视频(和潜在的音频)流编码的编解码器;您的播放器对容器格式和编解码器组合的支持
- 对它的编解码器和播放器/设备支持几乎肯定是您所看到的不一致结果的原因。(如果您不知道,编解码器基本上是一个可重复的数学公式,它告诉您的系统如何将打包到文件中的位和字节转换为运动图片(就此而言,再返回)