Mediaplayer class
Static Constructor
Method Name |
Description |
Public static mediaplayer create (context, Uri, surfaceholder holder) |
Specify to load the music file from the resource file corresponding to the resource ID, specify the surfaceholder object, and return the mediaplyaer object. |
Public static mediaplayer create (context, int resid) |
Specify to load the music file from the resource file corresponding to the resource ID, and return the mediaplyaer object |
Public static mediaplayer create (context, Uri URI) |
Specify to load the music file from the resource file corresponding to the URI and return the mediaplyaer object. |
Common Methods
Method Name |
Description |
Public void start () |
Start or resume playback |
Public void stop () |
Stop playing |
Public void pause () |
Pause playback |
Public void setdatasource (string path) |
Objects represented by the specified path |
Public void setdatasource (filedescriptor FD, long offset, long length) |
Specify the content of the file represented by FD starting from offset and its length is length. |
Public void setdatasource (filedescriptor FD) |
Specifies the file to be loaded by FD |
Public void setdatasource (context, Uri URI) |
Specifies the file represented by the URI. |
Public void setdatasource (context, Uri, Map <string, string> headers) |
Specifies the file represented by the URI. |
Public void prepare () |
Expected preparation, because after the setdatasource () method, mediaplayer does not actually load those audio files, you need to call the prepare () method to prepare the audio |
Public void setlooping (Boolean looping) |
Sets whether to play this music file cyclically. |
Public void setsurface (surface) |
Set Surface |
Public void setvolume (float leftvolume, float rightvolume) |
Set volume |
Public void setdisplay (surfaceholder SH) |
Set Display Mode |
Public void seekto (INT MSEs) |
Seek the specified time location. |
Public void islooping () |
Determine whether to play cyclically |
Public void isplaying () |
Determine whether the video is being played |
Public void release () |
Releases resources related to the mediaplayer object. |
Bind event listener
Listener |
Description |
Public void setoncompletionlistener (mediaplayer. oncompletionlistener listener) |
Bind an event listener for the mediaplayer playback completion event |
Public void setonerrorlistener (mediaplayer. onerrorlistener listener) |
Bind an event listener to a playback error event of the mediaplayer. |
Public void setonpreparedlistener (mediaplayer. onpreparedlistener listener) |
This listener is triggered when mediaplayer calls the prepare () method. |
Public void setonseekcompletelistener (mediaplayer. onseekcompletelistener listener) |
This listener is triggered when mediaplayer calls the seek () method. |
Play audio
1. Play back the application's resource file (RES/raw /)
Create a mediaplayer object using the static constructor described above
MediaPlayer mediaPlayer = MediaPlayer.create(MediaPlayerDemoActivity.this, R.raw.a1);
2. Play back the original resource file (assets) of the application)
1) Use the context. getassets () method to obtain the assetmanager object
2) Open the specified native resource folder using the openfd (string name) method of the assetmanager object and return an assetfiledescriptor object.
3) Get a filedescriptor object through getfiledescriptor () of assetfiledescriptor.
4) create a mediaplayer object through public void setdatasource (filedescriptor FD, long offset, long length)
5) Call mediaplayer. Prepare () to prepare the audio.
6) Call mediaplayer's start (), pause (), stop () and other methods for control
AssetFileDescriptor fileDescriptor = assetManager .openFd("a2.mp3"); mediaPlayer = new MediaPlayer(); mediaPlayer.setDataSource(fileDescriptor.getFileDescriptor(), fileDescriptor.getStartOffset(), fileDescriptor.getLength()); mediaPlayer.prepare(); mediaPlayer.start();
3. Play the Audio Resource file (sdcard) on External Storage)
1) create a mediaplayer object and load the expected audio file using the setdatasource (string path) method of the mediaplayer object
2) Call the prepare () method of the mediaplayer object to prepare the audio.
3) Call mediaplayer's start (), pause (), stop () and other methods for control
mediaPlayer = new MediaPlayer(); mediaPlayer.setDataSource("/mnt/sdcard/a3.mp3"); mediaPlayer.prepare(); mediaPlayer.start();
4. play audio files on the network
1) create a URI object based on the address on the network
2) set the audio source to load the audio file through public void setdatasource (context, Uri URI)
3) Call the prepare () method of the mediaplayer object to prepare the audio.
4) Call mediaplayer's start (), pause (), stop () and other methods for control
Mediaplayer = new mediaplayer (); // parses a network address URI uri = URI. parse ("http://zhangmenshiting2.baidu.com/data2/music/9138619/9138619.mp3? Xcode = dd0d0d0df7218eb9a79c7fd72cefb648 & Mid = 0.18926789458694 "); mediaplayer. setdatasource (mediaplayerdemoactivity. This, Uri); mediaplayer. Prepare (); mediaplayer. Start ();
Of course, you can also directly create a mediaplayer object through another static constructor.
Uri uri = Uri .parse("http://zhangmenshiting2.baidu.com/data2/music/9138619/9138619.mp3?xcode=dd0d0d0df7218eb9a79c7fd72cefb648&mid=0.18926789458694"); mediaPlayer = MediaPlayer.create(MediaPlayerDemoActivity.this, uri); mediaPlayer.start();
Effect:
Paste the source code:
Package COM. jiahui. media; import Java. io. ioexception; public class mediaplayerdemoactivity extends activity implementsonclicklistener {private button btnappstart, btnassetsstart, btnsdcardstart, btnnetworkstart; Public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); btnappstart = (button) This. findviewbyid (R. id. btnappstart); btnassetsstart = (Bu Tton) This. findviewbyid (R. id. btnassetsstart); btnsdcardstart = (button) This. findviewbyid (R. id. btnsdcardstart); btnnetworkstart = (button) This. findviewbyid (R. id. btnnetworkstart); btnappstart. setonclicklistener (this); btnassetsstart. setonclicklistener (this); btnsdcardstart. setonclicklistener (this); btnnetworkstart. setonclicklistener (this) ;}@ overridepublic void onclick (view v) {mediaplayer = Null; Switch (v. GETID () {case R. id. btnappstart: system. out. println ("--- play Application resource file ----"); // use the resource file in raw to create the mediaplayer object mediaplayer = mediaplayer. create (mediaplayerdemoactivity. this, R. raw. a1); mediaplayer. start (); break; case R. id. btnassetsstart: system. out. println ("--- play the assest resource file ----"); assetmanager = This. getassets (); try {assetfiledescriptor filedescriptor = assetmanager. openfd ("a2.mp3"); me Diaplayer = new mediaplayer (); mediaplayer. setdatasource (filedescriptor. getfiledescriptor (), filedescriptor. getstartoffset (), filedescriptor. getlength (); mediaplayer. prepare (); mediaplayer. start ();} catch (ioexception e) {// todo auto-generated catch blocke. printstacktrace ();} break; case R. id. btnsdcardstart: system. out. println ("--- play the file on sdcard ---"); try {mediaplayer = new mediaplayer (); mediaplayer. setdat Asource ("/mnt/sdcard/a3.mp3"); mediaplayer. prepare (); mediaplayer. start ();} catch (exception e) {} break; case R. id. btnnetworkstart: system. out. println ("playing audio on the network"); // http://zhangmenshiting2.baidu.com/data2/music/9138619/9138619.mp3? // Xcode = repository & Mid = 0.18926789458694try {// mediaplayer = new mediaplayer (); // parse a network address URI uri = URI. parse ("http://zhangmenshiting2.baidu.com/data2/music/9138619/9138619.mp3? Xcode = dd0d0d0df7218eb9a79c7fd72cefb648 & Mid = 0.18926789458694 "); mediaplayer. setdatasource (mediaplayerdemoactivity. this, Uri); // mediaplayer. prepare (); // you can directly load the audio file mediaplayer = mediaplayer on the network in this way. create (mediaplayerdemoactivity. this, Uri); mediaplayer. start () ;}catch (exception e) {// todo: handle exception} break; default: Break ;}}}
If you need to reprint reference please indicate the source: http://blog.csdn.net/jiahui524
Source code download: http://download.csdn.net/detail/jiahui524/3847987