출처: http://sajo.tistory.com/entry/%EC%95%88%EB%93%9C%EB%A1%9C%EC%9D%B4%EB%93%9C-%EB%8F%99%EC%98%81%EC%83%81-%EC%8A%A4%ED%8A%B8%EB%A6%AC%EB%B0%8D-%EC%9E%AC%EC%83%9D
[안드로이드] 동영상 스트리밍 재생
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;
public class main extends Activity {
private static final String MOVIE_URL = "http://www.archive.org/download/Unexpect2001/Unexpect2001_512kb.mp4";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.video);
VideoView videoView = (VideoView) findViewById(R.id.VideoView);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
// Set video link (mp4 format )
Uri video = Uri.parse(MOVIE_URL);
videoView.setMediaController(mediaController);
videoView.setVideoURI(video);
videoView.requestFocus();
videoView.start();
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingLeft="2px" android:paddingRight="2px"
android:paddingTop="2px" android:paddingBottom="2px"
android:layout_width="fill_parent" android:orientation="vertical">
<VideoView android:layout_height="fill_parent"
android:layout_width="fill_parent" android:id="@+id/VideoView"></VideoView>
////////////////////////////////////////////////////////////////////////
출처: http://androiddeveloper.tistory.com/70
Android - Video play (온라인상 동영상 재생)
Android 2011/10/20 20:53
////////////////////////////////////////////////////////////////////////////////////
출처: http://androiddeveloper.tistory.com/69
예전에 사운드 재생을 해봤는데요
오늘은 동영상을 재생을 한번 해보겠습니다.
온라인상에 있는 동영상을 재생하는 방법과 동영상파일을 첨부해서 재생시키는 두가지 방법을 살펴볼텐데요.
먼저 VideoView 도구를 이용한 첨부파일 동영상을 재생시키는 것을 살펴보겠습니다.
재생할 동영상을 res폴더에 raw폴더를 생성 후 raw폴더에 재생할 동영상을 넣습니다.
안드로이드에서 지원하는 동영성 코덱을 잘 알아보시고 넣어주시기 바랍니다.
필자는 wmv가 안되는 것을 모르고 왜 안되지 왜 안되는거야 하면서 이리저리 찾다가 알았습니다.
mp4가 기본적으로 지원하고 avi도 지원하는 것 같습니다.
그럼 VideoView를 사용하여 동영상을 재생해보겠습니다.
먼저 비디오를 재생하면 기본적으로 화면이 어떻해 재생되는 지 알죠?
가로입니다. 가로고정을 시켜줘야 합니다. 세로로해도 무난하지만 기본에 충실합시다.
프로젝트 생성
Project name : ExamOptionsMenu
Build Target : Android 2.1
Package name : jsh.exam.option
Create Activity : main
Min SDK Version : 7
src
main.java
package jsh.exam.video;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;
public class ExamvideoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 생성한 비디오뷰를 선언
VideoView videoView = (VideoView) findViewById(R.id.videoView1);
// 비디오뷰를 커스텀하기 위해서 미디어컨트롤러 객체 생성
MediaController mediaController = new MediaController(this);
// 비디오뷰에 연결
mediaController.setAnchorView(videoView);
// 안드로이드 res폴더에 raw폴더를 생성 후 재생할 동영상파일을 넣습니다.
Uri video = Uri.parse("android.resource://" + getPackageName()
+ "/raw/동영상파일이름");
//비디오뷰의 컨트롤러를 미디어컨트롤로러 사용
videoView.setMediaController(mediaController);
//비디오뷰에 재생할 동영상주소를 연결
videoView.setVideoURI(video);
//비디오뷰를 포커스하도록 지정
videoView.requestFocus();
//동영상 재생
videoView.start();
}
}
res
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<VideoView
android:id="@+id/videoView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"></VideoView>
</LinearLayout>
실행결과
프로젝트안에 첨부된 동영상파일이
재생되는 것을 볼 수 있습니다.
//////////////////////////////////////////////////////////////////////
출처: http://blog.naver.com/PostView.nhn?blogId=gh2501&logNo=128504857
출처: http://blog.daum.net/baramjin/16011132
비디오 재생을 위한 파일 브라우져를 만드는 경우 VideoView를 이용하는 새로운 Activity를 만들고 이를 이용하여 Video File을 재생할 수 있다.
예를 "video" Activity를 만들고 파일 경로명을 전달받는 intent를 다음과 같이 설계하였다면
public class video extends Activity {
private VideoView mVideoView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.videoview);
Intent intent = getIntent();
String path = intent.getStringExtra("FilePath");
...
}
이전 Activiy에서 파일 경로를 전달하는 것은 다음과 같이 한다.
Intent intent = new Intent(fileplayer.this, video.class);
intent.putExtra("FilePath", mPath+temp.name);
startActivity(intent);
이러한 intent를 명시적 intent라고 한다. intent 생성시에 target 클래스를 명확하게 지정하고 있으므로 항상 video 클래스가 시작되고, 여기에서 비디오를 재생하도록 코드를 구현해야 한다.
안드로이드의 경우 이미 Gallery, Gallery3D와 같은 비디오 재생 응용 프로그램이 있다.
비디오 재생을 위한 Activity를 별도로 생성하지 않고 Gallery에 포함된 Movie View나 View Image를 사용하려면 암시적 intent를 사용한다.
intent.setDataAndType(Uri.parse("file://" + mPath+temp.name), "video/*");
startActivity(intent);
intent 생성시 target 클래스를 지정하지 않고 android.content.Intent.ACTION_VIEW를 이용하여 action을 정의한다. intent에 넣어줄 데이터는 Uri 형태로 하며 Type을 "video/*" 로 정의한다. Galley의 경우 위의 Intent를 실행할 수 있는 Intent filter가 정의되어 있기 때문에 Gallery의 Movie View Activity가 실행되게 된다.
Gallery의 AndroidManifest.xml을 파일을 보면 다음과 같다.
android:label="@string/movieviewlabel"
android:screenOrientation="landscape"
android:configChanges="orientation|keyboardHidden"
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="rtsp" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="video/*" />
<data android:mimeType="application/sdp" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http" />
<data android:mimeType="video/mp4" />
<data android:mimeType="video/3gp" />
<data android:mimeType="video/3gpp" />
<data android:mimeType="video/3gpp2" />
</intent-filter>
</activity>
Gallery의 AndroidManifest.xml을 파일을 좀 더 보면 View Image를 사용하기 위한 방법도 알 수 있다.
<activity android:name="com.android.camera.ViewImage"
android:label="@string/view_label"
android:screenOrientation="behind"
android:configChanges="orientation|keyboardHidden">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
</activity>
즉 위의 코드에서 다음과 같이 수정하면 된다.
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file://" + mPath+temp.name), "image/*");
startActivity(intent);
반대의 경우를 생각해 보자.
지금 만들고 있는 fileplayer라는 응용 프로그램은 파일 브라우져 기능과 비디오 재생 기능을 가지고 있다. 비디오 재생은 "video" Activity에서 담당하고 있다.
이 경우 만들고 있는 응용 프로그램의 AndroidManifest.xml에 Gallery처럼 Intent Filter를 추가한다.
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:name=".video">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="video/*" />
<data android:mimeType="application/sdp" />
</intent-filter>
</activity>
그럼 파일 재생 시도 시에 팝업창이 뜨고 Galley와 fileviewer 중에 한가지를 선택하도록 안드로이드가 동작한다.
정상적으로 동작하려면 "video" Activity 클래스에서 intent를 처리하는 것도 변경해야 한다. 명시적 intent를 사용할 때는 FilePath 라는 추가 정보를 전달했지만 암시적 intent를 사용하는 경우는 Uri 를 입력하여 동작했으므로 다음과 같이 수정한다.
Intent intent = getIntent();
String action = intent.getAction();
Log.v("Video Intent:", action);
Uri data = intent.getData();
String path = data.getEncodedPath();
Log.v("Video Uri:", path);
///////////////////////////////////////////////////////////////////////////////////////////////////////
출처: http://www.androidpub.com/562881
http://daily3gp.com/vids/747.3gp
참 찾기 어려웠습니다.
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse(Uri.encode("http://daily3gp.com/vids/747.3gp"));
intent.setDataAndType(uri,"video/*");
startActivity(intent);
유튜브의 어정쩡한 http://www.youtube.com/watch?v=BgW_pn60rsc 이러한 URL은 절대 안되더군요
유튜브 URL 넣어놓고 안돌아가길래 별짓 다해봤는데. 결국엔 URL문제였습니다.
즉! URL주소가 http://daily3gp.com/vids/747.3gp처럼 인터넷 주소창에 넣었을 때 바로 다운이 되는 URL을 넣어야
돌아가는 것 같더군요.
유튜브의 URL 아무리 넣어봤자 돌아가지 않아요 ㅠ
어쨋든 돌렸습니다.
그럼 질문좀 드릴게요
1. 저게 다운로드가 되고 나서야 돌아가는거 같은데 맞나요?
결국엔 다운받으면서 재생되는것이 아니라 다운을 받고 어딘가에 저장해 논다음 돌리는게 아니냐는 질문입니다.
만약, 그렇다면 용량이 큰 건 엄청 늦게 시작되겠군요!
2. 파일형식이 있던것 같은데 어떤게 되나요?
제가 알기로는 3gp랑 mp4 정도 되는것 같던데 다른것도 되는게 있나요?
3. 유튜브에서 나오는 동영상을 돌릴 방법이 없을까요??
4. 1번이 맞다면 다운받으면서 실행하는 방법은 없을까요?
스트리밍이라고 하나요?
질문이 많습니다 답변좀 꼭 부탁드려요!!
그렇다면 http:// 로 되어있는 URL 같은 경우는 다운받아서 실행하는게 맞다는건가요??
rtsp로 된 URL은 스트리밍으로 받아서 되는거구요??
스트리밍 url은 어떻게 확인할 수 있나요?
알려주세요 ㅠㅠ 소스보기해도 안나와요..
////////////////////////////////////////////////////////////////////////////////////
출처: http://javaexpert.tistory.com/365
// VideoView에는 기본 버튼들이 정의되어 있어서 앞에 글에서처럼
// MediaPlayer을 이용해 만들었을 때 버튼을 만들고 정의하는 과정이 필요없다.
// 하지만 VIdeoView로 객체를 만들어 동영상을 재생시키면 무슨 설정을 해도 전체화면으로 나오게 할 수 없다
// 때문에 VIdeoView를 상속받은 클래스를 만들어 사용해야 한다.
// manifest에서 해당 activity에 android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
// 옵션을 설정해도 해결되지 않아 만들게 되었다.
class MyVideoView extends VideoView
{
public MyVideoView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
Display dis =((WindowManager)getContext().
getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
}
}
주의사항
1.activity에서는 다음과 같이 사용( VideoView가 아님 )
videoView = (MyVideoView)findViewById(R.id.videoView1);
2.xml에서는 다음과 같이 선언( VideoView가 아님 )
<package.MyVideoView android:layout_width="fill_parent"android:layout_height="fill_parent" android:id="@+id/aa1" ////////////////////////////////////////////////////////////////////////////////////
//기타
html5 재생 :
http://nosmoke.cctoday.co.kr/1107
http://justdevelopment.blogspot.com/2009/10/video-streaming-with-android-phone.html
인코딩 구격
http://developer.android.com/guide/appendix/media-formats.html
'구글과인터넷 > 안드로이드' 카테고리의 다른 글
안드로이드 플레그먼트 레이아웃 관련 (0) | 2012.12.12 |
---|---|
안드로이드 ListView 리스트 특정위치 이동하기 (0) | 2012.12.12 |
안드로이드 개발 시 참고하면 좋은 정보(개발 정리) (0) | 2012.12.12 |