android videoview + 안드로이드 동영상 재생

2016. 7. 5. 22:11Mobile/Android



반응형

android videoview + 안드로이드 동영상 재생


VideoView sample code 


res -> raw 폴더를 만드시고 동영상을 삽입 해주세요.


Uri.parse 로 http 도 연결이 가능합니다.


ex 1)String uriPath = "android.resource://" + getPackageName() + "/" + R.raw.ggari;


ex 2)Uri.parse(http://www.~~~~~);


이런식으로..




메인엑티비티 소스 코드 참고


참고

https://developer.android.com/reference/android/widget/VideoView.html


MainActivity.java


package com.example.videotest;


import android.app.Activity;

import android.media.MediaPlayer;

import android.net.Uri;

import android.os.Bundle;

import android.widget.Toast;

import android.widget.VideoView;


public class MainActivity extends Activity {

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        VideoView vv = (VideoView) findViewById(R.id.vv);

        //http://www ~~~ 가능

        String uriPath = "android.resource://" + getPackageName() + "/" + R.raw.ggari;

        Uri uri = Uri.parse(uriPath);

        vv.setVideoURI(uri);

        vv.requestFocus();

        vv.start();


        //이벤트기능

        vv.setOnInfoListener(new MediaPlayer.OnInfoListener() {

            @Override

            public boolean onInfo(MediaPlayer mp, int what, int extra) {

                switch (what) {

                    case MediaPlayer.MEDIA_ERROR_TIMED_OUT:

                        Toast.makeText(getApplicationContext(), "MEDIA_ERROR_TIMED_OUT", Toast.LENGTH_LONG).show();

                        break;

                    case MediaPlayer.MEDIA_INFO_BUFFERING_START:

                        // Progress Diaglog 출력

                        Toast.makeText(getApplicationContext(), "MEDIA_INFO_BUFFERING_START", Toast.LENGTH_LONG).show();

                        break;

                    case MediaPlayer.MEDIA_INFO_BUFFERING_END:

                        // Progress Dialog 삭제

                        Toast.makeText(getApplicationContext(), "MEDIA_INFO_BUFFERING_END", Toast.LENGTH_LONG).show();

                        break;

                }

                return false;

            }

        });

    }

}


xml 소스코드 참고


activity_main.xml


<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

              android:layout_width="match_parent"

              android:layout_height="match_parent"

              android:orientation="vertical">


    <VideoView

        android:id="@+id/vv"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        />

</LinearLayout>








끝~

반응형