【移動應(yīng)用開發(fā)技術(shù)】Android多媒體應(yīng)用如何使用MediaPlayer播放音頻_第1頁
【移動應(yīng)用開發(fā)技術(shù)】Android多媒體應(yīng)用如何使用MediaPlayer播放音頻_第2頁
【移動應(yīng)用開發(fā)技術(shù)】Android多媒體應(yīng)用如何使用MediaPlayer播放音頻_第3頁
【移動應(yīng)用開發(fā)技術(shù)】Android多媒體應(yīng)用如何使用MediaPlayer播放音頻_第4頁
【移動應(yīng)用開發(fā)技術(shù)】Android多媒體應(yīng)用如何使用MediaPlayer播放音頻_第5頁
已閱讀5頁,還剩4頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

【移動應(yīng)用開發(fā)技術(shù)】Android多媒體應(yīng)用如何使用MediaPlayer播放音頻

1.創(chuàng)建MediaPlayer對象,并裝載音頻文件MediaPlayer

player=new

MediaPlayer();

try

{

player.setDataSource("/sdcard/suger.mp3");//指定要裝載的音頻文件

}

catch

(IllegalArgumentException

e)

{

e.printStackTrace();

}

catch

(SecurityException

e)

{

e.printStackTrace();

}

catch

(IllegalStateException

e)

{

e.printStackTrace();

}

catch

(IOException

e)

{

e.printStackTrace();

}

try

{

player.prepare();//預(yù)加載音頻

}

catch

(IllegalStateException

e)

{

e.printStackTrace();

}

catch

(IOException

e)

{

e.printStackTrace();

}

}<?xml

version="1.0"

encoding="utf-8"?>

<LinearLayout

xmlns:android="/apk/res/android"

xmlns:tools="/tools"

android:id="@+id/ll1"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical"

>

<LinearLayout

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:orientation="vertical"

>

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="歡迎使用音樂播放器"

android:id="@+id/hint"/>

</LinearLayout>

<LinearLayout

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:orientation="horizontal"

>

<Button

android:id="@+id/play"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="播放"/>

<Button

android:id="@+id/pause"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="暫停"/>

<Button

android:id="@+id/stop"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="停止"/>

</LinearLayout>

</LinearLayout>package

com.example.test;

import

java.io.File;

import

android.app.Activity;

import

android.media.MediaPlayer;

import

android.media.MediaPlayer.OnCompletionListener;

import

.Uri;

import

android.os.Bundle;

import

android.os.Environment;

import

android.view.View;

import

android.view.View.OnClickListener;

import

android.widget.Button;

import

android.widget.TextView;

public

class

MainActivity

extends

Activity{

private

MediaPlayer

player;//MediaPlayer對象

private

boolean

isPause=false;//是否暫停

private

File

file;//要播放的音頻文件

private

TextView

hint;//聲明顯示提示信息的文本框

private

Button

play;//播放按鈕

private

Button

pause;//暫停/繼續(xù)按鈕

private

Button

stop;//停止按鈕

@Override

public

void

onCreate(Bundle

savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

play=(Button)findViewById(R.id.play);//獲取"播放"按鈕

play.setOnClickListener(new

OnClickListener()

{

@Override

public

void

onClick(View

arg0)

{

play();//開始播放音樂

if(isPause){

pause.setText("暫停");

isPause=false;//設(shè)置暫停標記為false

}

pause.setEnabled(true);//"暫停/繼續(xù)"按鈕可用

stop.setEnabled(true);//"停止"按鈕可用

play.setEnabled(false);//"播放"按鈕不可用

}

});

pause=(Button)findViewById(R.id.pause);//獲取"暫停"按鈕

pause.setOnClickListener(new

OnClickListener()

{

@Override

public

void

onClick(View

v)

{

if(player.isPlaying()&&!isPause){

player.pause();//暫停播放

isPause=true;

((Button)v).setText("繼續(xù)");

hint.setText("暫停播放音頻...");

play.setEnabled(true);//播放按鈕可用

}else{

player.start();//繼續(xù)播放

isPause=false;

((Button)v).setText("暫停");

hint.setText("繼續(xù)播放音頻...");

play.setEnabled(false);//播放按鈕不可用

}

}

});

stop=(Button)findViewById(R.id.stop);//獲取"停止"按鈕

stop.setOnClickListener(new

OnClickListener()

{

@Override

public

void

onClick(View

arg0)

{

player.stop();

hint.setText("停止播放音頻...");

pause.setEnabled(false);//"暫停/繼續(xù)"按鈕不可用

stop.setEnabled(false);//"停止"按鈕不可用

play.setEnabled(true);//"播放"按鈕可用

}

});

hint=(TextView)findViewById(R.id.hint);//獲取顯示提示信息的文本框

file=new

File(getSDPath()+"/"+"Music/Whistle.mp3");

if(file.exists()){

player=MediaPlayer.create(MainActivity.this,

Uri.parse(file.getAbsolutePath()));//創(chuàng)建MediaPlayer對象

}else{

hint.setText("要播放的音頻不存在!");

play.setEnabled(false);

return;

}

//添加完成事件監(jiān)聽器,用于當音樂播放完畢后,重新開始播放因音樂

player.setOnCompletionListener(new

OnCompletionListener()

{

@Override

public

void

onCompletion(MediaPlayer

arg0)

{

play();//重新開始播放

}

});

}

//播放音樂的方法

public

void

play(){

try

{

player.reset();

player.setDataSource(file.getAbsolutePath());//重新設(shè)置要播放的音頻

player.prepare();//預(yù)加載音頻

player.start();//開始播放

hint.setText("正在播放音樂...");

}

catch

(Exception

e)

{

e.printStackTrace();

}

}

//獲取sdcard根目錄的方法

public

String

getSDPath(){

File

sdDir

=

null;

boolean

sdCardExist

=

Environment.getExternalStorageState()

.equals(android.os.Environment.MEDIA_MOUNTED);

//判斷sd卡是否存在

if(sdCardExist)

//如果SD卡存在,則獲取跟目錄

{

sdDir

=

Environment.getExternalStorageDirectory();//獲取根目錄

}

return

sdDir.toString();

}

//Activity銷銷毀時,停止正在播放的音頻,并釋放MediaPlayer所占用的資源

@Override

protected

void

onDestroy()

{

if(player.isPlayin

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論