Android学习笔记_25_多媒体之在线播放器
一、布局文件:
<?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:background="#FFFFFF"
android:orientation="vertical" > <TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/filename" /> <EditText
android:id="@+id/filename"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="11.RMVB" /> <LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" > <ImageButton
android:id="@+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/play" /> <ImageButton
android:id="@+id/pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/pause" /> <ImageButton
android:id="@+id/reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/reset" /> <ImageButton
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/stop" />
</LinearLayout> <SurfaceView
android:id="@+id/surfaceView"
android:layout_width="fill_parent"
android:layout_height="240dip" /> </LinearLayout>
二、代码实现:
package com.example.video; import java.io.File;
import java.io.FileInputStream;
import java.io.IOException; import android.R.integer;
import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast; public class MainActivity extends Activity {
private static final String TAG = "VideoActivity";
private EditText filenameText;
private SurfaceView surfaceView;
private MediaPlayer mediaPlayer;
private String filename;// 当前播放文件的名称
private int position;// 记录播放位置
private boolean pause=false;//是否被暂停
private File file; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); this.mediaPlayer = new MediaPlayer();
this.filenameText = (EditText) this.findViewById(R.id.filename);
this.surfaceView = (SurfaceView) this.findViewById(R.id.surfaceView);
ImageButton playButton = (ImageButton) this.findViewById(R.id.play);
ImageButton pauseButton = (ImageButton) this.findViewById(R.id.pause);
ImageButton resetButton = (ImageButton) this.findViewById(R.id.reset);
ImageButton stopButton = (ImageButton) this.findViewById(R.id.stop); ButtonClickListener listener = new ButtonClickListener();
playButton.setOnClickListener(listener);
pauseButton.setOnClickListener(listener);
resetButton.setOnClickListener(listener);
stopButton.setOnClickListener(listener); /* 下面设置Surface不维护自己的缓冲区,而是等待屏幕的渲染引擎将内容推送到用户面前 */
this.surfaceView.getHolder().setType(
SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
this.surfaceView.getHolder().setFixedSize(176, 144);// 设置分辨率
//让屏幕高亮,不要锁频
this.surfaceView.getHolder().setKeepScreenOn(true);
this.surfaceView.getHolder().addCallback(new SurfaceListener());
} private class ButtonClickListener implements View.OnClickListener {
@Override
public void onClick(View v) {
try {
switch (v.getId()) {
case R.id.play:// 来自播放按钮
filename = filenameText.getText().toString();
file=new File("/storage/sdcard0/Music",filename);
if (file.exists()) {
play(0);
}else {
Toast.makeText(getApplicationContext(), "文件不存在", Toast.LENGTH_LONG).show();
}
break; case R.id.pause:// 来自暂停按钮
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
pause=true;
} else {
if (pause) {
mediaPlayer.start();
pause=false;
}
}
break; case R.id.reset:// 来自重新播放按钮
if (mediaPlayer.isPlaying()) {
mediaPlayer.seekTo(0);
}else {
if(file!=null){
play(0);
}
}
break;
case R.id.stop:// 来自停止按钮
if (mediaPlayer.isPlaying())
mediaPlayer.stop();
break;
}
} catch (Exception e) {
Log.e(TAG, e.toString());
}
}
} /**
* 播放视频
*/
private void play(int position) throws IOException {
mediaPlayer.reset();
//mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(file.getAbsolutePath());// 设置需要播放的视频
mediaPlayer.setDisplay(surfaceView.getHolder());// 把视频画面输出到SurfaceView
mediaPlayer.prepare();//进行缓冲处理,完成之后播放
mediaPlayer.setOnPreparedListener(new PrepareListener(position)); }
private final class PrepareListener implements OnPreparedListener{
private int position;
public PrepareListener(int position) {
this.position=position;
} //当缓冲完成之后就会调用onPrepared方法
@Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();//播放
if (position>0) {
mediaPlayer.seekTo(position);
}
}
} private class SurfaceListener implements SurfaceHolder.Callback {
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
} //当画面再次回到前台时调用
@Override
public void surfaceCreated(SurfaceHolder holder) {// 方法在onResume()后被调用
Log.i(TAG, "surfaceCreated()");
if (position > 0 && filename != null) {
try {
play(position);
position = 0;
} catch (Exception e) {
Log.e(TAG, e.toString());
}
}
} @Override
public void surfaceDestroyed(SurfaceHolder holder) {
if (mediaPlayer.isPlaying()) {
position = mediaPlayer.getCurrentPosition();// 得到播放位置
mediaPlayer.stop();
}
Log.i(TAG, "surfaceDestroyed()");
}
} @Override
protected void onDestroy() {
if (mediaPlayer.isPlaying())
mediaPlayer.stop();
mediaPlayer.release();
super.onDestroy();
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
} }
Android学习笔记_25_多媒体之在线播放器的更多相关文章
- Android学习笔记_24_多媒体MediaPlayer对象之音乐播放器与SoundPool声音池
一.MediaPlayer对象常用方法介绍: MediaPlayer mediaPlayer = new MediaPlayer(); if (mediaPlayer.isPlaying()) { m ...
- Android学习笔记进阶十一图片动画播放(AnimationDrawable)
大家平时见到的最多的可能就是Frame动画了,Android中当然也少不了它.它的使用更加简单,只需要创建一个 AnimationDrawabledF对象来表示Frame动画,然后通过addFrame ...
- Android学习笔记_27_多媒体之视频刻录
一.配置文件: <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android= ...
- Android学习笔记_26_多媒体之拍照
一.配置文件: 需要引入摄像头权限,sdcard读写权限. <?xml version="1.0" encoding="utf-8"?> <m ...
- JS学习笔记(6)--音乐播放器
说明(2017.3.15): 1. lrc.js里面存储LRC歌词的格式的数组,获取里面的时间轴,转为秒数. 2. 通过audio.currentTime属性,setinterval每秒获取歌曲播放的 ...
- udacity android 学习笔记: lesson 4 part b
udacity android 学习笔记: lesson 4 part b 作者:干货店打杂的 /titer1 /Archimedes 出处:https://code.csdn.net/titer1 ...
- 【转】 Pro Android学习笔记(七七):服务(2):Local Service
目录(?)[-] Local service代码 调用Local ServiceLocal Service client代码 AndroidManifestxml定义Serviceacitivty的l ...
- 【转】 Pro Android学习笔记(二九):用户界面和控制(17):include和merge
目录(?)[-] xml控件代码重用include xml控件代码重用merge 横屏和竖屏landsacpe portrait xml控件代码重用:include 如果我们定义一个控件,需要在不同的 ...
- Android 学习笔记之Volley(七)实现Json数据加载和解析...
学习内容: 1.使用Volley实现异步加载Json数据... Volley的第二大请求就是通过发送请求异步实现Json数据信息的加载,加载Json数据有两种方式,一种是通过获取Json对象,然后 ...
随机推荐
- 封装Lua for C#
http://blog.csdn.net/rcfalcon/article/details/5583095
- elasticSearch请求流程图
- 为数据赋能:腾讯TDSQL分布式金融级数据库前沿技术
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 简介:李海翔,网名"那海蓝蓝",腾讯金融云数据库技术专家.中国人民大学信息学院工程硕士企业导师.著有<数据库事务处 ...
- Linux下Makefile的automake生成全攻略--转
http://www.yesky.com/120/1865620.shtml 作为Linux下的程序开发人员,大家一定都遇到过Makefile,用make命令来编译自己写的程序确实是很方便.一般情况下 ...
- HDU 5596 ——GTW likes gt——————【想法题】
GTW likes gt Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)To ...
- 6、图标:icon
1.图标 /* ---html----*/ <ion-content text-center class="icons-basic-page"> <ion-row ...
- 搭建Vue2.0开发环境
1.必须要安装nodejs 2.搭建vue的开发环境 ,安装vue的脚手架工具 官方命令行工具 npm install --global vue-cli / cnpm install --global ...
- hdu 3265 矩形剪块面积并
http://acm.hust.edu.cn/vjudge/problem/10769 给n张海报,在每张海报上剪掉一个矩形,求面积并 把剪块的海报分成四个矩形,就是普通的求面积并问题了 #inclu ...
- java常用API之System类
System中代表程序所在系统,提供了对应的一些系统属性信息,和系统操作.System类不能手动创建对象,因为构造方法被private修饰,阻止外界创建对象.System类中的都是static方法,类 ...
- 数据访问层 (DAO)
数据持久化 持久化:将程序中的数据在瞬间状态下和持久状态间转换的机制(JDBC) 主要持久化操作:保存.删除.读取.和查找. 采用面向接口编程,可以降低代码间的耦合性,提高代码的可扩展性和可维护性. ...