Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Android视频播放但不会出现VideoView大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
用户更改位置时,我正尝试在Google地图上播放视频.最初在activity_main.xml中,视频视图设置为不可见.当视频即将播放时,我将视频视图设置为可见并设置ZOrderOnTop(true).

当视频由onLOCATIOnChanged触发时,视频视图不会出现.但我可以听到视频中的音频,所以我知道它的播放.

但是,如果我在onCreate中首先播放视频,则会成功播放,出现在屏幕上.并且onLOCATIOnChanged视频也会播放并显示在屏幕上.

如果我不在onCreate中播放视频,为什么视频视频视图不会出现

activtity_main.xml

<FrameLayout xmlns:android="http://scheR_923_11845@as.android.com/apk/res/android"
android:layout_width="fill_parent" 
android:layout_height="fill_parent"
android:gravity="center" >

<RelativeLayout xmlns:android="http://scheR_923_11845@as.android.com/apk/res/android"
    xmlns:tools="http://scheR_923_11845@as.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView android:id="@+id/top_message"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:minWidth="110sp" android:textColor="#66CCFF"
        android:textStyle="bold" android:textSize="20sp" android:text="Default Text" />

    <fragment xmlns:android="http://scheR_923_11845@as.android.com/apk/res/android"
        android:id="@+id/map"
        android:layouT_Below="@id/top_message"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal" /> 

</RelativeLayout>

<RelativeLayout xmlns:android="http://scheR_923_11845@as.android.com/apk/res/android"
    xmlns:tools="http://scheR_923_11845@as.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <VideoView 
        android:visibility="invisible"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:id="@+id/myVideo"
        android:gravity="center"
        android:layout_centerInParent="true" />

    </RelativeLayout>

</FrameLayout>
@H_602_5@mainActivity.java

public class MainActivity extends FragmentActivity implements LOCATIOnListener {

private GoogleMap googleMap;
private TextView distanCETextView;
private VideoView videoView;
private LOCATIOnManager LOCATIOnManager;

@Override
protected void onCreate(Bundle savedInstanceStatE) {
    super.onCreate(savedInstanceStatE);

    setContentView(R.layout.activity_main);        

    // setup overlay for text
    distanCETextView = (TextView)findViewById(R.id.top_messagE);
    distanCETextView.setText("Text set onCreate");

    // setup overlay for video
    videoView = (VideoView) findViewById(R.id.myVideo);

    // setup google map data
    googleMap = ((SupportMapFragment)(getSupportFragmentManager().findFragmentById(R.id.map))).getMap();
    googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    googleMap.getUiSetTings().setCompassEnabled(true);
    googleMap.getUiSetTings().setZoomControlsEnabled(true);
    googleMap.setMyLOCATIOnEnabled(true);

    LOCATIOnManager = (LOCATIOnManager) getSystemservice(Context.LOCATION_serviCE);
    LOCATIOnManager.requestLOCATIOnupdates(LOCATIOnManager.GPS_PROVIDER,5000,1,this);
    LOCATIOn LOCATIOn = LOCATIOnManager.getLastKNownLOCATIOn(LOCATIOnManager.GPS_PROVIDER);

    // set current LOCATIOn and move map to that LOCATIOn
    if (LOCATIOn != null) {
        LatLng myPosition = new LatLng(LOCATIOn.getLatitude(),LOCATIOn.getLongitude());
        googleMap.animateCamera(CameraupdateFactory.newLatLngZoom(myPosition,15));
    }

    // if below is uncommented then secondvideo will also appear 
    //playVideo(new String("android.resource://" + getPackagename() + "/" + R.raw.firstvideo));

}

public void onLOCATIOnChanged(LOCATIOn LOCATIOn) {
    LOCATIOnManager.removeupdates(this);
    distanCETextView.setText("LOCATIOn has changed");
    playVideo(new String("android.resource://" + getPackagename() + "/" + R.raw.secondvideo));          
}

private void playVideo(String videoFilE) {
    videoView.setVisibility(View.VISIBLE);
    videoView.setZOrderOnTop(true);

    MediaController mediaController = new MediaController(this);
    mediaController.setAnchorView(videoView);
    videoView.setMediaController(mediaController);
    videoView.setVideoPath(videoFilE);
    videoView.start();
    videoView.setOnTouchListener(new View.onTouchListener() {
        @Override
        public Boolean onTouch(View v,MotionEvent event) {
            videoView.setVisibility(View.INVISIBLE);
            videoView.setZOrderOnTop(false);
            videoView.stopPlayBACk();
            return false;
        }
    });
}

@Override
public void onProviderDisabled(String arg0) {
    // TODO Auto-generated method stub
}

@Override
public void onProviderEnabled(String arg0) {
    // TODO Auto-generated method stub
}

@Override
public void onStatusChanged(String arg0,int arg1,Bundle arg2) {
    // TODO Auto-generated method stub

}

}

解决方法@H_874_24@
setZOrderOnTop()不能用于动态更改z级别.从 docs,

附加视图后,对setZOrderOnTop()的调用没有任何效果.一种解决方案是始终将VideoView置于顶部并控制它是否与您正在使用的.setVisibility()一样.您可以在onCreate()中使用一个调用setZOrderOnTop(),这可能是您在onCreate中播放视频时的原因.

大佬总结

以上是大佬教程为你收集整理的Android视频播放但不会出现VideoView全部内容,希望文章能够帮你解决Android视频播放但不会出现VideoView所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。