537
No
class MainActivity
package kr.co.braverokmc.videoplayer;
import android.media.MediaPlayer;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;
public class MainActivity extends AppCompatActivity {
VideoView videoView;
String url ="https://sites.google.com/site/ubiaccessmobile/sample_video.mp4";
Toast toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
videoView=(VideoView)findViewById(R.id.videoView);
// 동영상 위에 동영상을 컨트롤 할 수 있게 컨트롤러 객체 생성
MediaController controller=new MediaController(this);
videoView.setMediaController(controller);
videoView.setVideoURI(Uri.parse(url));
videoView.requestFocus();
//재생이 준비 되었는지
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
toastShow("동영상 재생이 준비되었습니다.");
}
});
//재생이 완료되었을 때
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
toastShow("동영상 재생이 완료되었습니다.");
}
});
}
public void onClick(View v){
videoView.seekTo(0); //처음으로 이동
videoView.start();
}
private void toastShow(String text){
if(toast==null){
toast= Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT);
}else {
toast.setText(text);
}
toast.show();
}
}
R.layout.activity_main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="kr.co.braverokmc.videoplayer.MainActivity">
<Button
android:text="시작"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:id="@+id/button"
android:onClick="onClick"
/>
<VideoView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/videoView"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_below="@+id/button" />
</RelativeLayout>
AndroidMainfest.xml
<uses-permission android:name="android.permission.INTERNET"/>
댓글 ( 4)
댓글 남기기