Android training project: based on memory card music player V05 (training log)

The first stage of the training project: based on the storage card music player V05

(1) Functional requirements
Based on the card-based music player V0.4, make the following modifications:

1. Create an application constant interface to save the broadcast channel constants 2. Add two properties in the music player application class
-currentMusicIndex
-currentPosition
-methods to access them
3. Create a MusicPlayService to complete music playback, pause and switch work
4. Use broadcasting to realize the communication between MainActivity and MusicPlayService

(2) Running effect
Insert picture description here
(3) Involving knowledge points
1. Label (TextView)
2. Button (Button)
3. Media player (MediaPlayer)
4. Progress bar (ProgressBar)
5. Thread (Thread)
6. Message processor ( Handler
7. List view (ListView)
8. Asynchronous task (AsyncTask)
9. Application (Application)
10 Service (Service)
11. Broadcast receiver (BroadcastReceiver)
(4) Implementation steps
1. Copy the picture material to the drawable directory
Insert picture description here
Insert picture description here
2. Create button background image selector with mipmap directory
(1) Play button background image selector-play_button_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/play_button_pressed" android:state_pressed="true" />
    <item android:drawable="@drawable/play_button" android:state_pressed="false" />
</selector>

(2) Pause button background image selector-pause_button_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/pause_button_pressed" android:state_pressed="true" />
    <item android:drawable="@drawable/pause_button" android:state_pressed="false" />
</selector>

(3) Previous button background image selector-previous_button_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/previous_button_pressed" android:state_pressed="true" />
    <item android:drawable="@drawable/previous_button" android:state_pressed="false" />
</selector>

(4) Next button background image selector-next_button_selector.xml

?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/next_button_pressed" android:state_pressed="true" />
    <item android:drawable="@drawable/next_button" android:state_pressed="false" />
</selector>

3. Authorize access to the external memory card in the project manifest file and set the application icon
Insert picture description here
4. The main layout resource file activity_main.xml
Insert picture description here

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    android:orientation="vertical"
    android:padding="20dp"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ProgressBar
            android:id="@+id/pbScanMusic"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:visibility="gone" />

        <TextView
            android:id="@+id/tvScanMusic"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/scan_music"
            android:textColor="#0000ff"
            android:textSize="25sp"
            android:visibility="gone" />
    </LinearLayout>

    <ListView
        android:id="@+id/lvMusicName"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginBottom="16dp"
        android:layout_weight="8" />

    <TextView
        android:id="@+id/tvMusicName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:textColor="#0000ff"
        android:textSize="20sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:layout_weight="0.5"
        android:gravity="center"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tvCurrentPosition"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textColor="#ff0000" />

        <ProgressBar
            android:id="@+id/pbMusicProgress"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="6" />

        <TextView
            android:id="@+id/tvDuration"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textColor="#ff00ff" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:gravity="center"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btnPrevious"
            android:layout_width="60dp"
            android:layout_height="50dp"
            android:background="@drawable/previous_button_selector"
            android:onClick="doPrevious" />

        <Button
            android:id="@+id/btnPlayOrPause"
            android:layout_width="60dp"
            android:layout_height="50dp"
            android:background="@drawable/play_button_selector"
            android:onClick="doPlayOrPause" />

        <Button
            android:id="@+id/btnNext"
            android:layout_width="60dp"
            android:layout_height="50dp"
            android:background="@drawable/next_button_selector"
            android:onClick="doNext" />
    </LinearLayout>
</LinearLayout>

5. String resource file strings.xml
Insert picture description here
6. Create music name list item template music_name_list_item.xml
Insert picture description here
7. Create entity sub-package and create music entity class in it-Music

Insert picture description here
8. Create an app sub-package and create a music player application class in it-MusicPlayerApplication
Insert picture description here
9. Create an adapter sub-package and create a music adapter in it-MusicAdapter
Insert picture description here
10. Common application constant interfaces in the app sub-package-AppConstants
Insert picture description here

package net.hw.sdcard_musicplayer_v05.app;

/**
 * 功能:应用程序常量接口
 * 作者:谭金兰
 * 日期:2021年01月06日
 */
public interface AppConstants {
    String TAG = "net.hw.sdcard_musicplayer_v05"; // 应用程序标记
    String INTENT_ACTION_PREVIOUS = TAG + ".intent.action.PREVIOUS"; // 广播频道常量:播放上一首
    String INTENT_ACTION_PLAY = TAG + ".intent.action.PLAY"; // 广播频道常量:播放
    String INTENT_ACTION_PLAY_OR_PAUSE = TAG + ".intent.action.PLAY_OR_PAUSE"; // 广播频道常量:播放或暂停
    String INTENT_ACTION_NEXT = TAG + ".intent.action.NEXT"; // 广播频道常量:播放下一首
    String INTENT_ACTION_UPDATE_PROGRESS = TAG + ".intent.action.UPDATE_PROGRESS"; // 广播频道常量:更新播放进度
    String CONTROL_ICON = "control_icon"; // 控制图标名称常量
    String DURATION = "duration"; // 播放时长名称常量
    String CURRENT_POSITION = "current_position"; // 当前播放位置名称常量
}

11. Create a service sub-package, and create a music playback service class in it-MusicPlayService
Insert picture description here
12. Main interface class-MainActivity
(1) Declare variables
Insert picture description here
Start the application and check the effect
Insert picture description here

Guess you like

Origin blog.csdn.net/triet/article/details/112443645