MainActivity
...더보기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
package com.example.boostcoursepractice;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;
import java.io.File;
import java.net.URL;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity {
VideoView videoView;
public static String url = "http://sites.google.com/site/ubiaccessmobile/sample_video.mp4";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
videoView = (VideoView) findViewById(R.id.videoView);
MediaController mediaController = new MediaController(this);
videoView.setMediaController(mediaController);
videoView.setVideoURI(Uri.parse(url));
videoView.requestFocus();
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
Toast.makeText(getApplicationContext(), "동영상 준비됨.", Toast.LENGTH_LONG).show();
}
});
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
videoView.seekTo(0);
videoView.start();
}
});
}
}
|
cs |
activity_main.xml
...더보기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="시작" />
<VideoView
android:id="@+id/videoView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
|
cs |
동영상 재생은 일단 위 코드처럼 하면 된다. 이때 부연설명을 하자면 아래의 코드처럼
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
Toast.makeText(getApplicationContext(), "동영상 준비됨.", Toast.LENGTH_LONG).show();
}
});
비디오뷰에 setOnPreparedListener로 onPrepared메서드를 오버라이드한 OnPreparedListener를 설정한다면 requestFocus로 시작하여 비디오 재생 준비가 끝나면 onPrepared메서드가 호출되어 준비종료가 됬다는 것을 알수 있다. 그 이후로 동영상을 재생시켜주면 된다.
'2019 summer 부스트코스 에이스(안드로이드 프로그래밍) > 7. 멀티미디어' 카테고리의 다른 글
7-4-1 리싸이클러뷰 (0) | 2019.08.31 |
---|---|
7-3-1 음성 녹음하기 (0) | 2019.08.30 |
7-2-1 음악 재생하기 (0) | 2019.08.30 |
7-1-1 사진 찍어 저장하기 (0) | 2019.08.29 |