打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
第二十八讲:Android多媒体(Media)入门 ? { Android学习指南 }

第二十八讲:Android多媒体(Media)入门

18Aug

本讲内容:Android中的音频和视频使用入门指南

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

一、 简单音乐播放器

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

2、拷贝

这几张图片到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:state_enabled="false" android:drawable="@drawable/play_disable"> <!-- state_enabled=false -->
4     <item android:drawable="@drawable/play_50"> <!-- default -->
5 </item></item></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:state_enabled="false" android:drawable="@drawable/pause_disable"> <!-- state_enabled=false -->
4     <item android:drawable="@drawable/pause_50"> <!-- default -->
5 </item></item></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:state_enabled="false" android:drawable="@drawable/stop_disable"> <!-- state_enabled=false -->
4     <item android:drawable="@drawable/stop_50"> <!-- default -->
5 </item></item></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 </textview>
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:background="@drawable/play" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/play" android:adjustviewbounds="true" android:layout_margin="4dp">
08         </imagebutton>
09  
10         <imagebutton android:background="@drawable/pause" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/pause" android:adjustviewbounds="true" android:layout_margin="4dp">
11         </imagebutton>
12  
13         <imagebutton android:background="@drawable/stop" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/stop" android:adjustviewbounds="true" android:layout_margin="4dp">
14         </imagebutton>
15     </linearlayout>
16 </linearlayout>

 

4、MainMusic.java的内容如下:

001 package android.basic.lesson28;
002  
003 import java.io.IOException;
004  
005 import android.app.Activity;
006 import android.media.MediaPlayer;
007 import android.media.MediaPlayer.OnCompletionListener;
008 import android.media.MediaPlayer.OnPreparedListener;
009 import android.os.Bundle;
010 import android.view.View;
011 import android.view.View.OnClickListener;
012 import android.widget.ImageButton;
013 import android.widget.Toast;
014  
015 public class MainMusic extends Activity {
016  
017     // 声明变量
018     private ImageButton play, pause, stop;
019     private MediaPlayer mPlayer;
020  
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);
026  
027         // 定义UI组件
028         play = (ImageButton) findViewById(R.id.play);
029         pause = (ImageButton) findViewById(R.id.pause);
030         stop = (ImageButton) findViewById(R.id.stop);
031  
032         // 按钮先全部失效
033         play.setEnabled(false);
034         pause.setEnabled(false);
035         stop.setEnabled(false);
036  
037         // 定义单击监听器
038         OnClickListener ocl = new View.OnClickListener() {
039  
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         };
064  
065         // 绑定单击监听
066         play.setOnClickListener(ocl);
067         pause.setOnClickListener(ocl);
068         stop.setOnClickListener(ocl);
069  
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         });
090  
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     }
102  
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         }
117  
118     }
119  
120     // 播放
121     private void play() {
122  
123         mPlayer.start();
124         play.setEnabled(false);
125         pause.setEnabled(true);
126         stop.setEnabled(true);
127     }
128  
129     // 暂停
130     private void pause() {
131         mPlayer.pause();
132         play.setEnabled(true);
133         pause.setEnabled(false);
134         stop.setEnabled(true);
135     }
136  
137     // Activity销毁前停止播放
138     @Override
139     protected void onDestroy() {
140         super.onDestroy();
141         if (stop.isEnabled()) {
142             stop();
143         }
144  
145     }
146  
147 }

 

5、运行程序,查看效果

二、简单视频播放器

Android为视频播放提供了VideoView 和 MediaController 两个现成的组件,让我们可以方便的实现MP4、3GP等视频的播放。下面我们通过一个例子来看一下:

1、新建一个项目 Lesson28_Video

2、使用 Format Factory 这个软件压缩一个视频备用,我这里压缩的参数如下:

注意,如果播放时完全无法播放或者只有声音没有图像,你就需要换压缩软件和调整压缩参数重新压缩视频了,暂时只能这样,我也是折腾了2-3小时都是黑屏,郁闷中(似乎得出一个答案,是否黑屏和机器设备的性能有关,我降低压缩分辨率和每秒帧数,出图像音画同步,如果提高每秒帧数,声音出来后十几秒图像才会出来,但是出来后音画还是同步的,有兴趣的朋友可以多测试测试给出一个结论)。

用命令行的方式拷贝此视频到存储卡(sdcard)中,为什么不用eclipse中的可视化工具拷贝呢?因为那个方式靠大文件的时候经常失败,而命令行方式我没拷贝失败一次过。命令就是 adb push ,具体截个图给你看:

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

1 <?xml version="1.0" encoding="utf-8"?>
2 <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" android:layout_gravity="top">
3 <videoview android:layout_height="fill_parent" android:layout_width="fill_parent" android:id="@+id/VideoView01">
4 </videoview>
5 </linearlayout>

 

4、MainVideo.java的内容如下:

01 package android.basic.lesson28;
02  
03 import android.app.Activity;
04 import android.net.Uri;
05 import android.os.Bundle;
06 import android.view.Window;
07 import android.view.WindowManager;
08 import android.widget.MediaController;
09 import android.widget.VideoView;
10  
11 public class MainVideo extends Activity {
12     /** Called when the activity is first created. */
13     @Override
14     public void onCreate(Bundle savedInstanceState) {
15         super.onCreate(savedInstanceState);
16         //全屏
17         this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
18         //标题去掉
19         this.requestWindowFeature(Window.FEATURE_NO_TITLE);
20         //要在全屏等设置完毕后再加载布局
21         setContentView(R.layout.main);
22  
23         //定义UI组件
24         VideoView videoView = (VideoView) findViewById(R.id.VideoView01);
25         //定义MediaController对象
26         MediaController mediaController = new MediaController(this);
27         //把MediaController对象绑定到VideoView上
28         mediaController.setAnchorView(videoView);
29         //设置VideoView的控制器是mediaController
30         videoView.setMediaController(mediaController);
31  
32         //这两种方法都可以 videoView.setVideoPath("file:///sdcard/love_480320.mp4");
33         videoView.setVideoURI(Uri.parse("/sdcard/love_480320.mp4"));
34         //启动后就播放
35         videoView.start();
36     }
37 }

 

5、运行效果如下:

好了本讲内容就到这里,下次再见。

 
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
android如何创建一个透明的ImageButton
Android Layout XML属性研究
android 中ImageButton按下改变背景图片的效果
第二十四讲:Android动画入门(一) ? { Android学习指南 }
向android 的状态栏中加入快捷按钮(home,back,menu等等)的方法(续) ...
android 根据button不同状态显示不同(背景)图片
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服