您好,登錄后才能下訂單哦!
本篇文章為大家展示了利用Android開發(fā)一個(gè)音樂(lè)播放器,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。
讀取本地音樂(lè)文件
源代碼:
import android.media.MediaPlayer; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.ImageButton; import android.widget.SeekBar; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private MediaPlayer mediaPlayer; private SeekBar seekBar; private TextView tv_mian_stop; private TextView tv_mian_start; private int musicaction; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv_mian_start = (TextView) findViewById(R.id.tv_mian_start); tv_mian_stop = (TextView) findViewById(R.id.tv_mian_stop); seekBar = (SeekBar) findViewById(R.id.seekBar); //進(jìn)度條的監(jiān)聽事件 seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { //改變 //每當(dāng)進(jìn)度條發(fā)生改變時(shí)tv_mian_start的值發(fā)生變化 //new TimeUtil().secToTime()一個(gè)外部類(時(shí)間格式轉(zhuǎn)化) tv_mian_start.setText(new TimeUtil().secToTime(mediaPlayer.getCurrentPosition()/1000)+""); } @Override public void onStartTrackingTouch(SeekBar seekBar) { //開始 } @Override public void onStopTrackingTouch(SeekBar seekBar) { //結(jié)束 //獲取拖動(dòng)結(jié)束之后的位置 int progrss=seekBar.getProgress(); mediaPlayer.seekTo(progrss); } }); } public void isPlay(View view){ ImageButton imageButton= (ImageButton) view; if (mediaPlayer==null){ mediaPlayer = MediaPlayer.create(this, R.raw.seve); mediaPlayer.start(); //把圖標(biāo)修改為暫停的圖標(biāo) imageButton.setImageResource(android.R.drawable.ic_media_pause); //獲取音樂(lè)的總時(shí)長(zhǎng) int muiscsum=mediaPlayer.getDuration(); //設(shè)置進(jìn)度條的最大值為總時(shí)長(zhǎng) seekBar.setMax(muiscsum); //執(zhí)行一個(gè)多線程的耗時(shí)操作 new MyThread().start(); tv_mian_stop.setText(new TimeUtil().secToTime(muiscsum/1000)+""); }else if (mediaPlayer.isPlaying()){ mediaPlayer.pause(); imageButton.setImageResource(android.R.drawable.ic_media_play); }else{ mediaPlayer.start(); imageButton.setImageResource(android.R.drawable.ic_media_pause); } } class MyThread extends Thread{ @Override public void run() { super.run(); //當(dāng)進(jìn)度條的當(dāng)前值小于最大值時(shí)進(jìn)度條發(fā)生改變 while(seekBar.getProgress()<=seekBar.getMax()){ //獲取當(dāng)前音樂(lè)播放的位置 musicaction = mediaPlayer.getCurrentPosition(); //讓進(jìn)度條動(dòng)起來(lái) seekBar.setProgress(musicaction); } } } }
時(shí)間格式轉(zhuǎn)化類:
public class TimeUtil { public static String secToTime(int time) { String timeStr = null; int hour = 0; int minute = 0; int second = 0; if (time <= 0) return "00:00"; else { minute = time / 60; if (minute < 60) { second = time % 60; timeStr = unitFormat(minute) + ":" + unitFormat(second); } else { hour = minute / 60; if (hour > 99) return "99:59:59"; minute = minute % 60; second = time - hour * 3600 - minute * 60; timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second); } } return timeStr; } private static String unitFormat(int i) { String retStr = null; if (i >= 0 && i < 10) retStr = "0" + Integer.toString(i); else retStr = "" + i; return retStr; } }
真機(jī)效果圖:
上述內(nèi)容就是利用Android開發(fā)一個(gè)音樂(lè)播放器,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。