如何在我的Android应用程序中显示URL中的视频?

时间:2014-07-11 06:38:56

标签: android video android-videoview

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

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

1.In Manifest我使用这样的许可

<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.在布局中我使用像这样的VideoView

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

3.在onCreate中我使用此代码

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.当我运行我的应用程序时,显示“无法播放此视频”为什么?

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

修改

我可以解决我的问题。它适用于上面的网址(“http://www.dailymotion.com/embed/video/x1ade3x”)

我试试这个

1.在布局中我像这样使用WebView

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

2.在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 embed

但不能使用url包含类似“http://www.w3schools.com/html/mov_bbb.mp4

的文件类型

谢谢大家。我的问题解决了。 ;)

3 个答案:

答案 0 :(得分:2)

首先将您的视频文件转换为.MP4​​或.3GP.Here是用于显示来自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;
        }
    }
}

答案 1 :(得分:0)

你确定要在onCreate中调用吗?活动尚未显示。尝试这样的事情:

 VideoView videoView;
        videoView.post(new Runnable() {
            @Override
            public void run() {
                MediaController mc = new MediaController(this);
                mc.setAnchorView(vdo_ContentVideo);
                mc.setMediaPlayer(vdo_ContentVideo);
                vdo_ContentVideo.setMediaController(mc);
                vdo_ContentVideo.setVideoURI(uri);
                vdo_ContentVideo.start();
            }
        });

答案 2 :(得分:0)

嗨首先,您需要将视频文件转换为支持的.MP4或.3GP移动分辨率。之后按照以下代码

VideoView video_player_view;
    DisplayMetrics dm;
    MediaController media_Controller;
     String path1="http://www.dailymotion.com/embed/video/x1ade3x";
Uri uri=Uri.parse(path1);

video_player_view = (VideoView) findViewById(R.id.video);
        dm = new DisplayMetrics();
        this.getWindowManager().getDefaultDisplay().getMetrics(dm);
        int height = dm.heightPixels;
        int width = dm.widthPixels;
        video_player_view.setMinimumWidth(width);
        video_player_view.setMinimumHeight(height);
        video_player_view
                .setVideoPath(uri);   //here you put your url
        video_player_view.start();