안드로이드 MediaPlayer & VideoView

Post on 22-Jan-2018

9743 Views

Category:

Technology

2 Downloads

Preview:

Click to see full reader

Transcript

NHNNEXTEunjooIm

Android

MediaPlayer

& VideoViewGDGKorea

NovemberMeetup

NHNNEXTEunjooIm

android�media�player

android�video�play

NHNNEXTEunjooIm

1. What�are�the�difference�between�MediaPlayer�and�VideoView�

for�Android�

2. How�to�play�videos�in�android�from�assets�folder�or�raw�

folder?�

3. Android:�mediaplayer�went�away�with�unhandled�events�

4. Playing�a�video�in�VideoView�in�Android�

5. Using�VideoView�for�streaming�or�progressive-download�video�

6. Full�screen�videoview�without�stretching�the�video�

7. Playing�youtube�video�in�Android�app

NHNNEXTEunjooIm

안드로이드멀티미디어

Architecture

http://markmail.org/download.xqy?id=obl5o53uo3is5hoi&number=1

libaudio

JavaApplication

JavaFrameWork

Native

Driver

Camera MediaRecoder MediaPlayer

android.hardware.Camera

android.media.MediaRecorder

android.media.MediaPlayer

android.view.Surface

mediarecorder

Camera

CameraService

mediaplayer

MediaPlayerService

OpenCore StageFrightPlayerCameraHardware

UIlibSurfaceFlinger

AudioFlinger

Alsalib

Audio(Alsa)

Mainframebuffer

VideoPlaneHardwareCodec

V412Capture

Native system Service

Media Server Process

nuPlayer

AwesomePlayer

NHNNEXTEunjooIm

안드로이드미디어 재생

▪ 오디오,비디오재생을모두담당하는기본API

▪ 파일과스트림지원

▪ 볼륨과ringermode(벨,진동무음)등오디오자원과출력을담당

▪ 다양한자원에서영상을불러올수있는동영상재생전담위젯

▪ UI를제공하고확대/축소와tint기능제공

MediaPlayer AudioManager

VideoView

NHNNEXTEunjooIm

안드로이드미디어 재생

media sources Local Media

resources

Internal URIs

External URLs

content://directory/wordscontent�URI�표준�접두어

content�authority�=>��content�provider�식별

path

with

NHNNEXTEunjooIm

OpenCore StageFrightPlayer nuPlayer

AwesomePlayer

MediaPlayer

Media Playback

libaudio

JavaApplication

JavaFrameWork

Native

Driver

MediaPlayer

android.media.MediaPlayer

android.view.Surface

mediaplayer

MediaPlayerServiceUIlib

SurfaceFlinger

AudioFlinger

Alsalib

Audio(Alsa)

Mainframebuffer

VideoPlaneHardwareCodec

streammedia

mediafile

videostream

audiostream

android.view

sourceURIISurfaceaudiotype

Media Server

Process

ISurfaceaudiotype

http://markmail.org/download.xqy?id=obl5o53uo3is5hoi&number=1

NHNNEXTEunjooIm

MediaPlayer

SurfaceView

▪ 뷰위계질서내에그릴수있는표면을제공

▪ 백그라운드스레드에서화면을업데이트하여ANR을방지

▪ Surface객체를관리하는홀더

▪ MediaPlayer에서비디오를재생하려면surface를지정해야함

▪ SurfaceHolder생성후setDisplay(SurfaceHoldersurfaceHolder)

NHNNEXTEunjooIm

MediaPlayer

constructors : with default constructor

publicMediaPlayer()

+

+

setDisplay(SurfaceHoldersurfaceHolder)

+

prepare()orprepareAsync()

NHNNEXTEunjooIm

MediaPlayer

constructors : factory method

prepare()orprepareAsync()

성공적으로�로드될�경우��자동으로�동기적인�prepare()가�불리기�때문에�

대용량�미디어에는�비효율적

surfaceHolder를�지정하지�않으면�audio만�재생

NHNNEXTEunjooIm

MediaPlayer

Sample code : set up

MediaPlayermediaPlayer=newMediaPlayer();mediaPlayer.setDataSource(path);mediaPlayer.setDisplay(surfaceHolder);mediaPlayer.prepare();//mediaPlayer.prepareAsync();mediaPlayer.start();

MediaPlayermediaPlayer=MediaPlayer.create(context,R.raw.file1);

mediaPlayer.setDisplay(surfaceHolder);mediaPlayer.prepare();mediaPlayer.start();

with�default�constructor

With�create()��factory�method

NHNNEXTEunjooIm

MediaPlayer

Idle

Initialized

Prepared

Started

PlaybackCompleted

Preparing

End

Error

reset() release()

setDataSource()

OnErrorListener().

onError()

prepareAsync()

prepare()OnPrepareListener. onPrepared()

prepareAsync()

stop()

stop()

Looping == false && onCompletion() * from

OnCompletionListener

start() * from beginning

seekTo()

seekTo()/pause()pause()

start()

Looping == true && playback completes

seekTo()/start()

stop()

prepare()

start()

seekTo()

stop()

PausedStopped stop()

state diagram

NHNNEXTEunjooIm

MediaPlayer

callbacks

NHNNEXTEunjooIm

MediaPlayer

Sample code : Manifest

<uses-permissionandroid:name="android.permission.INTERNET"/>

<uses-permissionandroid:name="android.permission.WAKE_LOCK"/>

Internet�Permission�for�network�streaming

Wake�Lock�Permission�for�wake-up

NHNNEXTEunjooIm

MediaPlayer

Sample code : prepare surface

<SurfaceViewandroid:id=“+id/surface”

android:layout_width=“400dp”

android:layout_height=“240dp”/>

xml

ActivitypublicclassMediaPlayerActivityextendsActivityimplementsSurfaceHolder.Callback{

SurfaceViewsurfaceView;

SurfaceholdersurfaceHolder;

MediaPlayermediaPlayer;

@override

publicvoidonCreate(BundlesavedInstanceState){

//

surfaceView=(SurfaceView)findViewById(R.id.surface);

surfaceHolder=surfaceView.getHolder();

surfaceHolder.addCallback(this);

//

}

NHNNEXTEunjooIm

MediaPlayer

Sample code : MediaPlayer set up

Activity�-�Cont.publicclassMediaPlayerActivityextendsActivityimplementsSurfaceHolder.Callback{

//

@override

publicvoidsurfaceCreated(SurfaceHolderholder){

try{

mediaPlayer.setDataSource(//);

mediaPlayer.setDisplay(holder);

//

}catch(Exceptione){//}

}

}

NHNNEXTEunjooIm

MediaPlayer

Sample code : controll

publicclassMediaPlayerActivityextendsActivityimplementsSurfaceHolder.Callback{

//

mediaPlayer.start();

//

mediaPlayer.stop();

try{

mediaPlayer.prepare();//다음영상재생준비

}catch(Exceptione){//}

//

@Override

publicvoidonDestory(){

super.onDestroy();

if(mediaPlayer!=null){

mediaPlayer.release();

}

}

}

해당�button의�onClickListener에�구현�

지원하지�않는�상태에서는�

IllegalStateException�throw

Android:�mediaplayer�went�away�with�unhandled�events

NHNNEXTEunjooIm

MediaPlayer

Sample code : full size

<SurfaceViewandroid:id=“+id/surface”

android:layout_width=“match_parent”

android:layout_height=“match_parent”/>

xml

ActivitypublicclassMediaPlayerActivityextendsActivityimplementsSurfaceHolder.Callback{

//

@Override

publicvoidsurfaceCreated(SurfaceHolderholder){

//

try{

//

mediaPlayer.setOnVideoSizeChangedListener(sizeChangeListener);

}catch(Exceptione){e.printStackTrace();}

}

//

Full�screen�videoview�without�stretching�the�video

NHNNEXTEunjooIm

MediaPlayer

Sample code : full size

Activity�-�Cont.publicclassMediaPlayerActivityextendsActivityimplementsSurfaceHolder.Callback{

//MediaPlayer.OnPreparedListenerpreparedListener=

newMediaPlayer.OnPreparedListener(){@OverridepublicvoidonPrepared(MediaPlayermp){

Pointsize=newPoint();intvideoWidth=mediaPlayer.getVideoWidth();intvideoHeight=mediaPlayer.getVideoHeight();floatvideoProportion=(float)videoWidth/(float)videoHeight;getWindowManager().getDefaultDisplay().getSize(size);intscreenWidth=size.x;intscreenHeight=size.y;floatscreenProportion=(float)screenWidth/(float)screenHeight;android.view.ViewGroup.LayoutParamslayoutParams=

surfaceView.getLayoutParams();//

}//

}

NHNNEXTEunjooIm

MediaPlayer

Sample code : full size

Activity�-�Cont.publicclassMediaPlayerActivityextendsActivityimplementsSurfaceHolder.Callback{

//MediaPlayer.OnPreparedListenerpreparedListener=

newMediaPlayer.OnPreparedListener(){@OverridepublicvoidonPrepared(MediaPlayermp){

//if(videoProportion>screenProportion){

layoutParams.width=screenWidth;layoutParams.height=(int)((float)screenWidth/videoProportion);

}else{layoutParams.width=(int)(videoProportion*(float)screenHeight);layoutParams.height=screenHeight;

}surfaceView.setLayoutParams(layoutParams);

}};

}

NHNNEXTEunjooIm

VideoView

introduction

▪ 다양한자원에서영상을불러올수있는동영상재생전담위젯

▪ UI를제공하고확대/축소와tint기능제공

NHNNEXTEunjooIm

VideoView

MediaController ▪ MediaPlayer의컨트롤을담은뷰

▪ Play/Pause,Rewind,FastForward + progressslider

NHNNEXTEunjooIm

VideoView

set up

publicVideoView(//)

+

setVideoPath(path)orsetVideoURI(uri)

NHNNEXTEunjooIm

VideoView

Sample code : set up

<VideoViewandroid:id=“@+id/videoView”

android:layout_width=“400dp”

android:layout_height=“240dp”/>

xml

ActivitypublicclassVideoViewActivityextendsActivity{

@override

publicvoidonCreate(BundlesavedInstanceState){

//

VideoViewvideoView=(VideoView)findViewById(R.id.videoView);

//

videoView.setVideoPath(//);

//videoView.setVideoURL(url);

//

finalMediaControllermediaController=newMediaController(this);

videoView.setMediaController(mediaController);

}

} 예제:김상형.『안드로이드프로그래밍정복』.서울:한빛미디어,2013.

NHNNEXTEunjooIm

VideoView

Sample code : controll

Activity�-�Cont.publicclassPlayVideoActivityextendsActivity{

@override

publicvoidonCreate(BundlesavedInstanceState){

//

videoView.postDelayed(newRunnable(){

publicvoidrun(){

mediaController.show(0);

}

},100);

}

예제:김상형.『안드로이드프로그래밍정복』.서울:한빛미디어,2013.

NHNNEXTEunjooIm

▪ http://developers.google.com/youtube/android/player/

▪ 앱등록으로개발자키발급필요▪ 프로젝트에YouTubeDataAPIv3서비스추가필요▪ 사용자의기기에서YouTube앱4.2.16+실행필요

▪ YouTubePlayerFragment나YouTubePlayerView를View에배치하고YouTubePlayer를사용하여View에서동영상재생을제어

▪ 재생환경의세밀한조정가능▪ YouTubeStandalonePlayer를사용

▪ 더간편하고전체화면모드또는라이트박스모드지원▪ 동영상재생관련유연성과제어능력감소

PlayYouTube

VideoYouTube API 클라이언트 라이브러리

Playing�youtube�video�in�Android�app

NHNNEXTEunjooIm

Examples

https://github.com/luvgaram/android_GDG_examples

https://goo.gl/J8sGKI

NHNNEXTEunjooIm

참고자료

http://developer.android.com/intl/ko/guide/topics/media/mediaplayer.htmlhttp://developer.android.com/intl/ko/reference/android/media/MediaPlayer.html

AndroidDevelopers

http://www.slideshare.net/jerrinsg/android-media-framework-overview

Androidmediaframeworkoverview

http://www.netmite.com/android/mydroid/2.0/external/opencore/doc/mio_developers_guide.pdf

MediaI/Odeveloper’sGuideOpenCORE2.02,rev.1

http://markmail.org/download.xqy?id=obl5o53uo3is5hoi&number=1

AndroidMultiMediaFramworkOverviewLiLi,SolutionandServiceWindRiver

http://developers.google.com/youtube/android/player/

YouTubeAndroidPlayerAPI

안드로이드프로그래밍정복김상형.『안드로이드프로그래밍정복』.서울:한빛미디어,2013.

NHNNEXTEunjooIm

ThankYou

top related