Source: https://www.edwith.org/boostcourse-android/lecture/17105/



1. 실행 결과 화면

  



2. activity_main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    tools:context="com.tistory.qlyh8.practice.MainActivity">
    <Button
        android:id="@+id/button"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:text="재생"/>
   <VideoView
       android:id="@+id/video_view"
       android:layout_width="match_parent"
       android:layout_height="match_parent" />
</LinearLayout>
 
cs



3. AndroidManifest.xml

인터넷 권한 추가

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.tistory.qlyh8.practice">
 
    <uses-permission android:name="android.permission.INTERNET" />
 
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
 
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
 
</manifest>
cs



4. MainActivity.java

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
package com.tistory.qlyh8.practice;
 
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;
 
 
public class MainActivity extends AppCompatActivity {
 
    private 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 = findViewById(R.id.video_view);
        MediaController mc = new MediaController(this); // 컨트롤러(재생, 정지 등) 객체
        videoView.setMediaController(mc);   // 컨트롤러 설정
        videoView.setVideoURI(Uri.parse(url)); // 동영상 파일 위치 설정
        videoView.requestFocus();   // 파일 정보의 일부를 가짐
        videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                // 파일을 재생할 준비과정이 끝났는지 확인
                Toast.makeText(getApplicationContext(), "동영상 준비됨", Toast.LENGTH_SHORT).show();
            }
        });
 
        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                videoView.seekTo(0);    // 처음 위치로 돌아가기
                videoView.start();  // 재생
            }
        });
    }
}
cs



5. 참고

MediaPlayer: https://developer.android.com/guide/topics/media/mediaplayer

'Android' 카테고리의 다른 글

RecyclerView (리사이클러뷰)  (0) 2018.08.23
음성 녹음하기  (0) 2018.08.23
음악 재생하기  (0) 2018.08.23
카메라 미리 보여주기  (0) 2018.08.23
사진 찍어 보여주기  (0) 2018.08.23

+ Recent posts