怎么在Android中實(shí)現(xiàn)一個(gè)音樂(lè)播放器-創(chuàng)新互聯(lián)

這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)怎么在Android中實(shí)現(xiàn)一個(gè)音樂(lè)播放器,文章內(nèi)容豐富且以專(zhuān)業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

10年積累的成都做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)、成都外貿(mào)網(wǎng)站建設(shè)經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶(hù)對(duì)網(wǎng)站的新想法和需求。提供各種問(wèn)題對(duì)應(yīng)的解決方案。讓選擇我們的客戶(hù)得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先網(wǎng)站設(shè)計(jì)后付款的網(wǎng)站建設(shè)流程,更有六枝免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。

實(shí)現(xiàn)方式:

第一步:使用Android Studio創(chuàng)建一個(gè)Android工程,并且修改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" 
 tools:context="com.fyt.musicplayer.MainActivity" 
 android:orientation="vertical"> 
 
 <!--顯示播放進(jìn)度--> 
 <SeekBar 
 android:id="@+id/sb" 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content" /> 
 
 <RelativeLayout 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content"> 
 
 <!--顯示當(dāng)前進(jìn)度--> 
 <TextView 
 android:id="@+id/tv_progress" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="00:00"/> 
 
 <!--顯示總進(jìn)度--> 
 <TextView 
 android:id="@+id/tv_total" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:layout_alignParentRight="true" 
 android:text="00:00"/> 
 
 </RelativeLayout> 
 
 <Button 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="播放音樂(lè)" 
 android:onClick="play"/> 
 
 <Button 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="暫停播放" 
 android:onClick="pausePlay"/> 
 
 <Button 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="繼續(xù)播放" 
 android:onClick="continuePlay"/> 
 
 <Button 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="退出" 
 android:onClick="exit"/> 
 
</LinearLayout>

第二步:新建一個(gè)MusicService.java文件,用于處理音樂(lè)播放的邏輯

package com.fyt.musicplayer; 
 
import android.app.Service; 
import android.content.Intent; 
import android.media.MediaPlayer; 
import android.os.Binder; 
import android.os.Bundle; 
import android.os.IBinder; 
import android.os.Message; 
import android.support.annotation.Nullable; 
 
import java.io.IOException; 
import java.util.Timer; 
import java.util.TimerTask; 
 
//創(chuàng)建一個(gè)繼承自服務(wù)的音樂(lè)服務(wù)類(lèi) 
public class MusicService extends Service { 
 
 private MediaPlayer player; 
 private Timer timer; 
 
 //綁定服務(wù)時(shí),調(diào)用此方法 
 @Nullable 
 @Override 
 public IBinder onBind(Intent intent) { 
 
 return new MusicControl(); 
 } 
 
 //創(chuàng)建播放音樂(lè)的服務(wù) 
 @Override 
 public void onCreate() { 
 super.onCreate(); 
 
 //創(chuàng)建音樂(lè)播放器對(duì)象 
 player = new MediaPlayer(); 
 } 
 
 //銷(xiāo)毀播放音樂(lè)服務(wù) 
 @Override 
 public void onDestroy() { 
 super.onDestroy(); 
 
 //停止播放音樂(lè) 
 player.stop(); 
 
 //釋放占用的資源 
 player.release(); 
 
 //將player置為空 
 player = null; 
 } 
 
 //播放音樂(lè) 
 public void play() { 
 
 try { 
 
 if(player == null) 
 { 
 player = new MediaPlayer(); 
 } 
 
 //重置 
 player.reset(); 
 
 //加載多媒體文件 
 player.setDataSource("sdcard/zxmzf.mp3"); 
 
 //準(zhǔn)備播放音樂(lè) 
 player.prepare(); 
 
 //播放音樂(lè) 
 player.start(); 
 
 //添加計(jì)時(shí)器 
 addTimer(); 
 
 } catch (IOException e) { 
 e.printStackTrace(); 
 } 
 } 
 
 //暫停播放音樂(lè) 
 public void pausePlay() { 
 
 player.pause(); 
 } 
 
 //繼續(xù)播放音樂(lè) 
 public void continuePlay() { 
 
 player.start(); 
 } 
 
 //創(chuàng)建一個(gè)實(shí)現(xiàn)音樂(lè)接口的音樂(lè)控制類(lèi) 
 class MusicControl extends Binder implements MusicInterface { 
 
 @Override 
 public void play() { 
 
 MusicService.this.play(); 
 } 
 
 @Override 
 public void pausePlay() { 
 
 MusicService.this.pausePlay(); 
 } 
 
 @Override 
 public void continuePlay() { 
 
 MusicService.this.continuePlay(); 
 } 
 
 @Override 
 public void seekTo(int progress) { 
 
 MusicService.this.seekTo(progress); 
 } 
 } 
 
 //設(shè)置音樂(lè)的播放位置 
 public void seekTo(int progress) { 
 
 player.seekTo(progress); 
 } 
 
 //添加計(jì)時(shí)器用于設(shè)置音樂(lè)播放器中的播放進(jìn)度 
 public void addTimer() { 
 
 //如果沒(méi)有創(chuàng)建計(jì)時(shí)器對(duì)象 
 if(timer == null) { 
 
 //創(chuàng)建計(jì)時(shí)器對(duì)象 
 timer = new Timer(); 
 
 timer.schedule(new TimerTask() { 
 
 //執(zhí)行計(jì)時(shí)任務(wù) 
 @Override 
 public void run() { 
 
 //獲得歌曲總時(shí)長(zhǎng) 
 int duration = player.getDuration(); 
 
 //獲得歌曲的當(dāng)前播放進(jìn)度 
 int currentPosition = player.getCurrentPosition(); 
 
 //創(chuàng)建消息對(duì)象 
 Message msg = MainActivity.handler.obtainMessage(); 
 
 //將音樂(lè)的播放進(jìn)度封裝至消息對(duì)象中 
 Bundle bundle = new Bundle(); 
 bundle.putInt("duration", duration); 
 bundle.putInt("currentPosition", currentPosition); 
 msg.setData(bundle); 
 
 //將消息發(fā)送到主線(xiàn)程的消息隊(duì)列 
 MainActivity.handler.sendMessage(msg); 
 } 
 }, 
 
 //開(kāi)始計(jì)時(shí)任務(wù)后的5毫秒,第一次執(zhí)行run方法,以后每500毫秒執(zhí)行一次 
 5, 500); 
 } 
 } 
}

第三步:創(chuàng)建一個(gè)MusicInterface.java文件創(chuàng)建用于操作音樂(lè)播放的接口

package com.fyt.musicplayer; 
 
//創(chuàng)建一個(gè)音樂(lè)播放接口 
public interface MusicInterface { 
 
 //播放音樂(lè) 
 void play(); 
 
 //暫停播放音樂(lè) 
 void pausePlay(); 
 
 //繼續(xù)播放音樂(lè) 
 void continuePlay(); 
 
 //修改音樂(lè)的播放位置 
 void seekTo(int progress); 
}

第四步:修改MainActivity.java文件

package com.fyt.musicplayer; 
 
import android.app.Activity; 
import android.content.ComponentName; 
import android.content.Intent; 
import android.content.ServiceConnection; 
import android.os.Bundle; 
import android.os.Handler; 
import android.os.IBinder; 
import android.os.Message; 
import android.view.View; 
import android.widget.SeekBar; 
import android.widget.TextView; 
 
public class MainActivity extends Activity { 
 
 MyServiceConn conn; 
 Intent intent; 
 MusicInterface mi; 
 
 //用于設(shè)置音樂(lè)播放器的播放進(jìn)度 
 private static SeekBar sb; 
 
 private static TextView tv_progress; 
 private static TextView tv_total; 
 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
 super.onCreate(savedInstanceState); 
 setContentView(R.layout.activity_main); 
 
 tv_progress = (TextView) findViewById(R.id.tv_progress); 
 tv_total = (TextView) findViewById(R.id.tv_total); 
 
 //創(chuàng)建意圖對(duì)象 
 intent = new Intent(this, MusicService.class); 
 
 //啟動(dòng)服務(wù) 
 startService(intent); 
 
 //創(chuàng)建服務(wù)連接對(duì)象 
 conn = new MyServiceConn(); 
 
 //綁定服務(wù) 
 bindService(intent, conn, BIND_AUTO_CREATE); 
 
 //獲得布局文件上的滑動(dòng)條 
 sb = (SeekBar) findViewById(R.id.sb); 
 
 //為滑動(dòng)條添加事件監(jiān)聽(tīng) 
 sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { 
 
 //當(dāng)滑動(dòng)條中的進(jìn)度改變后,此方法被調(diào)用 
 @Override 
 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { 
 
 } 
 
 //滑動(dòng)條剛開(kāi)始滑動(dòng),此方法被調(diào)用 
 @Override 
 public void onStartTrackingTouch(SeekBar seekBar) { 
 
 } 
 
 //當(dāng)滑動(dòng)條停止滑動(dòng),此方法被調(diào)用 
 @Override 
 public void onStopTrackingTouch(SeekBar seekBar) { 
 
 //根據(jù)拖動(dòng)的進(jìn)度改變音樂(lè)播放進(jìn)度 
 int progress = seekBar.getProgress(); 
 
 //改變播放進(jìn)度 
 mi.seekTo(progress); 
 } 
 }); 
 } 
 
 //創(chuàng)建消息處理器對(duì)象 
 public static Handler handler = new Handler(){ 
 
 //在主線(xiàn)程中處理從子線(xiàn)程發(fā)送過(guò)來(lái)的消息 
 @Override 
 public void handleMessage(Message msg) { 
 
 //獲取從子線(xiàn)程發(fā)送過(guò)來(lái)的音樂(lè)播放的進(jìn)度 
 Bundle bundle = msg.getData(); 
 
 //歌曲的總時(shí)長(zhǎng)(毫秒) 
 int duration = bundle.getInt("duration"); 
 
 //歌曲的當(dāng)前進(jìn)度(毫秒) 
 int currentPostition = bundle.getInt("currentPosition"); 
 
 //刷新滑塊的進(jìn)度 
 sb.setMax(duration); 
 sb.setProgress(currentPostition); 
 
 //歌曲的總時(shí)長(zhǎng) 
 int minute = duration / 1000 / 60; 
 int second = duration / 1000 % 60; 
 
 String strMinute = null; 
 String strSecond = null; 
 
 //如果歌曲的時(shí)間中的分鐘小于10 
 if(minute < 10) { 
 
 //在分鐘的前面加一個(gè)0 
 strMinute = "0" + minute; 
 } else { 
 
 strMinute = minute + ""; 
 } 
 
 //如果歌曲的時(shí)間中的秒鐘小于10 
 if(second < 10) 
 { 
 //在秒鐘前面加一個(gè)0 
 strSecond = "0" + second; 
 } else { 
 
 strSecond = second + ""; 
 } 
 
 tv_total.setText(strMinute + ":" + strSecond); 
 
 //歌曲當(dāng)前播放時(shí)長(zhǎng) 
 minute = currentPostition / 1000 / 60; 
 second = currentPostition / 1000 % 60; 
 
 //如果歌曲的時(shí)間中的分鐘小于10 
 if(minute < 10) { 
 
 //在分鐘的前面加一個(gè)0 
 strMinute = "0" + minute; 
 } else { 
 
 strMinute = minute + ""; 
 } 
 
 //如果歌曲的時(shí)間中的秒鐘小于10 
 if(second < 10) { 
 
 //在秒鐘前面加一個(gè)0 
 strSecond = "0" + second; 
 } else { 
 
 strSecond = second + ""; 
 } 
 
 tv_progress.setText(strMinute + ":" + strSecond); 
 } 
 }; 
 
 //播放音樂(lè)按鈕響應(yīng)函數(shù) 
 public void play(View view) { 
 
 //播放音樂(lè) 
 mi.play(); 
 } 
 
 //暫停播放音樂(lè)按鈕響應(yīng)函數(shù) 
 public void pausePlay(View view) { 
 
 //暫停播放音樂(lè) 
 mi.pausePlay(); 
 } 
 
 //繼續(xù)播放音樂(lè)按鈕響應(yīng)函數(shù) 
 public void continuePlay (View view) { 
 
 //繼續(xù)播放音樂(lè) 
 mi.continuePlay(); 
 } 
 
 //退出音樂(lè)播放按鈕響應(yīng)函數(shù) 
 public void exit(View view) { 
 
 //解綁服務(wù) 
 unbindService(conn); 
 
 //停止服務(wù) 
 stopService(intent); 
 
 //結(jié)束這個(gè)activity 
 finish(); 
 } 
 
 //實(shí)現(xiàn)服務(wù)器連接接口 
 class MyServiceConn implements ServiceConnection { 
 
 @Override 
 public void onServiceConnected(ComponentName name, IBinder service) { 
 
 //獲得中間人對(duì)象 
 mi = (MusicInterface) service; 
 } 
 
 @Override 
 public void onServiceDisconnected(ComponentName name) { 
 
 } 
 } 
}

第五步:在配置文件中的Application節(jié)點(diǎn)下添加服務(wù)組件

<service android:name="com.fyt.playmusic.MusicService"> 
 </service>

最后一步:添加讀取SD卡的權(quán)限

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

上述就是小編為大家分享的怎么在Android中實(shí)現(xiàn)一個(gè)音樂(lè)播放器了,如果剛好有類(lèi)似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

本文標(biāo)題:怎么在Android中實(shí)現(xiàn)一個(gè)音樂(lè)播放器-創(chuàng)新互聯(lián)
本文鏈接:http://bm7419.com/article36/dgddsg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、網(wǎng)站導(dǎo)航、Google手機(jī)網(wǎng)站建設(shè)、建站公司App開(kāi)發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作