- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试在用户更改位置时在 Google map 上播放视频。最初在 activity_main.xml 中,视频 View 设置为不可见。当视频即将播放时,我将视频 View 设置为可见并设置 ZOrderOnTop(true)。
当视频被 onLocationChanged 触发时,videoview 不会出现。但我可以听到视频中的音频,所以我知道它在播放。
但是,如果我先在 onCreate 中播放视频,它会成功播放并出现在屏幕上。 onLocationChanged 视频也会播放并出现在屏幕上。
如果我不在 onCreate 中播放视频,为什么没有出现 videovideo View ?
activtity_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>
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
}
}
最佳答案
setZOrderOnTop()
不能用于动态更改 z 级别。来自docs ,
Note that this must be set before the surface view's containing window is attached to the window manager.
附加 View 后,调用 setZOrderOnTop()
没有任何影响。一种解决方案是始终拥有 VideoView
在顶部并控制它是否被 .setVisibility()
看到就像你已经在做的那样。您可以使用一个电话 setZOrderOnTop()
在onCreate()
,这可能就是您在 onCreate
中播放视频时它起作用的原因.
关于Android 视频播放但 VideoView 没有出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14269001/
这是我的代码 fragment ,我想在其中播放来自服务器的视频 private void PlayVideo() { try { getWindow().setFormat(
我对 Android Studio 相当陌生,正在尝试一个项目,其中基于点击事件,videoView(2 个视频)从 vid1 变为 vid2,vid2 变为 vid1 等等,并且两者同时播放且长度相
我感兴趣的是能够在 VideoView B 播放时“预缓冲”VideoView A。这可能吗? 我有两个 VideoView 实例。 在给定时间只会显示一个 VideoView,占据手机的整个屏幕。
我最初隐藏了 VideoView,当加载视频时,我正在显示 VideoView。但是,如果 VideoView 最初是 invisible,则永远不会调用 onPrepared。但是,如果 Video
我正在尝试将视频添加到我的应用程序中。视频本地存储在 res/raw 中。我不知道为什么会出现“videoView.setVideoURI("uri");”错误和“videoView.start();
我正在创建一个应用程序来在 Amazon FireTV 机顶盒上播放各种类型的视频。如果我使用默认的 Android VideoView 播放视频。如果我使用 Vitamio 的 VideoView,
我必须在 android 上强制使用 videoView.setZOrderOnTop(true)。但是在使用它之后,我无法在 videoView 上显示按钮。即使在设置 videoView.setZ
我正在尝试制作一个带有 VideoView 的视频播放器,它支持在其上使用 TextView 的字幕。 我已经解析了字幕,但现在我需要一个在每次帧更改时运行的事件,以使用当前播放位置更新字幕。 我尝试
我有一个 *.mp4文件,持续时间为 2 分钟。现在它有从 30 seconds upto 1.10 min 开始的音轨. 30s 之前的其余部分及之后 1.10min为空白。 现在的问题是当我尝试在
我想在我的started_activity背景下运行视频 View 。但加载失败。 屏幕是黑色的,如果我导航或将我的页面推送到另一个页面两次,它将显示应用程序没有响应警报。 我遵循这个视频教程:htt
我在这里使用imageView,当我点击它时应该向我显示一个视频(线性10),这个视频大约4秒,我需要一个代码,当我再次点击imageView 视频应该再次开始播放,每次我单击 imageView 时
我的一个 Activity 中有一个 VideoView,如果我尝试按后退 Button,它不会将我带到之前的 Activity 并且它也不会在日志猫中给出任何错误消息。 我尝试覆盖 onBackPr
我正在制作一个视频播放器,它将从服务器流式传输视频。我已经检查过,视频网址正确且有效!已经很晚了,所以我可能错过了一些东西,但为什么会这样 VideoView videoView = (VideoVi
我有一个应用程序正在从另一个充当服务器的应用程序接收视频文件。当应用程序保存在套接字上接收到的文件时,视频流开始播放该文件(正在构建中)。在代码示例中,按下 btnStream 后,按下 btnPla
我有一个视频 View ,我希望能够播放/暂停和全屏显示。我无法让视频显示控件。 private fun showVideo() { isLottieVisible = false
我制作了一个应用程序,我想在其中播放原始文件夹中的 SWF 文件。 当我运行应用程序时,它会显示 Sorry, the video cant be played. 我不知道如何纠正它。我已经在设备上安
有谁知道为什么我的应用程序在打印出 VideoView 中的变量时只返回错误的值? @Override public void onCreate(Bundle savedInstanceState)
我想编写在单击按钮时播放视频的代码。我无法显示视频。当我点击按钮播放时,模拟器停止工作。 public class Video extends Activity{ private static fin
所以我正在尝试在 android 中播放基本的 avi 视频,它似乎在 Windows Media Player、VLC 等上运行良好,所以它看起来不需要任何复杂的编解码器。我的应用程序中有一个视频
我正在开发具有两个 View 的应用程序,一个是 UI,另一个是 VideoView。在某些事件发生之前使用 UI 线程,然后我想将 setContentView 设置为 VideoView,但是在将
我是一名优秀的程序员,十分优秀!