简体   繁体   English

如何在android中从服务器播放mp4视频?

[英]How to play an mp4 video from server in android?

I am able to display a 3gp video from server. 我能够从服务器显示3gp视频。 But when I tried to play an mp4 video it displayed an alert saying that Sorry,this video cannot be played. 但是当我尝试播放mp4视频时,它会显示一条警告说抱歉,此视频无法播放。 Please help me in this regard. 请帮助我这方面。

package com.play.video;

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

public class PlayvideofromserverActivity extends Activity
{
    private VideoView vView;
    private String vSource;


    @Override
    public void onCreate(Bundle savedInstanceState) 
    { 

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);


        vView = (VideoView)findViewById(R.id.vview);


        vView.requestFocus();


            vSource ="http://server.com/testvideo.mp4";
            vView.setVideoURI(Uri.parse(vSource));


        vView.setMediaController(new MediaController(this));


        vView.start();
    }
} 

Try this code. 试试这个代码。 It help you. 它可以帮到你。

myVideoView.setMediaController(new MediaController(this));
myVideoView.setVideoPath(videoSource);
myVideoView.requestFocus();
myVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
  public void onPrepared(MediaPlayer mp) {
    myVideoView.start();
  }
});

Thanks 谢谢

Android doesn't always play mp4. Android并不总是播放mp4。

MP4 is just a container - the video and audio stream inside it will both be encoded in different formats. MP4只是一个容器 - 其中的视频和音频流都将以不同的格式编码。

Android natively only supports certain types of formats. There is a list here: 这里有一个列表:

http://developer.android.com/guide/appendix/media-formats.html http://developer.android.com/guide/appendix/media-formats.html

Make sure the video and audio encoding type is supported . 确保video and audio encoding type is supported Just because it says "mp4" doesn't automatically mean it should be playable. 仅仅因为它说“mp4”并不自动意味着它应该是可玩的。

Credit goes to Ken Wolf 归功于Ken Wolf

Try this code 试试这个代码

MediaController mc = new MediaController(this);
videoView.setMediaController(mc);

String s=Common.videofilepath;
//Set the path of Video or URI
videoView.setVideoURI(Uri.parse(Common.videofilepath));

//Set the focus
videoView.requestFocus();
videoView.start();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM