아래에 있는 것을 바탕으로 mpthree.mp3 라는 음악을 실행해 보고 싶습니다. 그래서
mp = MediaPlayer.create(context, R.raw.mpthree);
mp.start();
를 했는데 에뮬에서는 안돌아가는군요 .
( context 는 view 에서 사용하는데 Context context를 썻구요. )
( mpthree.mp3 는 res/raw라는 폴더 안에 넣었습니다.)
mp3 재생이 되는지 궁금합니다.
Playing from a Raw Resource
Perhaps the most common thing to want to do is play back media (notably sound) within your own applications. Doing this is easy:
- Put the sound (or other media resource) file into the
res/raw
folder of your project, where the Eclipse plugin (or aapt) will find it and make it into a resource that can be referenced from your R class - Create an instance of
MediaPlayer
, referencing that resource using MediaPlayer.create, and then call start() on the instance:
MediaPlayer mp = MediaPlayer.create(context, R.raw.sound_file_1); mp.start();
To stop playback, call stop(). If you wish to later replay the media, then you must reset() and prepare() the MediaPlayer object before calling start() again. (create()
calls prepare()
the first time.)
To pause playback, call pause(). Resume playback from where you paused with start().
오류가 납니다. mp3 wma 모두 해봤습니다. ㅠㅠ 왜 안될까요.. 뭔가 음악 파일을 못찾는것 같은데 ㅠ ..잘모르겠습니다.
public void setDataSource (String path)
Sets the data source (file-path or http/rtsp URL) to use. Call this after reset(), or before any other method (including setDataSource()) that might throw IllegalStateException in this class.
제가 path 설정을 하지 못하는 바람에 자꾸 에러가 나는것 같습니다.
path 에 file 로 하려면 R.raw.mpthree 로 하면 안되는지요.(mpthree.mp3 입니다.()
The method setDataSource(String) in the type MediaPlayer is not applicable for the arguments (int)
이런 오류가 납니다
R.raw.파일명은 int 값을 사용하는데 String 으로 받아야하니 제가 오류가 나는것 같습니다.
두번째 방법으로는(제가 문의했던 방식.)
public void setDataSource (Context context, Uri uri)
Sets the data source as a content Uri. Call this after reset(), or before any other method (including setDataSource()) that might throw IllegalStateException in this class.
이 있는데 이 방법으로 해서는 자꾸 오류가 나네요. context 문제인지 Uri 문제인지...
성공하신분 있으시면 특정 소스를 알려주시면 감사하겠습니다.
소스 + 파일 경로
mp.start();
처럼 MediaPlayer.create를 Raw Resource의 파일을 재생하는 것은 가능한 것 같습니다. (단 해당 파일은 지원되는 형식이어야 만.) 하지만 mp = new MediaPlayer() 방식으로 하는 것은 반드시 setDataSource를 해야만 prepare 단계로 가서 start 가 되는데 Raw Resource로는 접근이 안되네요. 회색님 말씀처럼 R.raw.xxxx를 path를 잡아 주던가 (불가능할 듯) SD card를 이용하여 그 음원(혹은 영상파일)을 복사하여 하던가 해야 겠네요. 아직도 R.raw.xxx 를 SetDataSource로 가져오는 방법을 찾고 있는 데 어렵습니다.
this.getResources().openRawResource(R.raw.mpthree);
MediaPlayer mp = MediaPlayer.create(context, R.raw.mpthree);
mp.start();
이런식으로 해주시면 될듯합니다.