Android多媒体(Media)入门

 

lesson28_media

Android 提供了 MediaPlayer 和 MediaRecorder 两个工具类,来帮助开发者操作音频和视频。我们通过两个小例子来学习一下多媒体资源的使用。

一、 简单音乐播放器

1、新建一个项目Lesson28_Music , 主Activity的名字是 MainMusic.java

2、拷贝 play_50 play_disable pause_50 pause_disable stop_50 stop_disable 这几张图片到res/drawable目录下,并建立3个xml文件,拷贝love.mp3到res/raw文件中。

play.xml

1<?xml version="1.0" encoding="utf-8"?>
2<SELECTOR xmlns:android="http://schemas.android.com/apk/res/android">
3    <ITEM android:drawable="@drawable/play_disable" android:state_enabled="false" /> <!-- state_enabled=false -->
4    <ITEM android:drawable="@drawable/play_50" /> <!-- default -->
5</SELECTOR>

pause.xml

1<?xml version="1.0" encoding="utf-8"?>
2<SELECTOR xmlns:android="http://schemas.android.com/apk/res/android">
3    <ITEM android:drawable="@drawable/pause_disable" android:state_enabled="false" /> <!-- state_enabled=false -->
4    <ITEM android:drawable="@drawable/pause_50" /> <!-- default -->
5</SELECTOR>

stop.xml

1<?xml version="1.0" encoding="utf-8"?>
2<SELECTOR xmlns:android="http://schemas.android.com/apk/res/android">
3    <ITEM android:drawable="@drawable/stop_disable" android:state_enabled="false" /> <!-- state_enabled=false -->
4    <ITEM android:drawable="@drawable/stop_50" /> <!-- default -->
5</SELECTOR>

3、res/layout/main.xml 的内容如下:

01<?xml version="1.0" encoding="utf-8"?>
02<LINEARLAYOUT xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical">
03    <TEXTVIEW android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="简单音乐播放器" android:textsize="25sp" />
04</LINEARLAYOUT>
05<LINEARLAYOUT xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="horizontal">
06  
07        <IMAGEBUTTON android:layout_height="wrap_content" android:layout_width="wrap_content" android:background="@drawable/play" android:id="@+id/play" android:layout_margin="4dp" android:adjustviewbounds="true">
08        </IMAGEBUTTON>
09  
10        <IMAGEBUTTON android:layout_height="wrap_content" android:layout_width="wrap_content" android:background="@drawable/pause" android:id="@+id/pause" android:layout_margin="4dp" android:adjustviewbounds="true">
11        </IMAGEBUTTON>
12  
13        <IMAGEBUTTON android:layout_height="wrap_content" android:layout_width="wrap_content" android:background="@drawable/stop" android:id="@+id/stop" android:layout_margin="4dp" android:adjustviewbounds="true">
14        </IMAGEBUTTON>
15    </LINEARLAYOUT>

4、MainMusic.java的内容如下:

001package android.basic.lesson28;
003import java.io.IOException;
005import android.app.Activity;
006import android.media.MediaPlayer;
007import android.media.MediaPlayer.OnCompletionListener;
008import android.media.MediaPlayer.OnPreparedListener;
009import android.os.Bundle;
010import android.view.View;
011import android.view.View.OnClickListener;
012import android.widget.ImageButton;
013import android.widget.Toast;
015public class MainMusic extends Activity {
017    // 声明变量
018    private ImageButton play, pause, stop;
019    private MediaPlayer mPlayer;
021    /** Called when the activity is first created. */
022    @Override
023    public void onCreate(Bundle savedInstanceState) {
024        super.onCreate(savedInstanceState);
025        setContentView(R.layout.main);
027        // 定义UI组件
028        play = (ImageButton) findViewById(R.id.play);
029        pause = (ImageButton) findViewById(R.id.pause);
030        stop = (ImageButton) findViewById(R.id.stop);
032        // 按钮先全部失效
033        play.setEnabled(false);
034        pause.setEnabled(false);
035        stop.setEnabled(false);
037        // 定义单击监听器
040            @Override
041            public void onClick(View v) {
042                switch (v.getId()) {
043                case R.id.play:
044                    // 播放
045                    Toast.makeText(MainMusic.this, "点击播放", Toast.LENGTH_SHORT)
046                            .show();
047                    play();
048                    break;
049                case R.id.pause:
050                    // 暂停
051                    Toast.makeText(MainMusic.this, "暂停播放", Toast.LENGTH_SHORT)
052                            .show();
053                    pause();
054                    break;
055                case R.id.stop:
056                    // 停止
057                    Toast.makeText(MainMusic.this, "停止播放", Toast.LENGTH_SHORT)
058                            .show();
059                    stop();
060                    break;
061                }
062            }
063        };
065        // 绑定单击监听
066        play.setOnClickListener(ocl);
067        pause.setOnClickListener(ocl);
068        stop.setOnClickListener(ocl);
070        // 初始化
071        initMediaPlayer();
072    }
073  
074    // 初始化播放器
075    private void initMediaPlayer() {
076  
077        // 定义播放器
078        mPlayer = MediaPlayer.create(getApplicationContext(), R.raw.love);
079  
080        // 定义资源准备好的监听器
081        mPlayer.setOnPreparedListener(new OnPreparedListener() {
082            @Override
083            public void onPrepared(MediaPlayer mp) {
084                // 资源准备好了再让播放器按钮有效
085                Toast.makeText(MainMusic.this, "onPrepared", Toast.LENGTH_SHORT)
086                        .show();
087                play.setEnabled(true);
088            }
089        });
091        // 定义播放完成监听器
092        mPlayer.setOnCompletionListener(new OnCompletionListener() {
093  
094            @Override
095            public void onCompletion(MediaPlayer mp) {
096                Toast.makeText(MainMusic.this, "onCompletion",
097                        Toast.LENGTH_SHORT).show();
098                stop();
099            }
100        });
101    }
103    // 停止播放
104    private void stop() {
105        mPlayer.stop();
106        pause.setEnabled(false);
107        stop.setEnabled(false);
108        try {
109            mPlayer.prepare();
110            mPlayer.seekTo(0);
111            play.setEnabled(true);
112        } catch (IllegalStateException e) {
113            e.printStackTrace();
114        } catch (IOException e) {
115            e.printStackTrace();
116        }
118    }
120    // 播放
121    private void play() {
123        mPlayer.start();
124        play.setEnabled(false);
125        pause.setEnabled(true);
126        stop.setEnabled(true);
127    }
129    // 暂停
130    private void pause() {
131        mPlayer.pause();
132        play.setEnabled(true);
133        pause.setEnabled(false);
134        stop.setEnabled(true);
135    }
137    // Activity销毁前停止播放
138    @Override
139    protected void onDestroy() {
140        super.onDestroy();
141        if (stop.isEnabled()) {
142            stop();
143        }
145    }
147}

5、运行程序,查看效果

image

image

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值