Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/187.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android视频播放,但不显示VideoView_Android_Maps_Android Videoview - Fatal编程技术网

Android视频播放,但不显示VideoView

Android视频播放,但不显示VideoView,android,maps,android-videoview,Android,Maps,Android Videoview,当用户改变位置时,我试图在谷歌地图上播放视频。最初在activity_main.xml中,videoview设置为不可见。当视频即将播放时,我将videoview设置为可见,并将ZorDeronTop设置为true 当onLocationChanged触发视频时,视频视图不会出现。但我能听到视频中的音频,所以我知道它在播放 但是,如果我先在onCreate中播放视频,则会成功播放,并显示在屏幕上。此外,onLocationChanged视频也会播放并出现在屏幕上 如果我没有在onCreate中播

当用户改变位置时,我试图在谷歌地图上播放视频。最初在activity_main.xml中,videoview设置为不可见。当视频即将播放时,我将videoview设置为可见,并将ZorDeronTop设置为true

当onLocationChanged触发视频时,视频视图不会出现。但我能听到视频中的音频,所以我知道它在播放

但是,如果我先在onCreate中播放视频,则会成功播放,并显示在屏幕上。此外,onLocationChanged视频也会播放并出现在屏幕上

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

activity\u main.xml

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.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://schemas.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://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.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>
setZOrderOnTop()
不能用于动态更改z级别。从

请注意,这必须在曲面视图的包含窗口之前设置 已附加到窗口管理器


附加视图后,对
setZOrderOnTop()
的调用不会产生任何效果。一种解决方案是始终将
视频视图
放在顶部,并像您已经做的那样,通过
.setVisibility()
控制是否可以看到它。您可以在
onCreate()
中使用一个调用
setZOrderOnTop()
,这可能就是为什么您在
onCreate
中播放视频时它起作用的原因。非常感谢您,没有其他解决方案适合我。令人印象深刻!:)
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

}

}