如何在android应用程序中播放视频

如何在android应用程序中播放视频,android,Android,我正在尝试开发一个android应用程序,其中我希望使用json中的url向用户显示电影预告片。URL可以来自Youtube dailymotion或任何其他来源。请帮助我如何才能使这样一个视频播放器,可以在我的应用程序中播放任何视频。 谢谢 借助MediaController和VideoView类,我们可以在android中播放视频文件 示例- activity_main.xml- <RelativeLayout xmlns:androclass="http://schemas.and

我正在尝试开发一个android应用程序,其中我希望使用json中的url向用户显示电影预告片。URL可以来自Youtube dailymotion或任何其他来源。请帮助我如何才能使这样一个视频播放器,可以在我的应用程序中播放任何视频。
谢谢

借助MediaController和VideoView类,我们可以在android中播放视频文件

示例-

activity_main.xml-

 <RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"  
        xmlns:tools="http://schemas.android.com/tools"  
        android:layout_width="match_parent"  
        android:layout_height="match_parent"  
        tools:context=".MainActivity" >  

        <VideoView  
            android:id="@+id/videoView1"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:layout_alignParentLeft="true"  
            android:layout_centerVertical="true" />  

    </RelativeLayout>  

在Xml中定义VideoView

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <VideoView
        android:id="@+id/VideoView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" />

</RelativeLayout>
它对我有用……)

试试这个:

  VideoView videoview;

  videoview = (VideoView) findViewById(R.id.videodetail_view);

  videoview.requestFocus();
试一试 {

final MediaController MediaController=new MediaController(Activity.this,true);
videoview.setMediaController(mediacontroller);
mediacontroller.setAnchorView(视频视图);
//mediacontroller.CanScroll(0);
scroll.getViewTreeObserver().addOnScrollChangedListener(新的ViewTreeObserver.OnScrollChangedListener(){
@凌驾
public void onScrollChanged(){
mediacontroller.hide();
}
});
urivideo=Uri.parse(videoUrl);
//videoview.setActivity(本);
//videoview.setShouldAutoplay(真);
videoview.setVideoURI(视频);
}捕获(例外e){
e、 printStackTrace();
}
videoview.setOnPreparedListener(新的MediaPlayer.OnPreparedListener(){
已准备好公开作废(MediaPlayer mp){
pDialog.disclose();
videoview.start();
}
});
在XmlLayout中

我试图在我的应用程序中使用视频视图,但当我从youtube加载视频时,出现错误“无法播放此视频”。视频视图从youtube视频中出现错误“无法播放此视频”
videoview = (VideoView) findViewById(R.id.VideoView);


try {
            // Start the MediaController
            MediaController mediacontroller = new MediaController(
                    VideoViewActivity.this);
            mediacontroller.setAnchorView(videoview);
            // Get the URL from String VideoURL
            Uri video = Uri.parse(VideoURL);
            videoview.setMediaController(mediacontroller);
            videoview.setVideoURI(video);

        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }

        videoview.requestFocus();
        videoview.setOnPreparedListener(new OnPreparedListener() {
            // Close the progress bar and play the video
            public void onPrepared(MediaPlayer mp) {
                pDialog.dismiss();
                videoview.start();
            }
        });
  VideoView videoview;

  videoview = (VideoView) findViewById(R.id.videodetail_view);

  videoview.requestFocus();
final MediaController mediacontroller = new   MediaController(Activity.this,true);

            videoview.setMediaController(mediacontroller);

            mediacontroller.setAnchorView(videoview);

            // mediacontroller.canScrollHorizontally(0);

     scroll.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {

                @Override
                public void onScrollChanged() {
                    mediacontroller.hide();
                }
            });


            Uri video = Uri.parse(videoUrl);
            //  videoview.setActivity(this);
            // videoview.setShouldAutoplay(true);
            videoview.setVideoURI(video);
        } catch (Exception e) {
            e.printStackTrace();
        }

     videoview.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            public void onPrepared(MediaPlayer mp) {
                pDialog.dismiss();
                videoview.start();
            }
        });


 In XmlLayout
  <VideoView android:id="@+id/videodetail_view"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="2dp"
            android:adjustViewBounds="true"
            android:background="@drawable/noimage"
            android:cropToPadding="true"
            android:scaleType="fitXY"/>