トレーニングプロジェクトの最初の段階:ストレージカード音楽プレーヤーV05に基づく
(1)機能要件
カードベースの音楽プレーヤーV0.4に基づいて、次の変更を行います。
1.放送チャネル定数を保存するアプリケーションの定数インターフェイスを作成し、音楽プレーヤーアプリケーションクラスの2つのプロパティを追加します。2.
-currentMusicIndex
-currentPosition
-methodsアクセスにそれらは
完全な音楽再生、一時停止、およびスイッチの仕事にMusicPlayServiceを作成します。3.
4.をMainActivityとMusicPlayService間の通信を実現するためのブロードキャスト
(2)ランニング効果
(3)知識ポイントの関与
1.ラベル(TextView)
2。ボタン(ボタン)
3。メディアプレーヤー(MediaPlayer)
4。プログレスバー(ProgressBar)
5。スレッド(スレッド)
6。メッセージプロセッサー(ハンドラー
7 。リストビュー(ListView)
8。非同期タスク(AsyncTask)
9。アプリケーション(アプリケーション)
10サービス(サービス)
11。ブロードキャストレシーバー(BroadcastReceiver)
(4)実装手順
1.画像素材をドローアブルディレクトリにコピーし
ます2.作成ボタンmipmapディレクトリを備えた背景画像セレクター
(1)再生ボタン背景画像セレクター-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_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_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_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.プロジェクトマニフェストファイルで外部メモリカードへのアクセスを承認し、アプリケーションアイコンを設定します
。4。メインレイアウトリソースファイルactivity_main.xml
<?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.文字列リソースファイルstrings.xml6
。音楽名リストアイテムテンプレートを
作成しますmusic_name_list_item.xml7 。エンティティサブパッケージを作成し、その中に音楽エンティティクラスを作成します-音楽
8.アプリサブパッケージを作成し、その中に音楽プレーヤーアプリケーションクラスを
作成します-MusicPlayerApplication 9.アダプターサブパッケージを作成し、その中に音楽アダプターを作成します-MusicAdapter10
。アプリサブパッケージ内の共通アプリケーション定数インターフェイス-AppConstants
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.サービスサブパッケージを作成し、その中に音楽再生サービスクラスを作成します
-MusicPlayService 12.メインインターフェイスクラス-MainActivity
(1)変数
を宣言しますアプリケーションを起動して効果を確認します