问题
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:
<?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 (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);
Any other suggestions?
MP4 is H.264 encoded using Photoshop and is 320x240.
回答1:
There are lots of factors to review.
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?
- If emulator, re-test on physical device
Are you using a H.264 Baseline Profile codec for encoding?
- Different apps produce different versions of H.264, try encoding via a different app, i.e.
ffmpeg
- Different apps produce different versions of H.264, try encoding via a different app, i.e.
What Android API version?
- Different versions support different codecs, review the Android docs
Implement
MediaPlayer.IOnInfoListenerandMediaPlayer.IOnErrorListenerand review theMediaInfoandMediaErrorthat you might be getting.
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
Create a simple test app for testing your videos and review the OnError and OnInfo parameters.
IOnInfoListener/IOnErrorListener Example:
[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();
};
}
}
Layout for the example:
<?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>
回答2:
You could try making the LinearLayout that is on top of the VideoView invisible when you play the movie and visible again once the video has finished.
来源:https://stackoverflow.com/questions/38442186/android-playing-mp4-in-videoview-no-video-but-audio-z-order-doesnt-work