1. 程式人生 > >android播放語音

android播放語音

第一步:Activity 可以播放、暫停、停止 簡單的一小例子 也是拿別人的改編 為了記載以後方便使用

package com.jingna.stu.mytest;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

/**
 * Created by Administrator on 2018/11/12.
 */

public class Test extends Activity
{       //聲名變數
    private Button start=null;
    private Button pause=null;
    private Button stop=null;
    private TextView state=null;
    private MediaPlayer mp3;
    private Boolean flag=false; //設定標記,false表示正在播放
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.test);
//取得各按鈕元件
        start=(Button) super.findViewById(R.id.start);
        pause=(Button) super.findViewById(R.id.pause);
        stop=(Button) super.findViewById(R.id.stop);
        state=(TextView)super.findViewById(R.id.state);
//為每個按鈕設定單擊事件
        start.setOnClickListener(new OnClickListenerStart());
        pause.setOnClickListener(new OnClickListenerPause());
        stop.setOnClickListener(new OnClickListenerStop());
        mp3= new MediaPlayer();    //建立一個MediaPlayer物件
//在res下新建一個raw資料夾把一首歌放到此資料夾中並用英文命名
        mp3 = MediaPlayer.create(this,R.raw.aas);
    }
    //各按鈕單擊事件的實現如下
//開始播放
    private class OnClickListenerStart implements View.OnClickListener {
        //implementsOnClickListener為實現OnClickListener介面
        @Override
//重寫onClic事件
        public void onClick(View v)
        {
//執行的程式碼,其中可能有異常。一旦發現異常,則立即跳到catch執行。否則不會執行catch裡面的內容
            try
            {
                if (mp3!=null)
                {
                    mp3.stop();
                }
                mp3.prepare();         //進入到準備狀態
                mp3.start();          //開始播放
                state.setText("Playing");  //改變輸出資訊為“Playing”,下同
            } catch (Exception e)
            {
                state.setText(e.toString());//以字串的形式輸出異常
                e.printStackTrace();  //在控制檯(control)上打印出異常
            }
        }
    }
    //暫停播放
    private class OnClickListenerPause implements View.OnClickListener {
        @Override
        public void onClick(View v)
        {
            try
            {
                if (flag==false) //若flag為false,則表示此時播放器的狀態為正在播放
                {
                    mp3.pause();
                    flag=true;
                    state.setText("pause");
                }
                else if(flag==true){
                    mp3.start();    //開始播放
                    flag=false;     //重新設定flag為false
                    state.setText("Playing");
                }
            } catch (Exception e)
            {
                state.setText(e.toString());
                e.printStackTrace();
            }
        }
    }
    //停止播放
    private class OnClickListenerStop implements View.OnClickListener {
        @Override
        public void onClick(View v)
        {
            try
            {
                if (mp3!=null)
                {
                    mp3.stop();
                    state.setText("stop");
                }
            } catch (Exception e)
            {
                state.setText(e.toString());
                e.printStackTrace();
            }
        }
    }
    //重寫暫停狀態事件
    protected void onPause(){
        try
        {
            mp3.release();   //釋放音樂資源
        } catch (Exception e)
        {
            state.setText(e.toString());
            e.printStackTrace();
        }
        super.onPause();
    }
}

第二步:xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <Button
        android:id="@+id/state"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="請點選" />
    <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
        android:orientation="horizontal">

        <Button
            android:id="@+id/start"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="start"/>
        <Button
            android:id="@+id/pause"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="pause"/>
        <Button
            android:id="@+id/stop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="stop"/>
    </LinearLayout>
</LinearLayout>