Dahne Course-Music player v1.0 (top)

Source: Internet
Author: User
Tags constant

permissions in a Linux system
Using a 10-bit string in a Linux system to represent access to files/folders, such as-rwxr--r--

1th digit representation type
D indicates folder (directory)
L means link (. link)
-Indicates default, that is, file

第2-4位 represents the access rights of the owner of the file
These three-bit characters are R, W, X, respectively read (read), Write, execute (execute)
If each character has a letter at the corresponding position, the corresponding permission is indicated. If-, indicates that no permissions are available

第5-7位 represents the access rights of other users in the same group as the owner of the file

第8-10位 represents the access rights of other users

The default administrator account (root) is not restricted by any permissions

access Rights
The Android app performs sensitive operations (involving privacy, cost, potential security risks ...). ), you need to declare access rights, or you may not be able to perform related actions or even direct errors

declaring Permissions
Use node to claim permissions before nodes of the Androidmanifest.xml file, and add multiple nodes if multiple permissions are used

For example, access to SDcard requires a declaration of permissions:

<!--allows mount and anti-mount file system Removable Storage--
<uses-permission android:name= "android.permission.MOUNT_UNMOUNT_ Filesystems "/>
<!--read external storage permissions--
<uses-permission android:name=" android.permission.READ_ External_storage "/>

a simple music playback and stop function is implemented first

Ready to work: Open Ddms and put music in the following path

public class Mainactivity extends Appcompatactivity {private Button btnplay;
    Private Button btnstop;
    The full path, beginning with "/" cannot be lost, and the path is strictly case-sensitive String Musicpath = "/storage/emulated/0/music/akia-california.mp3";

    Private MediaPlayer player;
        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);

        Setcontentview (R.layout.activity_main);

        Inneronclicklistener listener = new Inneronclicklistener ();
        Btnplay = Findviewbyid (R.id.btn_play);

        Btnplay.setonclicklistener (listener);
        Btnstop = Findviewbyid (r.id.btn_stop);

        Btnstop.setonclicklistener (listener);
    Player = new MediaPlayer (); } Private class Inneronclicklistener implements View.onclicklistener {@Override public void OnClick (View view)
                        {switch (View.getid ()) {case R.id.btn_play:try {
                        Resetting the Playback toolPlayer.reset ();
                        Set the path to play the song Player.setdatasource (Musicpath);
                        Ready to load the song, that is, buffer//song in SD card, need to read into memory Player.prepare ();
                    Play Player.start ();
                } catch (IOException e) {} break;
                    Case R.id.btn_stop:player.stop ();
            Break }
        }
    }
}

traversing files under SD card

public class Mainactivity extends appcompatactivity {@Override protected void onCreate (Bundle savedinstancestate
        ) {super.oncreate (savedinstancestate);

        Setcontentview (R.layout.activity_main); "Target" scan the music folder under SD card, get all song information//1, get the file object of the Music folder under SDcard///2, check whether the music folder exists//3, get the files under the music folder List//4, check whether the file list is valid//5, traverse the file list//5.1, check if it is a file//5.2, check if it is a mp3 file String Musicpath = "/s
        Torage/emulated/0/music/";

        File Musicdir = new file (Musicpath);
            if (musicdir.exists ()) {LOG.D ("MUSIC", "Information" folder exists ");
            file[] files = musicdir.listfiles ();
                if (Files! = null && files.length>0) {log.d ("MUSIC", "Information" Folder List is valid "); for (int i=0;i<files.length;i++) {if (Files[i].isfile ()) {String fileName = f
                        Iles[i].getname ();
    if (Filename.tolowercase (). EndsWith (". mp3")) {                        LOG.D ("MUSIC", Files[i].getabsolutepath ());
                        }else{LOG.W ("MUSIC", "error" is not currently traversed MP3 file ");
                    }}else{LOG.W ("MUSIC", "error" is currently traversed to the "+i+" is not a file ");
            }}}else {LOG.W ("MUSIC", "Error" Folder List is invalid ");
        }}else{LOG.W ("MUSIC", "error" folder does not exist "); }
    }
}

Output results

Environment Class
The Environment class is a tool class dedicated to accessing external storage, which provides a series of static methods and constants for easy access to external storage

File Getexternalstorage ()//Get SDcard Files Object
File Getexternalstoragepublicdirectory (String)//Gets the default folder under SDcard, such as the file object for the music folder, and the parameter is a constant of the environment class. such as Directory_music

Different mobile phone sdcard path, in order to ensure that you can find SDcard path, you can put the above pest

String Musicpath = "/storage/emulated/0/music/";
File Musicdir = new file (Musicpath);

Switch

File Musicdir = environment.getexternalstoragepublicdirectory (Environment.directory_music);

String getexternalstoragestate ();//Gets the state of SDcard, if SDcard is already normal mount can be used, then the value returned should be matched with the constant media_mounted in environment

Every cell phone has sdcard, whether your phone is 32GB or 64GB, mobile phone storage space is divided into primary storage space and extended storage space, then this extended storage space is sdcard, so sdcard is not necessarily SD card

SDcard unavailable State for example: When a phone is linked to a computer, the sdcard of the phone is not available because both ends cannot operate simultaneously on the same file

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.