이 샘플소스는 안드로이드 설치폴더에 android-sdk-windows/samples/android-8/ApiDemos 위치에 원본소스가 있습니다.
미디어 플레이어 객체를 생성하여 로컬 / 리소스 오디오 비디오 파일을 읽어들여서 재생처리를 합니다.
먼저 원하시는 패키지 경로에 MediaPlayerDemo.java 액티비티 클래스 파일을 추가합니다.
/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* 동영상 파일(비디오)과 음악파일(오디오)을 재생하는 미디어 플레이어를 제어를 합니다.
* /res/raw/test_cbr.mp3 음악파일을 재상하고 있습니다.
* /res/layout/mediaplayer_1.xml 레이아웃 파일을 액티비티의 레이아웃으로 적용합니다.
* 네개의 버튼으로 음악과 비디오 파일들을 리소스와 로컬파일에서 각각 읽어오도록 처리하고 있습니다.
*/
public class MediaPlayerDemo extends Activity {
private Button mlocalvideo;
private Button mresourcesvideo;
private Button mstreamvideo;
private Button mlocalaudio;
private Button mresourcesaudio;
private Button mstreamaudio;
private static final String MEDIA = "media";
private static final int LOCAL_AUDIO = 1;
private static final int STREAM_AUDIO = 2;
private static final int RESOURCES_AUDIO = 3;
private static final int LOCAL_VIDEO = 4;
private static final int STREAM_VIDEO = 5;
private static final int RESOURCES_VIDEO = 6;
/*
* 로컬오디오파일을 실행하는 미디어플레이어를 시작하는 액티비티를 띄웁니다.
*/
private OnClickListener mLocalAudioListener = new OnClickListener() {
public void onClick(View v) {
Intent intent =
new Intent(MediaPlayerDemo.this.getApplication(),
MediaPlayerDemo_Audio.class);
intent.putExtra(MEDIA, LOCAL_AUDIO);
startActivity(intent);
}
};
/*
* 리소스오디오파일을 실행하는 미디어플레이어를 시작하는 액티비티를 띄웁니다.
*/
private OnClickListener mResourcesAudioListener = new OnClickListener() {
public void onClick(View v) {
Intent intent =
new Intent(MediaPlayerDemo.this.getApplication(),
MediaPlayerDemo_Audio.class);
intent.putExtra(MEDIA, RESOURCES_AUDIO);
startActivity(intent);
}
};
/*
* 로컬비디오파일을 실행하는 미디어플레이어를 시작하는 액티비티를 띄웁니다.
*/
private OnClickListener mLocalVideoListener = new OnClickListener() {
public void onClick(View v) {
Intent intent =
new Intent(MediaPlayerDemo.this,
MediaPlayerDemo_Video.class);
intent.putExtra(MEDIA, LOCAL_VIDEO);
startActivity(intent);
}
};
/*
* 스트리밍비디오파일을 실행하는 미디어플레이어를 시작하는 액티비티를 띄웁니다.
*/
private OnClickListener mStreamVideoListener = new OnClickListener() {
public void onClick(View v) {
Intent intent =
new Intent(MediaPlayerDemo.this,
MediaPlayerDemo_Video.class);
intent.putExtra(MEDIA, STREAM_VIDEO);
startActivity(intent);
}
};
}
오디오 파일을 재생하는 액티비티 파일인 MediaPlayerDemo_Audio.java 를 원하시는 패키지 경로에 추가합니다.
/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
public class MediaPlayerDemo_Audio extends Activity {
private static final String TAG = "MediaPlayerDemo";
private MediaPlayer mMediaPlayer;
private static final String MEDIA = "media";
private static final int LOCAL_AUDIO = 1;
private static final int STREAM_AUDIO = 2;
private static final int RESOURCES_AUDIO = 3;
private static final int LOCAL_VIDEO = 4;
private static final int STREAM_VIDEO = 5;
private String path;
private TextView tx;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
tx = new TextView(this);
setContentView(tx);
Bundle extras = getIntent().getExtras();
//액티비티에서 로컬버튼인지 리소스버튼인지에 따라서 액태비티의 추가데이터 설정한 값을 가져와서 해당 처리를 합니다.
playAudio(extras.getInt(MEDIA));
}
private void playAudio(Integer media) {
try {
switch (media) {
case LOCAL_AUDIO:
/**
* TODO: Set the path variable to a local audio file path.
*/
path = "";
if (path == "") {
// Tell the user to provide an audio file URL.
Toast
.makeText(
MediaPlayerDemo_Audio.this,
"Please edit MediaPlayer_Audio Activity, "
+ "and set the path variable to your audio file path."
+ " Your audio file must be stored on sdcard.",
Toast.LENGTH_LONG).show();
}
//미디어 플레이어 객체생성
mMediaPlayer = new MediaPlayer();
//오디오파일경로 설정
mMediaPlayer.setDataSource(path);
mMediaPlayer.prepare();
mMediaPlayer.start();
break;
case RESOURCES_AUDIO:
/**
* TODO: Upload a audio file to res/raw folder and provide
* its resid in MediaPlayer.create() method.
*/
//오디오 리소스파일에서 미디오플레이어를 생성하여, 재생시작처리
mMediaPlayer = MediaPlayer.create(this, R.raw.test_cbr);
mMediaPlayer.start();
@Override
protected void onDestroy() {
super.onDestroy();
// TODO Auto-generated method stub
if (mMediaPlayer != null) {
//미디어 플레이어 해제..
mMediaPlayer.release();
mMediaPlayer = null;
}
}
}
비디오 파일을 재생하는 MediaPlayerDemo_Video.java 파일을 원하시는 경로에 추가해줍니다.
/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* 비디오 파일을 재생하는 클래스 입니다.
* 각종 미디어 플레이어 관련된 인터페이스들을 구현하여,
* 각 인터페이스들의 메소드들을 구현하면서, 미디어 플레이어를 제어하고 있습니다.
*/
public class MediaPlayerDemo_Video extends Activity implements
OnBufferingUpdateListener, OnCompletionListener,
OnPreparedListener, OnVideoSizeChangedListener, SurfaceHolder.Callback {
private static final String TAG = "MediaPlayerDemo";
private int mVideoWidth;
private int mVideoHeight;
private MediaPlayer mMediaPlayer;
private SurfaceView mPreview;
private SurfaceHolder holder;
private String path;
private Bundle extras;
private static final String MEDIA = "media";
private static final int LOCAL_AUDIO = 1;
private static final int STREAM_AUDIO = 2;
private static final int RESOURCES_AUDIO = 3;
private static final int LOCAL_VIDEO = 4;
private static final int STREAM_VIDEO = 5;
private boolean mIsVideoSizeKnown = false;
private boolean mIsVideoReadyToBePlayed = false;
/**
*
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.mediaplayer_2);
mPreview = (SurfaceView) findViewById(R.id.surface);
holder = mPreview.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
extras = getIntent().getExtras();
switch (Media) {
case LOCAL_VIDEO:
/*
* TODO: Set the path variable to a local media file path.
*/
path = "";
if (path == "") {
// Tell the user to provide a media file URL.
Toast
.makeText(
MediaPlayerDemo_Video.this,
"Please edit MediaPlayerDemo_Video Activity, "
+ "and set the path variable to your media file path."
+ " Your media file must be stored on sdcard.",
Toast.LENGTH_LONG).show();
}
break;
case STREAM_VIDEO:
/*
* TODO: Set path variable to progressive streamable mp4 or
* 3gpp format URL. Http protocol should be used.
* Mediaplayer can only play "progressive streamable
* contents" which basically means: 1. the movie atom has to
* precede all the media data atoms. 2. The clip has to be
* reasonably interleaved.
*
*/
path = "";
if (path == "") {
// Tell the user to provide a media file URL.
Toast
.makeText(
MediaPlayerDemo_Video.this,
"Please edit MediaPlayerDemo_Video Activity,"
+ " and set the path variable to your media file URL.",
Toast.LENGTH_LONG).show();
}
break;
}
// Create a new media player and set the listeners
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDataSource(path);
mMediaPlayer.setDisplay(holder);
mMediaPlayer.prepare();
mMediaPlayer.setOnBufferingUpdateListener(this);
mMediaPlayer.setOnCompletionListener(this);
mMediaPlayer.setOnPreparedListener(this);
mMediaPlayer.setOnVideoSizeChangedListener(this);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);