在Android中的VideoView中播放视频

时间:2010-07-16 10:03:41

标签: android

我无法弄清楚为什么我无法在我的VideoView中播放视频。我收到的所有信息都是:

  

无法播放视频:抱歉,此视频无法播放。

我也为我的模拟器创建了一张SD卡。我是否需要将SD卡放入SDK中的特定文件夹中?请评论。

这是布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
   android:id="@+id/LinearLayout01"
   android:layout_height="fill_parent"     
   android:paddingLeft="2px"
   android:paddingRight="2px"
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:paddingTop="2px"
   android:paddingBottom="2px"
   android:layout_width="fill_parent"
   android:orientation="vertical">

      <VideoView 
         android:layout_height="fill_parent"
         android:layout_width="fill_parent" 
         android:id="@+id/VideoView" />

</LinearLayout>

以下是代码:

package com.examples.videoviewdemo;

import android.app.Activity;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;

public class VideoViewDemo extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        VideoView videoView = (VideoView)findViewById(R.id.VideoView);
        //MediaController mediaController = new MediaController(this);
        // mediaController.setAnchorView(videoView);
        //videoView.setMediaController(mediaController);

        videoView.setVideoPath("/sdcard/blonde_secretary.3gp");

        videoView.start();  
    }
}

等待回复......

13 个答案:

答案 0 :(得分:31)

我的猜测是您的视频与Android不兼容。尝试使用其他视频。 This one肯定的作品曾用于Android(但出于某种原因,不适用于较新的设备)。如果该视频有效,而您的视频无效,那么您的视频与Android不兼容。

正如其他人所说,请在设备上进行测试。模拟器上的视频播放需要太多电量。

答案 1 :(得分:19)

示例项目

我终于得到了一个概念验证项目,所以我将在这里分享。

设置布局

布局设置如下,浅灰色区域为VideoView

enter image description here

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.videotest.MainActivity">

    <VideoView
        android:id="@+id/videoview"
        android:layout_width="300dp"
        android:layout_height="200dp"/>

    <Button
        android:text="Play"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/videoview"
        android:onClick="onButtonClick"
        android:id="@+id/button"/>

</RelativeLayout>

准备视频片段

根据documentation,Android应支持所有API级别的mp4 H.264播放(解码)。但是,似乎有很多因素会影响实际视频是否播放。我能找到的最深入的答案是如何对视频进行编码的here。它使用强大的ffmpeg命令行工具进行转换,以便在所有(希望是?)Android设备上播放。阅读我链接的答案以获得更多解释。我使用了稍微修改过的版本,因为我的原始版本出现了错误。

ffmpeg -y -i input_file.avi -s 432x320 -b:v 384k -vcodec libx264 -flags +loop+mv4 -cmp 256 -partitions +parti4x4+parti8x8+partp4x4+partp8x8 -subq 6 -trellis 0 -refs 5 -bf 0 -coder 0 -me_range 16 -g 250 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -qmin 10 -qmax 51 -qdiff 4 -c:a aac -ac 1 -ar 16000 -r 13 -ab 32000 -aspect 3:2 -strict -2 output_file.mp4

我肯定会在每个参数上阅读更多内容,以了解哪些需要调整视频和音频质量。

接下来,将output_file.mp4重命名为test.mp4并将其放入Android项目的/res/raw文件夹中。如果文件夹尚未存在,请创建该文件夹。

代码

代码并不多。视频播放时播放&#34; Play&#34;单击按钮。感谢this answer寻求帮助。

MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void onButtonClick(View v) {
        VideoView videoview = (VideoView) findViewById(R.id.videoview);
        Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.test);
        videoview.setVideoURI(uri);
        videoview.start();
    }
}

完成

这就是全部。您现在应该可以在模拟器或真实设备上播放视频片段。

答案 2 :(得分:7)

使videoView成为您的活动类的成员变量,而不是将其保留为onCreate函数的本地变量:

VideoView videoView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    videoView = (VideoView)findViewById(R.id.VideoView);        
    videoView.setVideoPath("/sdcard/blonde_secretary.3gp");
    videoView.start();  
}

答案 3 :(得分:5)

在清单中添加android.permission.READ_EXTERNAL_STORAGE,为我工作

答案 4 :(得分:2)

代码似乎完美无瑕! 简单明了。
所以它应该在手机上工作。模拟器很难播放视频,它也发生在我身上。

尝试将所需的API级别提升到最新级别,这可能会有所帮助!

右键单击打开的项目,选择Properties&gt; Android&gt;检查右侧的最新版本......

伊戈尔

答案 5 :(得分:2)

您可以通过DDMS访问SD卡

答案 6 :(得分:2)

VideoView只能播放3gp视频我建议使用此代码来播放视频 或尝试更高版本的Android。试试视频在线流媒体。

public void onCreate(Bundle savedInstanceState) {
    setContentView(R.layout.main);
    String videourl = "http://something.com/blah.mp4";
    Uri uri = Uri.parse(videourl);
    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
    intent.setDataAndType(uri, "video/mp4");
    startActivity(intent);
}

Click here to watch Android Video Streaming Tutorial

答案 7 :(得分:2)

要确认您的视频格式正确(分辨率,比特率,编解码器等),请查看official documentation - 摘录如下:

标准清晰度(低质量)
视频编解码器 - H.264
视频分辨率 - 176 x 144像素
视频帧率 - 12 fps
视频比特率 - 56 Kbps
音频编解码器 - AAC-LC
音频通道 - (单声道)
音频比特率 - 24 Kbps

标准清晰度(高品质)
视频编解码器 - H.264
视频分辨率 - 480 x 360像素
视频帧率 - 30 fps
视频比特率 - 500 Kbps
音频编解码器 - AAC-LC
音频通道 - 2(立体声)
音频比特率 - 128 Kbps

高清720p(所有设备均为N / A)
视频编解码器 - H.264
视频分辨率 - 1280 x 720像素
视频帧率 - 30 fps
视频比特率 - 2 Mbps
音频编解码器 - AAC-LC
音频通道 - 2(立体声)
音频比特率 - 192 Kbps

答案 8 :(得分:2)

VideoView videoView =(VideoView) findViewById(R.id.videoViewId);

Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getAbsolutePath()+"/yourvideo");

 videoView.setVideoURI(uri);
videoView.start();

使用setVideoUri而不是使用setVideoPath。您可以使用(Environment.getExternalStorageDirectory()。getAbsolutePath()+&#34; / yourvideo&#34;)获取存储在外部存储中的视频路径,并将其解析为Uri。如果您的视频存储在sdcard / MyVideo / video.mp4中,请替换&#34; / yourvideo&#34;在代码中由&#34; /MyVideo/video.mp4"

这对我来说很好:)   `

答案 9 :(得分:0)

好吧!如果您使用的是Android兼容性视频,则此警报的唯一原因是您必须使用的视频大小超过300MB。 Android不支持大型视频(> 300MB)。 我们可以通过使用NDK优化来获得它。

答案 10 :(得分:0)

问题可能出在电影格式上。如果是H264编码,请确保它处于基线配置文件中。

答案 11 :(得分:0)

我对VideoView几乎有同样的问题。我尝试使用Nexus 5播放视频(1080 * 1080)并且效果很好,但Galaxy ace 2上的同一视频给了我无法播放的视频消息。

但我注意到视频定义较低(120x120),效果很好。

所以也许只是“尺寸”的问题 (特别是与blonde_secretary.3gp视频......)

答案 12 :(得分:0)

//just copy this code to your main activity.

 if ( ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED ){
            if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, android.Manifest.permission.READ_EXTERNAL_STORAGE)){

            }else {
                ActivityCompat.requestPermissions(MainActivity.this,new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE},1);
            }
        }else {
        }