Android学习1: Android初学视频播放
2015-01-15 09:26
267 查看
本菜今天又学习了一下使用MediaPlay实现视频播放的功能.
代码也不复杂..
使用SurfaceView, 来实现视频播放的窗口, 使用MediaPlay来实现视频的播入控制.
进度条是使用Timer来实时从MediaPlay中获取视频的播放进度来进行刷新.
截图:
代码如下:
com/example/youtwo/testvideo/MainActivity.java
layout/activity_main.xml
代码也不复杂..
使用SurfaceView, 来实现视频播放的窗口, 使用MediaPlay来实现视频的播入控制.
进度条是使用Timer来实时从MediaPlay中获取视频的播放进度来进行刷新.
截图:
代码如下:
com/example/youtwo/testvideo/MainActivity.java
package com.example.youtwo.testvideo; import android.media.MediaPlayer; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.widget.Button; import android.widget.SeekBar; import java.io.IOException; import java.util.Timer; import java.util.TimerTask; public class MainActivity extends ActionBarActivity { private SurfaceView surfaceView = null; private SurfaceHolder surfaceHolder = null; private MediaPlayer m = null; private Button btn_start_video = null; private Button btn_stop_video = null; private boolean isChanging = false; private Timer mTimer; private TimerTask mTimerTask; private SeekBar skb_video = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); m = new MediaPlayer(); surfaceView = (SurfaceView) findViewById(R.id.surfaceView); surfaceHolder = surfaceView.getHolder(); btn_start_video = (Button) this.findViewById(R.id.Button03); btn_start_video.setOnClickListener(new ClickEvent()); btn_stop_video = (Button) this.findViewById(R.id.Button04); btn_stop_video.setOnClickListener(new ClickEvent()); skb_video = (SeekBar) this.findViewById(R.id.SeekBar); skb_video.setOnSeekBarChangeListener(new SeekBarChangeEvent()); mTimer = new Timer(); mTimerTask = new TimerTask() { @Override public void run() { if (isChanging == true) return; skb_video.setProgress(m.getCurrentPosition()); } }; mTimer.schedule(mTimerTask, 0, 10); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } class ClickEvent implements View.OnClickListener { @Override public void onClick(View v) { if (v == btn_start_video) { m.reset(); m.setDisplay(surfaceHolder);//设置屏幕 try { m.setDataSource("/sdcard/test.mp4"); m.prepare(); m.start(); skb_video.setMax(m.getDuration());//设置SeekBar的长度 } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else if(v == btn_stop_video) { m.stop(); } } } /* * SeekBar进度改变事件 */ class SeekBarChangeEvent implements SeekBar.OnSeekBarChangeListener { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { } @Override public void onStartTrackingTouch(SeekBar seekBar) { isChanging = true; } @Override public void onStopTrackingTouch(SeekBar seekBar) { isChanging = false; } } }
layout/activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <LinearLayout android:id="@+id/LinearLayout01" android:layout_width="fill_parent" android:layout_height="wrap_content" xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" > <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="播放视频" android:id="@+id/Button03" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="停止播放" android:id="@+id/Button04" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <SeekBar android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/SeekBar" android:indeterminate="false" android:layout_weight="1" android:layout_below="@+id/LinearLayout01" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" /> </LinearLayout> </LinearLayout> <SurfaceView android:layout_width="wrap_content" android:layout_height="fill_parent" android:id="@+id/surfaceView" android:layout_below="@+id/LinearLayout01" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" /> </RelativeLayout>
相关文章推荐
- Android学习记录--音频视频播放
- android 多媒体部分学习笔记十--简单视频播放
- [API GUIDE 学习笔记]android MediaPlayer 播放视频
- Android学习二、MediaPlayer播放视频
- android学习之视频播放
- Android多媒体学习十四:播放网络上的视频
- 在Android中播放音频和视频--值得学习
- 学习日记--Android VideoView播放视频控制:开始、暂停、快进
- android 学习笔记 播放音频 和视频
- Android学习之路------自定义屏保程序判断当前系统有视频正在播放
- android 多媒体部分学习笔记十--简单视频播放
- Android应用开发学习笔记之播放视频
- android 多媒体部分学习十四---网络视频播放(http/rtsp)
- Android 学习笔记之SurfaceView的使用+如何实现视频播放...
- Android学习04视频的播放
- 初学Android,多媒体之使用VideoView播放视频(七十七)
- Android多媒体学习十四:播放网络上的视频
- Android应用开发学习笔记之播放视频
- Android多媒体学习:播放网络上的视频
- Android OpenGL 播放视频学习