2016-07-13 2 views
-2

나는 json의 URL을 사용하여 영화 예고편을 사용자에게 보여주고 자하는 안드로이드 응용 프로그램을 개발하려고합니다. URL은 Youtube의 dailymotion이나 다른 소스에서 올 수 있습니다. 내 응용 프로그램에서 모든 비디오를 재생할 수있는 비디오 플레이어를 어떻게 만들 수 있는지 도움이됩니다. 감사합니다. .안드로이드 응용 프로그램에서 비디오를 재생하는 방법

답변

0

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> 

MainActivity.java -

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

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     VideoView videoView =(VideoView)findViewById(R.id.videoView1); 

       //Creating MediaController 
     MediaController mediaController= new MediaController(this); 
      mediaController.setAnchorView(videoView);   

       //specify the location of media file 
      Uri uri=Uri.parse(Environment.getExternalStorageDirectory().getPath()+"/media/1.mp4");   

       //Setting MediaController and URI, then starting the videoView 
      videoView.setMediaController(mediaController); 
      videoView.setVideoURI(uri);   
      videoView.requestFocus(); 
      videoView.start(); 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    } 

} 
+0

비디오보기 YouTube 동영상에서 나에게 오류를 제공 "캔트이 플레이 비디오 " – Arslan

0

당신의 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> 

그 후 활동에이 코드를 정의하십시오.

ViedoView videoview;

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(); 
      } 
     }); 
그것은 나를 위해 일하고

.. :)

0

이 시도 :

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"/> 
+0

내 응용 프로그램에서 비디오보기를 사용하려고했지만 youtube에서 비디오를로드 할 때 오류가 발생합니다"Can 이 비디오를 재생할 "수 있습니다. – Arslan

관련 문제