Android - 在 VideoView 中播放 MP4 - 没有视频但有音频(Z 顺序不起作用) [英] Android - Playing MP4 in VideoView - no video but audio (Z-Order Doesn't Work)
问题描述
我正在尝试做一个在 VideoView 中播放 MP4 的简单测试,但我只能听到音频.我已经按照其他帖子中的建议尝试了 Z 顺序技巧,但没有奏效.
I'm trying to do a simple test playing an MP4 in a VideoView, but I can only hear audio. I've already tried the Z-order trick as suggested in other posts, but it didn't work.
TestLayout.axml:
TestLayout.axml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/testLayout"
android:clipChildren="false">
<VideoView
android:id="@+id/testVideo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:paddingLeft="10dp"
android:paddingRight="20dp"
android:paddingTop="170dp"
android:paddingBottom="100dp"
android:clipChildren="false"
android:clipToPadding="false">
<ImageButton
android:id="@+id/play"
android:layout_height="20dp"
android:layout_width="60dp"
android:background="@android:color/transparent"
android:adjustViewBounds="false"
android:src="@drawable/play"
android:scaleType="fitXY"
android:padding="0dp"/>
</LinearLayout>
</RelativeLayout>
TestActivity.cs(按钮触发代码):
TestActivity.cs (button trigger code):
var videoView = (VideoView)FindViewById(Resource.Id.testVideo);
videoView.SetVideoURI(Android.Net.Uri.Parse("android.resource://" + PackageName + "/" +Resource.Raw.testVideo));
videoView.SetMediaController(null);
videoView.RequestFocus();
ISurfaceHolder holder = videoView.Holder;
holder.SetType(SurfaceType.PushBuffers);
videoView.Start();
videoView.SetZOrderMediaOverlay(true);
videoView.SetZOrderOnTop(true);
还有其他建议吗?
MP4 是使用 Photoshop 编码的 H.264,大小为 320x240.
MP4 is H.264 encoded using Photoshop and is 320x240.
推荐答案
有很多因素需要审查.
如果您听到音频,则视频正在播放,但视频未按预期解码和显示.
If you are hearing the audio, the video is playing, but the video is not being decoded and displayed as expected.
这是在模拟器还是物理设备上发生的?
Is this happening on an emulator or physical device?
- 如果是模拟器,请在物理设备上重新测试
您是否在使用 H.264 基线配置文件 用于编码的编解码器?
Are you using a H.264 Baseline Profile codec for encoding?
- 不同的应用产生不同版本的 H.264,尝试通过不同的应用进行编码,即
ffmpeg
什么 Android API 版本?
What Android API version?
- 不同的版本支持不同的编解码器,查看 Android 文档
实施 MediaPlayer.IOnInfoListener
和 MediaPlayer.IOnErrorListener
并检查您的 MediaInfo
和 MediaError
可能会得到.
Implement MediaPlayer.IOnInfoListener
and MediaPlayer.IOnErrorListener
and review the MediaInfo
and MediaError
that you might be getting.
注意:我发现 Xamarin 的 Android 播放器在 Genymotion 上播放良好的视频上失败,AVD 和 BlueStacks 也是如此.最后在物理设备上测试
Note: I have found that Xamarin's Android Player fails on videos that play fine on Genymotion, same goes for AVD and BlueStacks. In the end, test on physical devices
创建一个简单的测试应用来测试您的视频并查看OnError 和 OnInfo 参数.
Create a simple test app for testing your videos and review the OnError and OnInfo parameters.
[Activity(Label = "DroidVideo", MainLauncher = true, Icon = "@mipmap/icon")]
public class MainActivity : Activity, MediaPlayer.IOnInfoListener, MediaPlayer.IOnErrorListener
{
public bool OnError(MediaPlayer mp, [GeneratedEnum] MediaError what, int extra)
{
Log.Debug("Media", what.ToString());
return true;
}
public bool OnInfo(MediaPlayer mp, [GeneratedEnum] MediaInfo what, int extra)
{
Log.Info("Media", what.ToString());
return true;
}
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Main);
Button button = FindViewById<Button>(Resource.Id.myButton);
button.Click += delegate {
VideoView video = FindViewById<VideoView>(Resource.Id.myVideo);
video.SetOnInfoListener(this);
var videoUri = Android.Net.Uri.Parse("android.resource://" + Path.Combine(PackageName, "raw", Resource.Raw.pool.ToString()));
video.SetVideoURI(videoUri);
video.Start();
};
}
}
示例布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/myButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<VideoView
android:id="@+id/myVideo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
这篇关于Android - 在 VideoView 中播放 MP4 - 没有视频但有音频(Z 顺序不起作用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!