Android-在VideoView中播放MP4-没有视频但有音频(Z顺序不起作用)
本文关键字:音频 顺序 不起作用 视频 VideoView 播放 MP4- Android- | 更新日期: 2023-09-27 17:57:26
我正在试着做一个简单的测试,在VideoView中播放MP4,但我只能听到音频。我已经尝试过其他帖子中建议的Z顺序技巧,但没有奏效。
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(按钮触发代码):
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。
有很多因素需要审查。
-
如果您听到音频,则视频正在播放,但视频未按预期解码和显示。
-
这是在模拟器或物理设备上发生的吗?
- 如果是模拟器,请在物理设备上重新测试
-
您是否使用H.264 Baseline Profile编解码器进行编码?
- 不同的应用程序生成不同版本的H.264,请尝试通过不同的应用(即
ffmpeg
)进行编码
- 不同的应用程序生成不同版本的H.264,请尝试通过不同的应用(即
-
什么是安卓API版本?
- 不同的版本支持不同的编解码器,查看Android文档
-
实现
MediaPlayer.IOnInfoListener
和MediaPlayer.IOnErrorListener
,并查看您可能得到的MediaInfo
和MediaError
。
注意:我发现Xamarin的Android播放器在Genymotion上播放良好的视频时失败,AVD和BlueStacks也是如此。最后,在物理设备上进行测试
创建一个简单的测试应用程序来测试视频,并查看OnError和OnInfo参数。
IOnInfoListener/IOnErrorListener
示例:
[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>
您可以尝试在播放电影时使VideoView顶部的LinearLayout不可见,并在视频结束后再次可见。