【问题标题】:Android Could not play mp4 video with VideoView?Android 无法使用 VideoView 播放 mp4 视频?
【发布时间】:2012-08-23 20:35:54
【问题描述】:

我正在尝试使用仅包含 videoview 的全屏活动流式传输 mp4 视频。但即使它很容易在我的 galaxt s3 上播放,它也不能播放大多数设备。我怀疑它与视频编码有关(顺便说一句,您必须成为编解码器大师才能知道哪个视频播放哪个不播放)。

这是我正在使用的代码。

    //make screen full with video
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFormat(PixelFormat.TRANSLUCENT);
    setContentView(R.layout.full_screen_video);

    //get extra video source string
    Intent intent = getIntent();
    if(intent != null){
        videoSrc = intent.getStringExtra(TefalTvApp.RECIPE_VIDEO_SOURCE);
        if (DEBUG) {
            Log.d(TAG, "Recipe title : " + videoSrc);   
        }
    }

    video = (VideoView) findViewById(R.id.recipeVideo);
    controller = new MediaController(FullScreenVideoActivity.this);
    controller.setMediaPlayer(video);
    video.setMediaController(controller);
    video.setVideoPath(videoSrc);
    video.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            if (DEBUG) {
                Log.d(TAG, "Hello wworld");
            }
        }
    });

    video.setOnPreparedListener(new OnPreparedListener() {

        @Override
        public void onPrepared(MediaPlayer mp) {
            video.requestFocus();
            video.start();  
        }
    });

这是调试结果。您在调试中有视频网址。

08-23 23:25:53.349: D/RecipeDetailActivity(18179): Video src : http://www.tefaltv.com/Upload/Video/acticook_kiymalifasulye.mp4
08-23 23:25:53.379: I/ApplicationPackageManager(18179): cscCountry is not German : TUR
08-23 23:25:53.389: D/FullScreenVideoActivity(18179): Recipe title : http://www.tefaltv.com/Upload/Video/acticook_kiymalifasulye.mp4
08-23 23:25:53.399: D/VideoView(18179): onMeasure()
08-23 23:25:53.399: I/VideoView(18179):     Setting size: 480x295
08-23 23:25:53.439: I/MediaPlayer(18179): uri is:http://www.tefaltv.com/Upload/Video/acticook_kiymalifasulye.mp4
08-23 23:25:53.439: I/MediaPlayer(18179): path is null
08-23 23:25:53.439: D/MediaPlayer(18179): Couldn't open file on client side, trying server side
08-23 23:25:53.519: D/VideoView(18179): onMeasure()
08-23 23:25:53.519: I/VideoView(18179):     Setting size: 480x295
08-23 23:25:57.689: I/VideoView(18179): start()
08-23 23:25:57.739: D/VideoView(18179): onMeasure()
08-23 23:25:57.739: I/VideoView(18179):     Setting size: 480x270
08-23 23:25:57.759: E/MediaPlayer(18179): error (1, -2147483648)
08-23 23:25:57.769: I/VideoView(18179): start()
08-23 23:25:57.779: E/MediaPlayer(18179): start called in state 0
08-23 23:25:57.779: E/MediaPlayer(18179): error (-38, 0)
08-23 23:25:57.799: E/MediaPlayer(18179): Error (1,-2147483648)
08-23 23:25:57.799: D/VideoView(18179): Error: 1,-2147483648
08-23 23:25:57.799: E/MediaPlayer(18179): Error (-38,0)
08-23 23:25:57.799: D/VideoView(18179): Error: -38,0

更新: 视频编码细节: 格式:H.264、624 x 351 AAC,44100 赫兹,立体声 (L R) 每秒帧数:25 数据速率:2.23mbit/s 当前尺寸:624x351 px 实际

【问题讨论】:

    标签: android video mp4 codec


    【解决方案1】:

    这主要是因为编码。我看到您的视频文件分辨率是 624x351,这不是标准的,所以这可能是一个原因。还要检查最大比特率和 fps,因为所有移动设备都有一些上限,就像大多数 android 手机的 30fps 一样。您应该参考此文档以使您的文件与所有 android 手机兼容。 Android Media Formats

    另外,如果您可以共享编码命令(如果您正在执行一个),将很容易追踪问题。

    【讨论】:

    • 不,我不是提供视频的人。但我可以快速查看视频编码详细信息并更新问题。
    • 是的,我确实检查了您的编码详细信息:- 尺寸:- 624X351,编解码器:AAC、H.264、音频通道 -2 和比特率 2,233。我可以帮助你测试标准视频。这个页面我们为我们自己的应用程序选择了标准。让我知道它是否有帮助。其中 HLS 为 Http 直播(m3u8 播放列表格式),其余为 Mp4。 ec2-23-20-128-151.compute-1.amazonaws.com/test.html 。我们为 iPhone 制作了它,但您也可以为 android 得出一些结论。
    • 是的,我发布了和你一样的编码细节。让我检查一下你提供的那个。
    • 太好了,让我知道哪一个适合你,这样我就可以为你提供编码细节。
    • 好的,我刚刚测试了你的 mp4 版本,但它给出了同样的错误。这是我从您的页面使用的网址。 ec2-23-20-128-151.compute-1.amazonaws.com/tests/videos/…
    【解决方案2】:

    我建议您使用以下代码,其中我正在成功运行我的应用程序

    代码如下:

    XML 文件:

         <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#f0f0f0" >
    
            <Button
                android:id="@+id/btnVideoGallery"
                android:layout_width="75dp"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="15dp"
                android:text="@string/gallery" />
    
            <Button
                android:id="@+id/btnCancel"
                android:layout_width="120dp"
                android:layout_height="wrap_content"
                android:layout_below="@+id/btnVideoGallery"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="22dp"
                android:text="@string/cancel" />
    
            <TextView
                android:id="@+id/lblDisplayImage"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/btnCancel"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="10dp"
                android:text="@string/below_this_text_video_will_be_displayed"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#000000"
                android:textSize="13dp" />
    
            <VideoView
                android:id="@+id/vvDisplayVideo"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/lblDisplayImage"
                android:layout_marginTop="15dp" />
    
        </RelativeLayout>
    

    Java 文件:

    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.KeyEvent;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.MediaController;
    import android.widget.VideoView;
    
    public class VideoActivity extends Activity {
    
        private Button btnVideoGallery,btnCancel;
        private VideoView vvDisplayVideo;
        /** The Constant PICK_VIDEO. */
        private static final int PICK_VIDEO=1;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_video_options);
    
            btnVideoGallery=(Button)findViewById(R.id.btnVideoGallery);
            vvDisplayVideo=(VideoView)findViewById(R.id.vvDisplayVideo);
            btnCancel=(Button)findViewById(R.id.btnCancel);
            vvDisplayVideo.setVisibility(View.GONE);
    
            btnVideoGallery.setOnClickListener(new OnClickListener() {
    
                public void onClick(View v) {
    
                    Intent video=new Intent();
                    video.setAction(Intent.ACTION_PICK);
                    video.setType("video/*");
                    startActivityForResult(video, PICK_VIDEO);
    
                }
            });
    
            btnCancel.setOnClickListener(new OnClickListener() {
    
                public void onClick(View v) {
    
                    Intent goStartUp=new Intent(VideoActivity.this, StartUpActivity.class);
                    goStartUp.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(goStartUp);
                    finish();
                }
            });
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            // TODO Auto-generated method stub
            if (resultCode==Activity.RESULT_OK && requestCode == PICK_VIDEO) {
    
                vvDisplayVideo.setVisibility(View.VISIBLE);
                vvDisplayVideo.setVideoURI(data.getData());
                vvDisplayVideo.setFocusable(true);
                MediaController mc=new MediaController(this);
                vvDisplayVideo.setMediaController(mc);
                Log.i("True", "Executed");
            }
        }
    
        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
            // TODO Auto-generated method stub
    
            Intent goStartUp=new Intent(VideoActivity.this, StartUpActivity.class);
            goStartUp.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(goStartUp);
            finish();
            return super.onKeyDown(keyCode, event);
        }
    }
    

    您也可以根据自己的用途修改清单文件:

    <manifest ...
    <uses-sdk...  />
    <uses-permission android:name="android.permission.CAMERA" />
        <uses-permission android:name="android.permission.RECORD_VIDEO" />
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    
        <uses-feature
            android:name="android.hardware.camera"
            android:required="false" />
    
    <application .....
    </application>
    
    </manifest>
    

    【讨论】:

      【解决方案3】:

      您正在流式传输的视频大小与您的视频视图大小不同

      【讨论】:

        猜你喜欢
        • 2012-11-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-28
        相关资源
        最近更新 更多