您好,登錄后才能下訂單哦!
這篇文章給大家介紹Android開(kāi)發(fā)中怎么實(shí)現(xiàn)一個(gè)MP3播放器,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
在android中播放mp3非常簡(jiǎn)單,也是項(xiàng)目中經(jīng)常使用的,比如說(shuō)要做項(xiàng)目的背景音樂(lè),應(yīng)用中某些功能的提示音等的。應(yīng)用非常廣泛,下面提供一個(gè)簡(jiǎn)單的使用實(shí)例:
layout文件的配置:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:orientation="vertical" android:layout_height="wrap_content"> <Button android:text="播放" android:id="@+id/btnStart" android:layout_width="fill_parent" android:layout_height="wrap_content"> </Button> <Button android:text="停止" android:id="@+id/btnStop" android:layout_width="fill_parent" android:layout_height="wrap_content"> </Button> </LinearLayout>
java實(shí)現(xiàn)文件:
/** * */ package com.demo.media; import android.app.Activity; import android.media.MediaPlayer; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import com.demo.HelloWorld.R; /** * @author xsl * vaiyanzi@gmail.com * 實(shí)現(xiàn)MP3播放功能 */ public class mediademo extends Activity { private Button btnStart,btnStop; private static MediaPlayer mediaPlayer=null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.media); btnStart = (Button) this.findViewById(R.id.btnStart); btnStart.setOnClickListener(new ButtonListener()); btnStop = (Button) this.findViewById(R.id.btnStop); btnStop.setOnClickListener(new ButtonListener()); } class ButtonListener implements OnClickListener{ public void onClick(View v) { switch(v.getId()){ case R.id.btnStart: if(mediaPlayer==null){ //創(chuàng)建播放實(shí)例 mediaPlayer=MediaPlayer.create(mediademo.this, R.raw.tishiyin); } try { //設(shè)置是否循環(huán)播放 mediaPlayer.setLooping(true); //設(shè)置播放起始點(diǎn) mediaPlayer.seekTo(0); //開(kāi)始播放 mediaPlayer.start(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } break; case R.id.btnStop: if(mediaPlayer!=null){ //停止播放 mediaPlayer.stop(); //釋放資源 mediaPlayer.release(); mediaPlayer=null; } break; } } } }
關(guān)于A(yíng)ndroid開(kāi)發(fā)中怎么實(shí)現(xiàn)一個(gè)MP3播放器就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guā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)容。