Android music player production scan local music display on mobile phone (I)
thinking
First, scan all local audio files, and then load them into the collection. Next, display them on the screen with listview. These are probably the steps. Let's talk about them in detail
Create a container
Friends who have done data analysis should know JavaBeans to load the parsed data. We also want to create a JavaBean here to load the scanned audio files. The specific code is:
Create a tool class to scan local audio
Class defines a method for scanning local audio, and its return value is a list collection. The generic contained in the collection is the JavaBean created in the previous step. The specific code is as follows:
Well, when we call the scanning method of this class, we can get a list collection containing local audio, but before that, we have to add a permission: let the program read the local files of the mobile phone and add permissions in androidmanifest.xlm, as follows:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Data display
Listview display data needs to be used together with the adapter, and the adapter needs to pass a context and a data source. The context can be obtained directly. We have obtained the data source. Next, we will write listview and adapter. First, look at the layout file of mainactivity, which is very simple:
The next step is to write in mainactivity. The specific code is as follows:
Each listview needs an item. The specific code is as follows:
Operation effect:
Demo download address: Music Player
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.