|
|
楼主 |
发表于 2010-11-17 15:03:04
|
显示全部楼层
本帖最后由 demo 于 2010-11-18 07:09 编辑
Now we should be able to build a list of all the MP3 files in /sdcard. So now we just need to be able to select a song and play it. Fist things first, let's override onListItemClick() so we will be notified when a song is clicked on:
51. @Override
52. protected void onListItemClick(ListView l, View v, int position, long id) {
53. currentPosition = position;
54. playSong(MEDIA_PATH + songs.get(position));
55. }
Pretty basic function here. We set currentPosition to hold the index of the position that was clicked on and then pass in the path of the song to playSong(String), so lets take a look at what's happening in playSong(String):
57. private void playSong(String songPath) {
58. try {
59.
60. mp.reset();
61. mp.setDataSource(songPath);
62. mp.prepare();
63. mp.start();
64.
65. // Setup listener so next song starts automatically
66. mp.setOnCompletionListener(new OnCompletionListener() {
67.
68. public void onCompletion(MediaPlayer arg0) {
69. nextSong();
70. }
71.
72. });
73.
74. } catch (IOException e) {
75. Log.v(getString(R.string.app_name), e.getMessage());
76. }
77. }
The MediaPlayer object makes things really easy for us here. First we call mp.reset(), which will reset the MediaPlayer to its normal state. This is required if you were playing a song and want to change the data source. The reset() function will also stop whatever is playing, so if a song is playing and then you select another it will stop that one before starting the next song.
We then pass in the path to the song to mp.setDataSource(String) and call prepare() and start(). At this point the MediaPlayer will start playing your song.
Next job is to setup an OnCompletionListener starting on line 66. The function onCompletion(MediaPlayer) will be called when the song is over. All we do there is call the function nextSong() from our Activity. Here is nextSong():
79. private void nextSong() {
80. if (++currentPosition >= songs.size()) {
81. // Last song, just reset currentPosition
82. currentPosition = 0;
83. } else {
84. // Play next song
85. playSong(MEDIA_PATH + songs.get(currentPosition));
86. }
87. }
Here we check to make sure this isn't the last song on the list, if it is we won't do anything, if not we'll play the next song using the playSong(String) function.
So that's it for the code, on the next page we'll figure out how to get this thing running...
Emulator Command Line Options
There are 2 command line options that must be set for this project to work. One to mount an SD card so that you can access it at "/sdcard" and another to enable audio.
To enable the SD card first you need to create an sd card image, to do this you need to open a command prompt in your SDK/tools folder and type the following command:
mksdcard <size>M <file>
Where <size> is the size of the SD card in MB, and this must be reasonably large as a small SD card image actually crashes the emulator. And <file> is the path to the file to create. This is the command that I used:
mksdcard 128M c:\temp\sd.img
Now your SD card image is setup, so once you have your project setup you will need to supply the 2 command line arguments. So, in Eclipse go to "Run -> Open Run Dialog". Now you may need to create a new configuration if there is not one there for your application, you can do that by double clicking on "Android Application" and supplying the Name, Project, and starting Activity on the "Android" tab. On the "Emulator" tab you need to supply 2 arguments, "-sdcard <file>" and "-useaudio".
It should look something like this:
file:///C:/DOCUME%7E1/Jzhang/LOCALS%7E1/Temp/moz-screenshot.png

So now that this is all setup we should be ready to run this thing right? Doh, here's what we see:

We need to add songs to the SD card for this thing to work. This is pretty easy, just open a command prompt in the SDK/tools folder once again and use the "adb push <file> <destination>" command. For example:
adb push "c:\music\02 Paranoid Android.mp3" /sdcard
And after you push a song or many songs out there you'll be able to start using this very primitive music player.
What do you have to look forward to in the next installments?
* Creating a service for the MediaPlayer
* Adding animated controls
* ID3 Tag Support
So, be sure to check back!
|
|