Android開(kāi)發(fā)簡(jiǎn)易音樂(lè)播放器
這里介紹一個(gè)簡(jiǎn)易的音樂(lè)播放器,供大家參考,具體內(nèi)容如下
效果圖如下:
但是,由于這是一個(gè)簡(jiǎn)易版的音樂(lè)播放器,所播放的音樂(lè)只有一首,且被寫(xiě)死,但,操作卻十分簡(jiǎn)單,方便理解!
這是代碼的主要設(shè)計(jì):
音樂(lè)主要存放在這一個(gè)文件中:
下面就來(lái)介紹各部分代碼:
activity—main。xml:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#696969"> <LinearLayout android:layout_width="match_parent" android:layout_height="60dp" android:layout_marginTop="10dp" android:layout_alignParentTop="true" android:id="@+id/title" android:orientation="horizontal"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:layout_marginBottom="3dp" android:text="生僻字" android:textSize="25dp" android:gravity="center" android:textColor="#ffffff"/> </LinearLayout> <TextView android:layout_width="match_parent" android:layout_height="0.5dp" android:background="#afafaf" android:layout_below="@+id/title"/> <ImageView android:id="@+id/disc" android:layout_width="280dp" android:layout_height="280dp" android:layout_centerHorizontal="true" android:layout_below="@+id/title" android:layout_marginTop="50dp" android:src="@drawable/xcvb" /> <ImageView android:id="@+id/needle" android:layout_width="120dp" android:layout_height="120dp" android:layout_below="@+id/title" android:layout_marginLeft="150dp"/> <RelativeLayout android:id="@+id/music1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_above="@+id/rl" android:layout_marginTop="20dp" android:layout_marginBottom="10dp" android:gravity="center"> <SeekBar android:id="@+id/music_seek_bar" android:layout_width="240dp" android:layout_height="wrap_content"/> <TextSwitcher android:id="@+id/text_switcher" android:layout_width="80dp" android:layout_height="50dp" android:layout_toRightOf="@+id/music_seek_bar"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="00:00/2:00" android:textColor="@color/colorAccent"/> </TextSwitcher> </RelativeLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="70dp" android:gravity="center" android:id="@+id/rl" android:layout_marginBottom="20dp" android:layout_alignParentBottom="true" android:orientation="horizontal"> <ImageView android:id="@+id/playing_pre" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:src="@drawable/music_previous" /> <ImageView android:id="@+id/playing_play" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:src="@drawable/music_play" /> <ImageView android:id="@+id/playing_next" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:src="@drawable/music_next" /> </LinearLayout> </RelativeLayout>
main。activity部分:
package com.example.cungu.musicdemo;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Build;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextSwitcher;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MainActivity extends AppCompatActivity implements View.OnClickListener,
Runnable, ServiceConnection, SeekBar.OnSeekBarChangeListener {
private ImageView disc,needle,playingPre,playingPlay,playingNext;
private ObjectAnimator discAnimation,needleAnimation;//自定義指針和唱盤
private boolean isPlaying = true;//0,1 判斷是否處于播放狀態(tài)
//聲明服務(wù)
private static final String TAG = MainActivity.class.getSimpleName();
private MediaService.MusicController mMusicController;
//使用方法:mMusicController.play();播放 mMusicController.pause();暫停
private boolean running;
private TextSwitcher mSwitcher;
private SeekBar mSeekBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//設(shè)置透明欄
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
| WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
window.getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.TRANSPARENT);
}
//滑動(dòng)條部分
mSeekBar = (SeekBar) findViewById(R.id.music_seek_bar);
mSeekBar.setOnSeekBarChangeListener(this);
mSwitcher = (TextSwitcher) findViewById(R.id.text_switcher);
mSwitcher.setInAnimation(this, android.R.anim.fade_in);
mSwitcher.setOutAnimation(this, android.R.anim.fade_out);
Intent intent = new Intent(this, MediaService.class);
//增加StartService,來(lái)增加后臺(tái)播放功能
startService(intent);
// 綁定服務(wù),使用context來(lái)綁定
// 那個(gè)界面需要綁定 就用哪個(gè) Activity
// 參數(shù)1:Intent 代表需要綁定哪一個(gè)Service
// 參數(shù)2:ServiceConnection 回調(diào)接口,可以接收到Service連接成功和斷開(kāi)的回調(diào),成功就可以取到對(duì)象。
// 綁定服務(wù) 參數(shù)2就是服務(wù)和指定的對(duì)象綁定在一起
bindService(intent, this, BIND_AUTO_CREATE);
//指針和唱片部分
initViews();//定義背景圖
setAnimations();
}
private void initViews() {
playingPre = (ImageView) findViewById(R.id.playing_pre);
playingPlay = (ImageView) findViewById(R.id.playing_play);
playingNext = (ImageView) findViewById(R.id.playing_next);
disc = (ImageView) findViewById(R.id.disc);
needle = (ImageView) findViewById(R.id.needle);
playingPre.setOnClickListener(this);
playingPlay.setOnClickListener(this);
playingNext.setOnClickListener(this);
}
//動(dòng)畫(huà)設(shè)置
private void setAnimations() {
discAnimation = ObjectAnimator.ofFloat(disc, "rotation", 0, 360);
discAnimation.setDuration(20000);
discAnimation.setInterpolator(new LinearInterpolator());
discAnimation.setRepeatCount(ValueAnimator.INFINITE);
needleAnimation = ObjectAnimator.ofFloat(needle, "rotation", 0, 25);
needle.setPivotX(0);
needle.setPivotY(0);
needleAnimation.setDuration(800);
needleAnimation.setInterpolator(new LinearInterpolator());
}
@Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.playing_pre://前一曲
if (discAnimation != null) {
discAnimation.end();
playing();
}
break;
case R.id.playing_play://播放中
if (isPlaying){
playing();
}else {
if (needleAnimation != null) {
needleAnimation.reverse();
needleAnimation.end();
mMusicController.pause();
}
if (discAnimation != null && discAnimation.isRunning()) {
discAnimation.cancel();
mMusicController.pause();
float valueAvatar = (float) discAnimation.getAnimatedValue();
discAnimation.setFloatValues(valueAvatar, 360f + valueAvatar);
}
playingPlay.setImageResource(R.drawable.music_play);
isPlaying = true;
}
break;
case R.id.playing_next://下一曲
if (discAnimation != null) {
discAnimation.end();
playing();
}
break;
default:
break;
}
}
//播放:1、播放音樂(lè) 2、動(dòng)畫(huà)旋轉(zhuǎn) 3、暫停圖片切換為播放按鈕圖片
private void playing(){
needleAnimation.start();
discAnimation.start();
playingPlay.setImageResource(R.drawable.music_pause);
mMusicController.play();//播放
isPlaying = false;
}
//===================================播放歌曲服務(wù)開(kāi)啟、停止、結(jié)束===============================
@Override
protected void onStart() {
super.onStart();
Thread thread = new Thread(this);
thread.start();
}
@Override
protected void onStop() {
running = false;
super.onStop();
}
@Override
protected void onDestroy() {
// 解除綁定
unbindService(this);
super.onDestroy();
}
//---------------------播放到當(dāng)前音樂(lè)的滑動(dòng)條及時(shí)間設(shè)置--------------------------
@Override
public void run() {
running = true;
try {
while (running) {
if (mMusicController != null) {
long musicDuration = mMusicController.getMusicDuration();
final long position = mMusicController.getPosition();
final Date dateTotal = new Date(musicDuration);
final SimpleDateFormat sb = new SimpleDateFormat("mm:ss");
mSeekBar.setMax((int) musicDuration);
mSeekBar.setProgress((int) position);
mSwitcher.post(
new Runnable() {
@Override
public void run() {
Date date = new Date(position);
String time = sb.format(date) + "/" + sb.format(dateTotal);
mSwitcher.setCurrentText(time);
}
}
);
}
Thread.sleep(500);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//-----------------------------
//服務(wù)綁定與解除綁定的回調(diào)
/**
* 當(dāng)服務(wù)與當(dāng)前綁定對(duì)象,綁定成功,服務(wù)onBind方法調(diào)用并且返回之后
* 回調(diào)給這個(gè)方法
*
* @param name
* @param service IBinder 就是服務(wù) onBind 返回的對(duì)象
*/
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mMusicController = ((MediaService.MusicController) service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
mMusicController = null;
}
public void btnStopService(View view) {
Intent intent = new Intent(this, MediaService.class);
stopService(intent);
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
mMusicController.setPosition(seekBar.getProgress());
}
}
mediaserver部分的代碼:
package com.example.cungu.musicdemo;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.Environment;
import android.os.IBinder;
public class MediaService extends Service {
private MediaPlayer mPlayer;
/* 綁定服務(wù)的實(shí)現(xiàn)流程:
* 1.服務(wù) onCreate, onBind, onDestroy 方法
* 2.onBind 方法需要返回一個(gè) IBinder 對(duì)象
* 3.如果 Activity 綁定,Activity 就可以取到 IBinder 對(duì)象,可以直接調(diào)用對(duì)象的方法
*/
// 相同應(yīng)用內(nèi)部不同組件綁定,可以使用內(nèi)部類以及Binder對(duì)象來(lái)返回。
public class MusicController extends Binder {
public void play() {
mPlayer.start();//開(kāi)啟音樂(lè)
}
public void pause() {
mPlayer.pause();//暫停音樂(lè)
}
public long getMusicDuration() {
return mPlayer.getDuration();//獲取文件的總長(zhǎng)度
}
public long getPosition() {
return mPlayer.getCurrentPosition();//獲取當(dāng)前播放進(jìn)度
}
public void setPosition (int position) {
mPlayer.seekTo(position);//重新設(shè)定播放進(jìn)度
}
}
/**
* 當(dāng)綁定服務(wù)的時(shí)候,自動(dòng)回調(diào)這個(gè)方法
* 返回的對(duì)象可以直接操作Service內(nèi)部的內(nèi)容
* @param intent
* @return
*/
@Override
public IBinder onBind(Intent intent) {
return new MusicController();
}
@Override
public void onCreate() {
super.onCreate();
mPlayer = MediaPlayer.create(this, R.raw.yinyue1);
}
/**
* 任意一次unbindService()方法,都會(huì)觸發(fā)這個(gè)方法
* 用于釋放一些綁定時(shí)使用的資源
* @param intent
* @return
*/
@Override
public boolean onUnbind(Intent intent) {
return super.onUnbind(intent);
}
@Override
public void onDestroy() {
if (mPlayer.isPlaying()) {
mPlayer.stop();
}
mPlayer.release();
mPlayer = null;
super.onDestroy();
}
}
到此,這一個(gè)簡(jiǎn)易的音樂(lè)播放器,就完成了。
但是一個(gè)音樂(lè)播放器,至少播放的音樂(lè)不會(huì)只有一首,所以,可以考慮試做一個(gè)本地音樂(lè)播放器,讀取本地的音樂(lè)!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:擁抱kotlin之如何習(xí)慣使用kotlin高階函數(shù)
欄 目:Android
下一篇:Android自定義gridView仿頭條頻道拖動(dòng)管理功能
本文標(biāo)題:Android開(kāi)發(fā)簡(jiǎn)易音樂(lè)播放器
本文地址:http://www.jygsgssxh.com/a1/Android/8971.html
您可能感興趣的文章
- 01-10Android自定義View之繪制圓形頭像功能
- 01-10Android實(shí)現(xiàn)雙擊返回鍵退出應(yīng)用實(shí)現(xiàn)方法詳解
- 01-10android實(shí)現(xiàn)記住用戶名和密碼以及自動(dòng)登錄
- 01-10android實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能
- 01-10Android 友盟第三方登錄與分享的實(shí)現(xiàn)代碼
- 01-10android實(shí)現(xiàn)指紋識(shí)別功能
- 01-10Emoji表情在Android JNI中的兼容性問(wèn)題詳解
- 01-10Android實(shí)現(xiàn)圓形漸變加載進(jìn)度條
- 01-10android開(kāi)發(fā)環(huán)境中SDK文件夾下的所需內(nèi)容詳解
- 01-10android異步消息機(jī)制 源碼層面徹底解析(1)


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10Android自定義View之繪制圓形頭像功能
- 01-10Android實(shí)現(xiàn)雙擊返回鍵退出應(yīng)用實(shí)現(xiàn)方
- 01-10android實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能
- 01-10android實(shí)現(xiàn)記住用戶名和密碼以及自動(dòng)
- 01-10C++自定義API函數(shù)實(shí)現(xiàn)大數(shù)相乘算法
- 01-10Android 友盟第三方登錄與分享的實(shí)現(xiàn)代
- 01-10android實(shí)現(xiàn)指紋識(shí)別功能
- 01-10如何給Flutter界面切換實(shí)現(xiàn)點(diǎn)特效
- 01-10Android實(shí)現(xiàn)圓形漸變加載進(jìn)度條
- 01-10Emoji表情在Android JNI中的兼容性問(wèn)題詳
隨機(jī)閱讀
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 04-02jquery與jsp,用jquery
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 01-10delphi制作wav文件的方法
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什


