Android.media.MediaPlayer
안드로이드를 사용한 음악재생에 간단하게는 두가지를 사용할 수 있다.
재생시간이 짧은 효과음등을 재생할때는 android.media.SoundPool을 사용할 수 있겠고, 재생시간이 긴 배경음악등을 재생할때는 android.media.MediaPlayer를 쓸 수 있겠다.
음악파일은 res/raw
디렉터리에 위치시키면 된다.
StreamingMediaPlayer
android.media.MediaPlayer does not yet support streaming from external URLs so this class provides a pseudo-streaming function by downloading the content incrementally & playing as soon as we get enough audio in our temporary storage: http://code.google.com/p/mynpr/source/browse/trunk/mynpr/src/com/webeclubbin/mynpr/StreamingMediaPlayer.java?spec=svn9&r=9
MediaPlayer로 다운 받은 파일이 Play 안되는 문제
이 곳에서 아래와 같은 내용을 찾을 수 있다.
// you store your tempfile in the application cache directory and the problem is that the MediaPlayer doesn't have access rights to application directories.
// Instead you should passing a FileDescriptor to your MediaPlayer with setDataSource(FileDescriptor fd).
// Something like this:
// ...
FileInputStream fis = new FileInputStream(bufferedFile);
FileDescriptor fd = fis.getFD();
mediaPlayer.setDataSource(fd);
이 내용을 바탕으로 아래와 같이 작성할 수 있다.
try {
MediaPlayer mediaPlayer = new MediaPlayer();
FileInputStream fis = new FileInputStream(pathinlocal+FileName);
FileDescriptor fd = fis.getFD();
mediaPlayer.setDataSource(fd);
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IOException e) {
// handle exception
//((EditText) ((Activity) mMain).findViewById(R.id.txtStatus)).setText(e.getMessage());
}
Android Camera(캠코더) 프로파일을 반환방법
Nexus S 등에서 전면카메라로 촬영했을 경우 정상적으로 작동하지 않는 현상이 발생할 수 있다. 이런 경우 퀄리티를 LOW로 설정하거나, 카메라 ID를 설정해야 한다.
/** 캠코더의 캠코더 프로파일을 반환한다. */
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
private static CamcorderProfile getCamcorderProfile(int cameraId) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
return CamcorderProfile.get(cameraId, CamcorderProfile.QUALITY_HIGH);
} else if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
return CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
}
return null;
}
Favorite site
- Android Developer: MediaPlayer class reference
- MediaPlayer/SoundPool/간단한 미디어 재생 (mp3, ogg, wav 등) 1
- Custom Progressive Audio Streaming with MediaPlayer
- SoundPool과 MediaPlayer의 비교
- Supported Media Formats
- MP4 on Android (안드로이드 지원 비디오/오디오 포맷)
References
-
Android-mediaplayer_vs_soundpool.zip ↩