欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android編程實(shí)現(xiàn)播放MP3功能示例

 更新時(shí)間:2017年02月22日 11:44:22   作者:藍(lán)之風(fēng)  
這篇文章主要介紹了Android編程實(shí)現(xiàn)播放MP3功能,結(jié)合實(shí)例形式分析了Android播放MP3功能的界面布局與功能實(shí)現(xiàn)相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了Android編程實(shí)現(xiàn)播放MP3功能。分享給大家供大家參考,具體如下:

在android中播放mp3非常簡單,也是項(xiàng)目中經(jīng)常使用的,比如說要做項(xiàng)目的背景音樂,應(yīng)用中某些功能的提示音等的。應(yīng)用非常廣泛,下面提供一個(gè)簡單的使用實(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);
          //開始播放
          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)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android Service組件使用技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android資源操作技巧匯總》、《Android文件操作技巧匯總》、《Android開發(fā)入門與進(jìn)階教程》、《Android視圖View技巧總結(jié)》及《Android控件用法總結(jié)

希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論