Androidは簡単な音楽プレーヤーを実現します。
本論文の例では、Androidの簡易音楽プレーヤーを共有しています。参考にしてください。具体的な内容は以下の通りです。
機能紹介
今回の実験はAndriod Studioを用いて音楽の再生、一時停止、前の曲、次の曲、音楽の再生の進捗及び手動ドラッグにより音楽の再生進捗を制御する簡易音楽プレーヤを開発しました。
実現プロセス
プロジェクトの導入に必要な音楽ファイル、アイコン、背景など
1.rawフォルダを作成し、音楽ファイルをこのフォルダに導入して、プロジェクトで使用しやすいです。
2.drawableに必要な画像、アイコンを導入する
UIインタフェースを設計する
1.5つのbuttonコントロールを設計して、それぞれ前の曲に対応して、次の曲、一時停止、再生、終了します。
2.TextViewを3つ設計し、それぞれ曲の紹介情報、曲の進捗(歌の総時間と歌の現在再生時間)、曲の名前に対応する。
サービスの作成
Music Serviceオブジェクトを作成してServiceを継承します。
Music Serviceに必要なメンバー変数
受信機を初期化し、動的にバインディングする
以上が本文の全部です。皆さんの勉強に役に立つように、私たちを応援してください。
機能紹介
今回の実験はAndriod Studioを用いて音楽の再生、一時停止、前の曲、次の曲、音楽の再生の進捗及び手動ドラッグにより音楽の再生進捗を制御する簡易音楽プレーヤを開発しました。
実現プロセス
プロジェクトの導入に必要な音楽ファイル、アイコン、背景など
1.rawフォルダを作成し、音楽ファイルをこのフォルダに導入して、プロジェクトで使用しやすいです。
2.drawableに必要な画像、アイコンを導入する
UIインタフェースを設計する
1.5つのbuttonコントロールを設計して、それぞれ前の曲に対応して、次の曲、一時停止、再生、終了します。
2.TextViewを3つ設計し、それぞれ曲の紹介情報、曲の進捗(歌の総時間と歌の現在再生時間)、曲の名前に対応する。
サービスの作成
Music Serviceオブジェクトを作成してServiceを継承します。
Music Serviceに必要なメンバー変数
MyReceiver serviceReceiver;
Thread processThread;
AssetManager am;// ,
String[] musics = new String[]{"legendsneverdie.mp3", "promise.mp3",
"beautiful.mp3"};//
MediaPlayer mPlayer;
// ,0x11 ;0x12 ;0x13
int status = 0x11;
//
int current = 0;
循環再生を実現
public void onCreate() {
super.onCreate();
am = getAssets();
// BroadcastReceiver
serviceReceiver = new MyReceiver();
// IntentFilter
IntentFilter filter = new IntentFilter();
filter.addAction(MainActivity.CTL_ACTION);
registerReceiver(serviceReceiver, filter);
// MediaPlayer
mPlayer = new MediaPlayer();
// MediaPlayer
mPlayer.setOnCompletionListener(new OnCompletionListener()
{
@Override
public void onCompletion(MediaPlayer mp) {
Log.d("musicService", " ");
current++;
if (current >= 3) {
current = 0;
}
//
prepareAndPlay(musics[current]);
// Activity
Intent sendIntent = new Intent(MainActivity.UPDATE_ACTION);
sendIntent.putExtra("current", current);
sendIntent.putExtra("currentTime", mPlayer.getCurrentPosition());
sendIntent.putExtra("totalTime", mPlayer.getDuration());
// , Activity BroadcastReceiver
sendBroadcast(sendIntent);
}
});
private void prepareAndPlay(String music) {
try {
//
AssetFileDescriptor afd = am.openFd(music);
mPlayer.reset();
// MediaPlayer 。
mPlayer.setDataSource(afd.getFileDescriptor(),
afd.getStartOffset(), afd.getLength());
//
mPlayer.prepare();
//
mPlayer.start();
} catch (IOException e) {
e.printStackTrace();
}
}
更新進捗バーを実現
processThread = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
if (status == 0x12) {
try {
Thread.sleep(1000);
Intent sendIntent = new Intent(MainActivity.UPDATE_ACTION);
sendIntent.putExtra("current", current);
sendIntent.putExtra("currentTime", mPlayer.getCurrentPosition());
sendIntent.putExtra("totalTime", mPlayer.getDuration());
// , Activity BroadcastReceiver
sendBroadcast(sendIntent);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
});
processThread.start();
}
ブロードキャスト通信受信機の実現(activityとの通信の実現)
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {
int control = intent.getIntExtra("control", -1);
Log.d("musicReceiver", " , control=" + control);
switch (control) {
//
case 1:
//
if (status == 0x11) {
//
prepareAndPlay(musics[current]);
status = 0x12;
}
//
else if (status == 0x12) {
//
mPlayer.pause();
//
status = 0x13;
}
//
else if (status == 0x13) {
//
mPlayer.start();
//
status = 0x12;
}
break;
//
case 2:
if (status == 0x12 || status == 0x13) {
mPlayer.stop();
if (current + 1 >= musics.length) {
current = 0;
} else {
current++;
}
prepareAndPlay(musics[current]);
status = 0x12;
break;
}
//
case 3:
if (status == 0x12 || status == 0x13) {
mPlayer.stop();
if (current - 1 < 0) {
current = musics.length - 1;
} else {
current--;
}
prepareAndPlay(musics[current]);
status = 0x12;
}
}
// Activity 、
Intent sendIntent = new Intent(MainActivity.UPDATE_ACTION);
sendIntent.putExtra("update", status);
sendIntent.putExtra("current", current);
// , Activity BroadcastReceiver
sendBroadcast(sendIntent);
}
}
activityの実現受信機を初期化し、動的にバインディングする
// 、
TextView title, author, currentTime, totalTime;
// / 、
ImageButton play;
ImageView lastMusic, nextMusic;
//
ProgressBar progressBar;
ActivityReceiver activityReceiver;
public static final String CTL_ACTION =
"org.xr.action.CTL_ACTION";
public static final String UPDATE_ACTION =
"org.xr.action.UPDATE_ACTION";
// ,0x11 ;0x12 ;0x13
int status = 0x11;
String[] titleStrs = new String[]{"Legends Never Die", " ", " "};
String[] authorStrs = new String[]{" ", " ", " "};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//
play = (ImageButton) this.findViewById(R.id.play);
lastMusic = this.findViewById(R.id.lastMusic);
nextMusic = this.findViewById(R.id.nextMusic);
title = (TextView) findViewById(R.id.title);
author = (TextView) findViewById(R.id.author);
currentTime = findViewById(R.id.currentTime);
totalTime = findViewById(R.id.totalTime);
progressBar = findViewById(R.id.progressBar);
//
play.setOnClickListener(this);
lastMusic.setOnClickListener(this);
nextMusic.setOnClickListener(this);
activityReceiver = new ActivityReceiver();
// IntentFilter
IntentFilter filter = new IntentFilter();
// BroadcastReceiver Action
filter.addAction(UPDATE_ACTION);
// BroadcastReceiver
registerReceiver(activityReceiver, filter);
Intent intent = new Intent(this, MusicService.class);
// Service
startService(intent);
}
activityの放送受信機を設定する(serviceから送信された放送を受信する)
public void onReceive(Context context, Intent intent) {
// Intent update ,update
int update = intent.getIntExtra("update", -1);
// Intent current ,current
int current = intent.getIntExtra("current", -1);
int totalPosition = intent.getIntExtra("totalTime", -1);
int currentPosition = intent.getIntExtra("currentTime", -1);
Log.d("activityReceiver", " ");
Log.d("activityReceiver", "current:" + current + " totalPosition:" + totalPosition + " currentPosition:" + currentPosition + " update:" + update);
if (current >= 0) {
title.setText(titleStrs[current]);
author.setText(authorStrs[current]);
}
if (totalPosition >= 0) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("mm:ss", Locale.CHINA);
Date date = new Date(totalPosition);
String formatTime = simpleDateFormat.format(date);
totalTime.setText(formatTime);
}
if (currentPosition >= 0) {
double process = ((double)currentPosition / totalPosition)*100;
Log.d("activityReceiver", " :" + (double)currentPosition/totalPosition);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("mm:ss", Locale.CHINA);
Date date = new Date(currentPosition);
String formatTime = simpleDateFormat.format(date);
progressBar.setProgress((int) process);
currentTime.setText(formatTime);
}
switch (update) {
case 0x11:
play.setImageResource(R.drawable.play);
status = 0x11;
break;
//
case 0x12:
//
play.setImageResource(R.drawable.pause);
//
status = 0x12;
break;
//
case 0x13:
//
play.setImageResource(R.drawable.play);
//
status = 0x13;
break;
}
}
アイコンのクリック機能を実現します。
// Intent
Intent intent = new Intent("org.xr.action.CTL_ACTION");
switch (source.getId()) {
// /
case R.id.play:
intent.putExtra("control", 1);
break;
case R.id.lastMusic:
intent.putExtra("control", 3);
case R.id.nextMusic:
intent.putExtra("control", 2);
}
// , Service BroadcastReceiver
sendBroadcast(intent);
}
結果の展示以上が本文の全部です。皆さんの勉強に役に立つように、私たちを応援してください。