How to display Video from URL in my android app?

白昼怎懂夜的黑 提交于 2020-01-25 01:20:26

问题


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

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

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.In Layout I use VideoView like this

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

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.when I run my app it's show "Can't play this video" Why?

5.How can i display video from URL?

EDIT

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

I try this

1.In Layout I use WebView like this

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

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);
}
}

It's work very well and work with youtube embed

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. ;)


回答1:


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;
        }
    }
}



回答2:


are you sure you want to call it in onCreate? Activity is not yet visible. Try something like this:

 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();
            }
        });



回答3:


Hi First you need to convert your video file as .MP4 or .3GP mobile resolution supported. after that follow below code

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();


来源:https://stackoverflow.com/questions/24691505/how-to-display-video-from-url-in-my-android-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!