Android music player.

Source: Internet
Author: User
Package com. king. android. media;

Import java. io. File;
Import java. io. FilenameFilter;
Import java. util. ArrayList;
Import java. util. List;

Import android. app. Activity;
Import android. media. MediaPlayer;
Import android. media. MediaPlayer. OnCompletionListener;
Import android. OS. Bundle;
Import android. OS. Environment;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. Toast;

Import com. king. android. R;

Public class MusicBActivity extends Activity implements OnClickListener {
Public static final String TAG = "MusicActivity ";
Static List <File> allFiles = new ArrayList <File> ();
Private int mCurrentJMusic = 0; // The current music.
Private MediaPlayer mMediaPlayer = null;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
RefreshFile (getSDCardPath (), ". mp3 ");
If (allFiles. size () <= 0 ){
Toast. makeText (MusicBActivity. this, "no songs", Toast. LENGTH_SHORT). show ();
Finish ();
}
MMediaPlayer = new MediaPlayer ();
InitView ();
}

Public String getSDCardPath (){
String sdPath = null;
Boolean hasCard = Environment. getExternalStorageState (). equals (Environment. MEDIA_MOUNTED );
If (hasCard ){
SdPath = Environment. getExternalStorageDirectory (). toString ();
} Else {
SdPath = Environment. getDataDirectory (). toString ();
}
Return sdPath;
}
Private void initView (){
SetContentView (R. layout. media_layout );

Button btnMedia = (Button) findViewById (R. id. btn_last );
BtnMedia. setOnClickListener (this );

BtnMedia = (Button) findViewById (R. id. btn_pause );
BtnMedia. setOnClickListener (this );

BtnMedia = (Button) findViewById (R. id. btn_stop );
BtnMedia. setOnClickListener (this );

BtnMedia = (Button) findViewById (R. id. btn_play );
BtnMedia. setOnClickListener (this );

BtnMedia = (Button) findViewById (R. id. btn_next );
BtnMedia. setOnClickListener (this );

}

@ Override
Public void onClick (View v ){
Switch (v. getId ()){
Case R. id. btn_play:
PlayMusic (allFiles. get (mCurrentJMusic ));
Break;

Case R. id. btn_next:
NextMusic ();
Break;
Case R. id. btn_last:
FrontMusic ();
Break;

Case R. id. btn_pause:
If (mMediaPlayer. isPlaying ()){
// Pause
MMediaPlayer. pause ();
} Else {
// Play
MMediaPlayer. start ();
}
Break;

Case R. id. btn_stop:
// Whether the video is being played
If (mMediaPlayer. isPlaying ()){
// Reset the MediaPlayer to the initial state.
MMediaPlayer. reset ();
}
Break;

}
}

Private void playMusic (File file ){
Try {
// Reset MediaPlayer
MMediaPlayer. reset ();
// Set the playback file path
// MMediaPlayer. setDataSource (file. getAbsolutePath () + File. separator + file. getName ());
MMediaPlayer. setDataSource (file. getAbsolutePath ());
// Play the file
MMediaPlayer. prepare ();
// Start playing
MMediaPlayer. start ();
MMediaPlayer. setOnCompletionListener (new OnCompletionListener (){
@ Override
Public void onCompletion (MediaPlayer mp ){
// After playing the video, start the next one.
NextMusic ();
}
});
} Catch (Exception e ){
E. printStackTrace ();
}
}

// The next one
Private void nextMusic (){
If (++ mCurrentJMusic> = allFiles. size ()){
MCurrentJMusic = 0;
} Else {
PlayMusic (allFiles. get (mCurrentJMusic ));
}
}

// Previous
Private void FrontMusic (){
If (-- mCurrentJMusic> = 0 ){
MCurrentJMusic = allFiles. size ();
} Else {
PlayMusic (allFiles. get (mCurrentJMusic ));
}
}

/*** Recursively retrieve the folder file * TODO: TODO * Author: Andy. liu * Create Time: 11:40:33 * TAG: @ param path file path, usually select the root directory * TAG: @ param fileType file type. * Return: void */

Public void refreshFile (String path, String fileType ){
File file = new File (path );
File [] files = file. listFiles ();
If (null = files)
Return;

For (int I = 0; I <files. length; I ++ ){
If (files [I]. isDirectory ()){
RefreshFile (files [I]. getAbsolutePath (), fileType); // note the Recursive Method
} Else {
If (files [I]. getName (). endsWith (fileType ))
AllFiles. add (files [I]);
}
}
}

}

 

There are many methods to obtain folders in folders, but the recursion method is the most efficient. You should use the best method to solve the problem...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.