java - 无法使用 Android 默认视频播放器使用 Intent 播放视频

标签 java android android-intent android-videoview

我的 Android 设备可以直接播放相同的视频 MP4 和 AVI 视频文件,但在使用源代码时,在显示“正在加载视频”消息的进度对话框后,出现错误“无法播放此视频”。 。 我正在使用两种方式: 1.第一种方法:

Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
Uri data = Uri.parse("http://localhost/h.mp4");
                    intent.setDataAndType(data, "video/*");
                    startActivity(intent);
  1. 第二种方法:

        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    

有什么帮助吗?

最佳答案

您可以尝试VideoView(Documentation)

try{
      VideoView videoView = (VideoView) findViewById(R.id.VideoView);         
      MediaController mediaController = new MediaController(this);
      mediaController.setAnchorView(videoView);
      // Set Audio/Video
      String strfilename = "http://localhost/h.mp4";
      Uri video = Uri.parse(strfilename);
      videoView.setMediaController(mediaController);
      videoView.setVideoURI(video);
      videoView.start();   
} 
catch (Exception e) {
//Handle Errors
} 

编辑
Android 在使用 videoView 时对视频编码方式也很挑剔。 (参见下面引用 link )

For video content that is streamed over HTTP or RTSP, there are additional requirements:

•For 3GPP and MPEG-4 containers, the moov atom must precede any mdat atoms, but must succeed the ftyp atom.
•For 3GPP, MPEG-4, and WebM containers, audio and video samples corresponding to the same time offset may be no more than 500 KB apart. To minimize this audio/video drift, consider interleaving audio and video in smaller chunk sizes.

关于java - 无法使用 Android 默认视频播放器使用 Intent 播放视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18450373/

相关文章:

java - api中安装4J自动启动功能

java - Android是否可以创建HWND

android - 为什么 Tab 主机在 android 中不显示图标?

java - 如何将用户重定向到 Android 中的数据使用设置?

java - 将 Intent 返回到另一个应用程序后出现 ResourceNotFoundException

java - 需要帮助将代码转换为使用字符串而不是整数

java - 打印字符串,所有元音都替换为下划线 - java

java - Java 中的 setter 问题 - Internet Bill 程序

java - Android - 找不到默认 Activity

java - 在什么时候我可以从 Intent 中获取信息?