안드로이드
동영상 재생
liufeier
2021. 11. 5. 11:46
반응형
동영상 파일의 위치를 setVideoURI 메소드로 지정하면 동영상을 재생할 수 있다
동영상의 재생 상태를 보거나 동영상을 제어할 때 사용되는 MediaControll 객체는 setMediaController 메소드로 설정할 수 있는데, 손가락으로 터치하면 컨트롤러 부분을 보여주게 된다
VideoView 객체는 getDuraion이나 pause와 같이 동영상을 제어하는데 필요한 메소드들도 정의되어 있다
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.a74_videoplayer">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:usesCleartextTraffic="true"
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/Theme.A74_VideoPlayer">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activity_main
<?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"
tools:context=".MainActivity">
<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>
MainActivity
package com.example.a74_videoplayer;
import androidx.appcompat.app.AppCompatActivity;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.VideoView;
public class MainActivity extends AppCompatActivity {
public static final String VIDEO_URL = "https://sites.google.com/site/ubiaccessmobile/sample_video.mp4";
VideoView videoView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
videoView = findViewById(R.id.videoView);
MediaController mc = new MediaController(this);
videoView.setMediaController(mc);
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
videoView.setVideoURI(Uri.parse(VIDEO_URL));
videoView.requestFocus();
videoView.start();
}
});
}
}
동영상을 좀더 세밀하게 제어하려면 MediaPlayer 객체를 사용할 수 있다
반응형