Android应用软件开发

194课时
1.7K学过
8分

课程评价 (0)

请对课程作出评价:
0/300

学员评价

暂无精选评价
3分钟

6.3 实施步骤

实施步骤

步骤1:新建一个Module,命名为Ex6_3,其他设置默认。

步骤2:编写activity_main.xml文件。清单如下:

Ex6_3 activity_main.xml清单

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

    <TextView
        android:id="@+id/hint"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="单击播放开始播放音乐" />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="播放" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="暂停" />

        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="停止" />
    </LinearLayout>

</LinearLayout>

步骤3:编写MainActivity.java文件。清单如下:

Ex6_3 MainActivity.java清单

package com.example.a123.music;

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

public class MainActivity extends Activity {
    
private MediaPlayer mp;//mediaPlayer对象
    private Button play, pause, stop;//播放 暂停/继续 停止 按钮
    private TextView hint;//显示当前播放状态
    private boolean isPause = false;//是否暂停

private void play() {
        try {
            mp.reset();
            mp = MediaPlayer.create(MainActivity.this, R.raw.sound);//重新设置要播放的音频
            mp.start();//开始播放
            hint.setText("正在播放音频...");
            play.setEnabled(false);
            pause.setEnabled(true);
            stop.setEnabled(true);
        } catch (Exception e) {
            e.printStackTrace();//输出异常信息
        }}

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        play = (Button) findViewById(R.id.button1);
        pause = (Button) findViewById(R.id.button2);
        stop = (Button) findViewById(R.id.button3);
        hint = (TextView) findViewById(R.id.hint);
        hint.setTextSize(20);
        mp = MediaPlayer.create(MainActivity.this, R.raw.sound);//创建mediaplayer对象

        mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer arg0) {
                // TODO Auto-generated method stub
                play();//重新开始播放
            }
        });
        
play.setOnClickListener(new View.OnClickListener() {
           @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                play();
                if (isPause) {
                    pause.setText("暂停");
                    isPause = false;
                }
            }
        });
        
pause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (mp.isPlaying() && !isPause) {
                    mp.pause();
                    isPause = true;
                    pause.setText("继续");
                    hint.setText("暂停播放音频...");
                    play.setEnabled(true);
                } else {
                    mp.start();
                    pause.setText("暂停");
                    hint.setText("继续播放音频...");
                    isPause = false;
                    play.setEnabled(false);
                }
            }
        });
        
stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                mp.stop();
                hint.setText("停止播放音频...");
                pause.setEnabled(false);
                stop.setEnabled(false);
                play.setEnabled(true);
            }
        });

    }
    
protected void onDestroy() {
        // TODO Auto-generated method stub
        if (mp.isPlaying()) {
            mp.stop();
        }
        mp.release();//释放资源
        super.onDestroy();
    }
    
}