来自资源的视频未显示
Video from resources is not displaying
我正在尝试从 VideoView
中的应用程序资源中显示 mp4 视频,参考 this answer and this tutorial 我使用了以下方式:
// make the videoView visible
storyVideo.setVisibility(View.VISIBLE);
// set Video Preferences
storyVideo.requestFocus();
storyVideo.setBackgroundColor(Color.BLACK);
// get & set the video URI
Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.story_media4);
storyVideo.setVideoURI(uri);
// Start The Video
storyVideo.start();
其中 story_media4 存储在资源中的原始文件中:
您应该删除 storyVideo.setBackgroundColor(Color.BLACK);
。
我测试了您的代码并删除了 setBackgroundColor(Color.BLACK)
并解决了这个问题。
请测试以下代码 OnPreparedListener()
:
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
Log.i("LOG","Video test");
}
});
请删除 onPrepared
中的 videoView.start()
并使用 Log()
进行测试。
实际上视频显示但不显示,在this answer的帮助下,我在视频URI设置和视频准备监听器方法上设置video_view.setZOrderOnTop(true);
后可以显示视频:
// set Video Preferences
storyVideo.requestFocus();
storyVideo.setZOrderOnTop(true);
// get & set the video URI
Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.story_media4);
storyVideo.setVideoURI(uri);
// when the video is ready for display
storyVideo.setOnPreparedListener(onVideoPrepared);
private MediaPlayer.OnPreparedListener onVideoPrepared = new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
storyVideo.setZOrderOnTop(true);
// to start the video
storyVideo.start();
}
};
更新:
对于上述方法,视频将显示在所有视图之上,如果出于某种原因您希望视频被一个或多个视图覆盖(如标签或描述文本),最好的方法是设置背景透明视频的数量:
storyVideo.setBackgroundColor(Color.TRANSPARENT);
public class Videoplay extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_videoplay);
VideoView vidView = (VideoView)findViewById(R.id.myVideo);
MediaController vidControl = new MediaController(this);
String vidAddress = "https://archive.org/download/ksnn_compilation_master_the_internet/ksnn_compilation_master_the_internet_512kb.mp4";
Uri vidUri = Uri.parse(vidAddress);
Toast.makeText(Videoplay.this, "Loading Teaser",Toast.LENGTH_LONG).show();
vidView.setVideoURI(vidUri);
vidView.start();
vidControl.setAnchorView(vidView);
vidView.setMediaController(vidControl);
}
我正在尝试从 VideoView
中的应用程序资源中显示 mp4 视频,参考 this answer and this tutorial 我使用了以下方式:
// make the videoView visible
storyVideo.setVisibility(View.VISIBLE);
// set Video Preferences
storyVideo.requestFocus();
storyVideo.setBackgroundColor(Color.BLACK);
// get & set the video URI
Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.story_media4);
storyVideo.setVideoURI(uri);
// Start The Video
storyVideo.start();
其中 story_media4 存储在资源中的原始文件中:
您应该删除 storyVideo.setBackgroundColor(Color.BLACK);
。
我测试了您的代码并删除了 setBackgroundColor(Color.BLACK)
并解决了这个问题。
请测试以下代码 OnPreparedListener()
:
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
Log.i("LOG","Video test");
}
});
请删除 onPrepared
中的 videoView.start()
并使用 Log()
进行测试。
实际上视频显示但不显示,在this answer的帮助下,我在视频URI设置和视频准备监听器方法上设置video_view.setZOrderOnTop(true);
后可以显示视频:
// set Video Preferences
storyVideo.requestFocus();
storyVideo.setZOrderOnTop(true);
// get & set the video URI
Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.story_media4);
storyVideo.setVideoURI(uri);
// when the video is ready for display
storyVideo.setOnPreparedListener(onVideoPrepared);
private MediaPlayer.OnPreparedListener onVideoPrepared = new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
storyVideo.setZOrderOnTop(true);
// to start the video
storyVideo.start();
}
};
更新: 对于上述方法,视频将显示在所有视图之上,如果出于某种原因您希望视频被一个或多个视图覆盖(如标签或描述文本),最好的方法是设置背景透明视频的数量:
storyVideo.setBackgroundColor(Color.TRANSPARENT);
public class Videoplay extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_videoplay);
VideoView vidView = (VideoView)findViewById(R.id.myVideo);
MediaController vidControl = new MediaController(this);
String vidAddress = "https://archive.org/download/ksnn_compilation_master_the_internet/ksnn_compilation_master_the_internet_512kb.mp4";
Uri vidUri = Uri.parse(vidAddress);
Toast.makeText(Videoplay.this, "Loading Teaser",Toast.LENGTH_LONG).show();
vidView.setVideoURI(vidUri);
vidView.start();
vidControl.setAnchorView(vidView);
vidView.setMediaController(vidControl);
}