安卓開發(fā),實(shí)現(xiàn)簡單音樂播放器-創(chuàng)新互聯(lián)

Android平臺(tái)多媒體框架核心使用的是OpenCORE多媒體框架,在安卓系統(tǒng)中所有涉及音頻視頻的錄制。解碼。播放都是通過它來實(shí)現(xiàn)的。Android系統(tǒng)音頻視頻以及流媒體類型數(shù)據(jù)的播放有MediaPlayer類來完成。

站在用戶的角度思考問題,與客戶深入溝通,找到泉州網(wǎng)站設(shè)計(jì)與泉州網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:網(wǎng)站制作、網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、域名注冊(cè)虛擬主機(jī)、企業(yè)郵箱。業(yè)務(wù)覆蓋泉州地區(qū)。

下面進(jìn)行一個(gè)實(shí)例來演示MediaPlayer的使用:

具體實(shí)現(xiàn)效果如下:

安卓開發(fā),實(shí)現(xiàn)簡單音樂播放器

其中選項(xiàng)1,2,3分別是三種不同的音頻加載方式:

方式1是內(nèi)部加載,音頻文件存放在/res/raw文件夾中,

方式2是本地加載,音頻文件存放在本地SD卡中,

方式三為網(wǎng)絡(luò)加載,音頻文件從網(wǎng)絡(luò)中獲取。

xml文件代碼如下:

<RelativeLayout 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=".MainActivity" >

  <TextView

    android:id="@+id/text1"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="請(qǐng)選擇:" />

  <RadioGroup

    android:id="@+id/radiogroup"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:layout_below="@+id/text1"

    android:layout_marginTop="30dp">

    <RadioButton

      android:id="@+id/radio1"

      android:layout_width="fill_parent"

      android:layout_height="wrap_content"

      android:text="選項(xiàng)1"/>

    <RadioButton

      android:id="@+id/radio2"

      android:layout_width="fill_parent"

      android:layout_height="wrap_content"

      android:text="選項(xiàng)2"/>

    <RadioButton

      android:id="@+id/radio3"

      android:layout_width="fill_parent"

      android:layout_height="wrap_content"

      android:text="選項(xiàng)3"/>

  </RadioGroup>

  <TextView

    android:id="@+id/text2"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:layout_alignParentLeft="true"

    android:layout_below="@+id/radiogroup"

    android:layout_marginTop="52dp"

    android:text="你的選擇是:" />

  <SeekBar

    android:id="@+id/seekbar"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:layout_alignParentLeft="true"

    android:layout_below="@+id/text2"

    android:layout_marginTop="16dp" />

  <Button

    android:id="@+id/stop"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:layout_alignParentBottom="true"

    android:layout_alignParentRight="true"

    android:layout_marginBottom="84dp"

    android:text="停止" />

  <Button

    android:id="@+id/pause"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:layout_alignBaseline="@+id/stop"

    android:layout_alignBottom="@+id/stop"

    android:layout_centerHorizontal="true"

    android:text="暫停" />

  <Button

    android:id="@+id/play"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:layout_alignBaseline="@+id/pause"

    android:layout_alignBottom="@+id/pause"

    android:layout_alignParentLeft="true"

    android:text="開始" />

</RelativeLayout>

MainActivity代碼如下:

public class MainActivity extends Activity implements OnSeekBarChangeListener{

private static final String music_name="music.mp3";

private static final String music_path="/res/raw/";

private static final String music_sdpath="/sdcard/huawei/";

private static final String music_network_url="http://sc1.111ttt.com/2015/5/11/05/104050035435.mp3";

private String music_play_path="";

private SeekBar seekbar=null;

private RadioGroup radiogroup;

private boolean progressflag=false;

private MediaPlayer mediaplayer;

private Timer timer;

private TimerTask timertask;

private Button button1,button2,button3;

private TextView text_path;

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

music_component();

mediaplayer=new MediaPlayer();

button_handler();

seekbar.setOnSeekBarChangeListener(this);

//注冊(cè)進(jìn)度改變事件監(jiān)聽器

}

public void onProgressChanged(SeekBar seekbar, int arg1, boolean arg2) {

// TODO Auto-generated method stub

}

public void onStartTrackingTouch(SeekBar arg0) {

// TODO Auto-generated method stub

progressflag=true;

}

public void onStopTrackingTouch(SeekBar arg0) {

// TODO Auto-generated method stub

mediaplayer.seekTo(seekbar.getProgress());

progressflag=false;

}

protected void onDestroy(){

if(mediaplayer!=null){

mediaplayer.release();

timer.cancel();

timertask.cancel();

}

super.onDestroy();

}

private void music_component(){

radiogroup=(RadioGroup)findViewById(R.id.radiogroup);

seekbar=(SeekBar)findViewById(R.id.seekbar);

button1=(Button)findViewById(R.id.play);

button2=(Button)findViewById(R.id.pause);

button3=(Button)findViewById(R.id.stop);

text_path=(TextView)findViewById(R.id.text2);

}

private void button_handler(){

radiogroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

public void onCheckedChanged(RadioGroup arg0, int arg1) {

// TODO Auto-generated method stub

if(mediaplayer!=null){

mediaplayer.reset();

}

}

});

button1.setOnClickListener(new OnClickListener() {

public void onClick(View arg0) {

// TODO Auto-generated method stub

playmusic();

}

});

button2.setOnClickListener(new OnClickListener() {

public void onClick(View arg0) {

// TODO Auto-generated method stub

pausemusic();

}

});

button3.setOnClickListener(new OnClickListener() {

public void onClick(View arg0) {

// TODO Auto-generated method stub

stopmusic();

}

});

}

protected void stopmusic() {

// TODO Auto-generated method stub

if(mediaplayer!=null&&mediaplayer.isPlaying()){

mediaplayer.reset();

Toast.makeText(MainActivity.this, "播放結(jié)束.", Toast.LENGTH_SHORT).show();

timer.cancel();

timertask.cancel();

}

}

protected void pausemusic() {

// TODO Auto-generated method stub

if(mediaplayer!=null&&mediaplayer.isPlaying()){

Toast.makeText(MainActivity.this, "播放暫停.", Toast.LENGTH_SHORT).show();

mediaplayer.pause();

}else{

mediaplayer.start();

Toast.makeText(MainActivity.this, "繼續(xù)播放.", Toast.LENGTH_SHORT).show();

}

}

private void playmusic() {

// TODO Auto-generated method stub

mediaplayer.reset();

switch (radiogroup.getCheckedRadioButtonId()) {

case R.id.radio1:

music_play_path="音樂來自于:"+music_path+music_name;

text_path.setText(music_play_path);

mediaplayer=mediaplayer.create(MainActivity.this, R.raw.music);

doPlayMusic(music_path+music_name,true);

break;

    case R.id.radio2:

     music_play_path="音樂來自于:"+music_sdpath+music_name;

     text_path.setText(music_play_path);

     doPlayMusic(music_sdpath+music_name,false);

break;

    case R.id.radio3:

     music_play_path="音樂來自于:"+"網(wǎng)絡(luò):"+music_network_url;

     text_path.setText(music_play_path);

     doPlayMusic(music_network_url,false);

break;

default:

break;

}

}

private void doPlayMusic(String musicpath,boolean is_res_way) {

// mp3路徑和是否為內(nèi)部資源加載方式,如果不是,就用setDataSource()方法

try {

if(!is_res_way){

mediaplayer.setDataSource(musicpath);

  mediaplayer.prepare();

}

seekbar.setMax(mediaplayer.getDuration());

//設(shè)置進(jìn)度條大值

timer=new Timer();

timertask=new TimerTask() {

@Override

public void run() {

// TODO Auto-generated method stub

if(progressflag=true)

seekbar.setProgress(mediaplayer.getCurrentPosition());

//設(shè)置進(jìn)度條為當(dāng)前播放進(jìn)度

}

};

timer.schedule(timertask,0,10);

//用計(jì)時(shí)器記錄播放進(jìn)度

mediaplayer.start();

mediaplayer.setOnCompletionListener(new OnCompletionListener() {

public void onCompletion(MediaPlayer arg0) {

// TODO Auto-generated method stub

Toast.makeText(MainActivity.this, "播放完成.", Toast.LENGTH_SHORT).show();

timer.cancel();

timertask.cancel();

seekbar.setProgress(0);

mediaplayer.reset();

}

});

//注冊(cè)播放完成事件監(jiān)聽器

} catch (IllegalStateException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

以上代碼為實(shí)例源碼,可以直接用,音樂文件的名字是"music.mp3"。

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

當(dāng)前名稱:安卓開發(fā),實(shí)現(xiàn)簡單音樂播放器-創(chuàng)新互聯(lián)
文章分享:http://muchs.cn/article34/dphise.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站定制網(wǎng)站、網(wǎng)站設(shè)計(jì)、網(wǎng)站改版、外貿(mào)建站、用戶體驗(yàn)

廣告

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

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司