如何在我的Android应用中显示来自URL的视频? [英] How to display Video from URL in my android app?

查看:118
本文介绍了如何在我的Android应用中显示来自URL的视频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我要在我的应用中显示的视频的示例.

This is an example of a video I want to show in my app.

http://www.dailymotion.com/embed/video/x1ade3x

1.在清单中,我使用这样的权限

1.In Manifest I use permission like this

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

2.在Layout中,我像这样使用VideoView

2.In Layout I use VideoView like this

<VideoView
      android:id="@+id/vdo_ContentVideo"
      android:layout_width="match_parent"
      android:layout_height="360dp"/>

3.在onCreate中,我使用此代码

3.In onCreate I use this code

vdo_ContentVideo = (VideoView)findViewById(R.id.vdo_ContentVideo);
if(vdo_ContentVideo != null){
    String path1="http://www.dailymotion.com/embed/video/x1ade3x";
    Uri uri=Uri.parse(path1);

    MediaController mc = new MediaController(this);
    mc.setAnchorView(vdo_ContentVideo);
    mc.setMediaPlayer(vdo_ContentVideo);
    vdo_ContentVideo.setMediaController(mc);
    vdo_ContentVideo.setVideoURI(uri);
    vdo_ContentVideo.start();
}

4.当我运行我的应用程序时,显示无法播放此视频"为什么?

4.when I run my app it's show "Can't play this video" Why?

5.如何显示来自URL的视频?

5.How can i display video from URL?

编辑

我可以解决我的问题.可以使用上述网址(" http://www.dailymotion.com/embed/video/x1ade3x )

I can solve my problem. It's work with above url ("http://www.dailymotion.com/embed/video/x1ade3x")

我尝试

1.在Layout中,我像这样使用WebView

1.In Layout I use WebView like this

<WebView
    android:id="@+id/webView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

2.在MainActivity中

2.In MainActivity

public class MainActivityextends Activity{

String url = "http://www.dailymotion.com/embed/video/x1ade3x";
WebView mWebView = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    weatherInfo = getIntent().getExtras().getParcelable("weatherdata");
    setContentView(R.layout.activity_main2);

    initwebView(rootView);
}

private void initwebView(View root) {
    mWebView = (WebView) root.findViewById(R.id.webView);

    /** unfortunately, we have to check sdk version ***/
    if (Build.VERSION.SDK_INT < 8) {
        //mWebView.getSettings().setPluginsEnabled(true);
    } else {
        mWebView.getSettings().setPluginState(WebSettings.PluginState.ON);
    }
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.setWebChromeClient(new WebChromeClient());
    mWebView.loadUrl(url);
}
}

效果很好,可以与youtube嵌入一起使用

It's work very well and work with youtube embed

但不能使用url包含这样的文件类型:" http://www.w3schools .com/html/mov_bbb.mp4 "

but not work with url is contain file type like this "http://www.w3schools.com/html/mov_bbb.mp4"

谢谢大家.我的问题是解决. ;)

Thank you every people. My problem was solve. ;)

推荐答案

首先将视频文件转换为.MP4​​或.3GP.这是对我有用的从Url显示视频的代码.

First convert your video file as .MP4 or .3GP.Here is the code which work for me to display video from Url.

 public class VideoViewDemo extends Activity {
    private static final String TAG = "VideoViewDemo";

    private VideoView mVideoView;
    private EditText mPath;
    private ImageButton mPlay;
    private ImageButton mPause;
    private ImageButton mReset;
    private ImageButton mStop;
    private String current;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);
        mVideoView = (VideoView) findViewById(R.id.surface_view);

        mPath = (EditText) findViewById(R.id.path);
        mPath.setText("http://daily3gp.com/vids/747.3gp");

        mPlay = (ImageButton) findViewById(R.id.play);
        mPause = (ImageButton) findViewById(R.id.pause);
        mReset = (ImageButton) findViewById(R.id.reset);
        mStop = (ImageButton) findViewById(R.id.stop);

        mPlay.setOnClickListener(new OnClickListener() {
            public void onClick(View view) {
                playVideo();
            }
        });
        mPause.setOnClickListener(new OnClickListener() {
            public void onClick(View view) {
                if (mVideoView != null) {
                    mVideoView.pause();
                }
            }
        });
        mReset.setOnClickListener(new OnClickListener() {
            public void onClick(View view) {
                if (mVideoView != null) {
                    mVideoView.seekTo(0);
                }
            }
        });
        mStop.setOnClickListener(new OnClickListener() {
            public void onClick(View view) {
                if (mVideoView != null) {
                    current = null;
                    mVideoView.stopPlayback();
                }
            }
        });
        runOnUiThread(new Runnable() {
            public void run() {
                playVideo();

            }

        });
    }

    private void playVideo() {
        try {
            final String path = mPath.getText().toString();
            Log.v(TAG, "path: " + path);
            if (path == null || path.length() == 0) {
                Toast.makeText(VideoViewDemo.this, "File URL/path is empty",
                        Toast.LENGTH_LONG).show();

            } else {
                // If the path has not changed, just start the media player
                if (path.equals(current) && mVideoView != null) {
                    mVideoView.start();
                    mVideoView.requestFocus();
                    return;
                }
                current = path;
                mVideoView.setVideoPath(getDataSource(path));
                mVideoView.start();
                mVideoView.requestFocus();

            }
        } catch (Exception e) {
            Log.e(TAG, "error: " + e.getMessage(), e);
            if (mVideoView != null) {
                mVideoView.stopPlayback();
            }
        }
    }

    private String getDataSource(String path) throws IOException {
        if (!URLUtil.isNetworkUrl(path)) {
            return path;
        } else {
            URL url = new URL(path);
            URLConnection cn = url.openConnection();
            cn.connect();
            InputStream stream = cn.getInputStream();
            if (stream == null)
                throw new RuntimeException("stream is null");
            File temp = File.createTempFile("mediaplayertmp", "dat");
            temp.deleteOnExit();
            String tempPath = temp.getAbsolutePath();
            FileOutputStream out = new FileOutputStream(temp);
            byte buf[] = new byte[128];
            do {
                int numread = stream.read(buf);
                if (numread <= 0)
                    break;
                out.write(buf, 0, numread);
            } while (true);
            try {
                stream.close();
            } catch (IOException ex) {
                Log.e(TAG, "error: " + ex.getMessage(), ex);
            }
            return tempPath;
        }
    }
}

这篇关于如何在我的Android应用中显示来自URL的视频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆