Android Implementation Music Player (i)

Source: Internet
Author: User

Graphical User Interface

This article documents the process of implementing my Android simple music player, and (a) describes how to build a front-end page of a music player. First of all, look at the end of the interface is this (the interface is a bit rude)

The music file list is read from the SD card.

First we create the music player's Gui,diagram in layout as shown here:

According to diagram we write the following code in Activity_main.xml:

<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:id= "@+id/container"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"Tools:context= "Com.example.lab5.MainActivity"Tools:ignore= "Mergerootframe" >    <ListViewAndroid:id= "@+id/listview"Android:layout_width= "Fill_parent"Android:layout_height= "350DP" >    </ListView>    <SeekBarAndroid:id= "@+id/seekbar"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content" />    <LinearLayoutAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"android:orientation= "Horizontal"android:gravity= "Center_horizontal" >        <ButtonAndroid:id= "@+id/play"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Play" />        <ButtonAndroid:id= "@+id/pause"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Pause" />        <ButtonAndroid:id= "@+id/stop"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Stop" />    </LinearLayout></LinearLayout>

If you write XML, the general layout is like this. The next thing we do is to read the contents of the SD card, the first step, first push music files to the virtual machine SD card inside.

Important thing to say in advance: When you create AVD, you must remember to assign SDcard size!!

1. Configure the ADB environment &&push music files

Find the path to Adb.exe, you can right-click on the desktop Eclipse, and then find "Open file location", adb in the Sdk\platform-tools, the path is copied out. Desktop-My Computer-right-properties-Advanced system settings-environment variables-User Variables-path

Click Edit, after path + ";" +adb path, remember to add a semicolon and the front area separately, click OK, has been determined down on the good, here is the WIN10 configuration, other operating systems reference Baidu experience. Next, we can push the music band SDcard in the command line way.

Win+r Open Run, enter cmd Open command Line window, we first into the virtual machine sdcard inside to see, the premise must be sdcard read and write permission, type adb shell,enter after typing "# mount-o remount rw/" Enter, You can exit by typing exit, then open the Command window again, and then click

Enter the ADB shell-cd MNT-CD sdcard-ls to view the contents of the SDcard.

Cd:call Director;

Ls:list director Contents;

Mkdir:make a new directory;

We can see that there are many subfolders under the SDcard folder, the system is built by default, we see a music folder, we put it in here, you can also use the command mkdir+ file name, create a new subfolder under SDcard. Then exit the Command window. Start push, chicken freeze! First or the ADB shell,enter after the input adb push c:\cloudmusic\chuanshuo.mp3/mnt/sdcard/music/return to see the speed of uploading is representative of success. The front is where my music file is located, and the space is followed by the location of the AVD to be copied to. After uploading, you can view the Music folder list using the SDcard view above.

2, OK, now sdcard inside already have music file, next we in Androidmanifest manifest file, add read-write SDcard XML code:

<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name= "Android.permission.READ_EXTERNAL_STORAGE"/>

3, the code in the Mainactivity

 Public classMainactivityextendsActivity {ArrayList<String> files =NewArraylist<string> ();//an array of song namesListView LV;//Define a ListView and associate it with the ListView inside the layout@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); LV=(ListView) Findviewbyid (R.id.listview); /*Adapter for the ListView*/Arrayadapter<String> adapter =NewArrayadapter<string> ( This, Android.        R.layout.simple_expandable_list_item_1, GetData ()); /*add Arrayadapter to the ListView object*/Lv.setadapter (adapter); }    PrivateArraylist<string>GetData () {File Sdcarddir=NewFile ("Mnt/sdcard/music"); File[] MP3s=Sdcarddir.listfiles (); if(mp3s = =NULL|| Mp3s.length = = 0) {Toast.maketext (Getapplicationcontext (),"No File", Toast.length_long). Show (); } Else {             for(inti = 0; i < mp3s.length; i++) {Files.add (Mp3s[i].getname ()); }        }        returnfiles; }}

4, the operation is OK.

Android Implementation Music Player (i)

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.