Android学习笔记_25_多媒体之在线播放器
来源:互联网 发布:幸运大转盘抽奖软件 编辑:程序博客网 时间:2024/04/24 04:11
一、布局文件:
<?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; }}
0 0
- Android学习笔记_25_多媒体之在线播放器
- Android学习笔记_26_多媒体之拍照
- android多媒体学习笔记一
- android多媒体学习笔记二
- android 学习笔记11-多媒体
- Android多媒体--学习笔记1
- Android学习笔记_27_多媒体之视频刻录
- IOS利用AVPlayer开发多媒体在线播放器
- IOS利用AVPlayer开发多媒体在线播放器
- Android学习笔记_24_多媒体MediaPlayer对象之音乐播放器与SoundPool声音池
- Android学习之运用多媒体
- android 多媒体部分学习笔记十二----mediaplay自定义播放器
- 多媒体播放器学习笔记
- Android 多媒体开发学习之简单的音乐播放器
- android多媒体部分学习笔记五----audio
- android学习笔记——多媒体
- Android学习笔记第三篇--多媒体
- Android Day 14 多媒体学习笔记
- 单例模式优、缺点
- Android学习笔记_22_服务Service应用之—与Activity进行相互通信的本地服务
- Android学习笔记_23_服务Service之AIDL和远程服务实现进程通信以及进程间传递自定义类型参数
- 多线程下载
- Android学习笔记_24_多媒体MediaPlayer对象之音乐播放器与SoundPool声音池
- Android学习笔记_25_多媒体之在线播放器
- Android学习笔记_26_多媒体之拍照
- 使用 Cobbler 自动化和管理系统安装
- Android学习笔记_27_多媒体之视频刻录
- Android学习笔记_28_手势识别
- 程序员:把爱写进程序
- Spring.NET框架简介及模块说明
- Android学习笔记_29_样式和主题
- Android学习笔记_30_常用控件使用