一直以来都没有对自己的工作做一个很好的总结,从今天起对工作进行总结,希望给大家一点帮助,同时也是加深自己的理解.
在安卓端做视频开发是很费劲了,但是如果使用的VideoView这个标签,就会非常方便的做到视频文件的播放,做到快进,快退,暂停,播放时间的展示,进度条的显示.VideoView这个标签已经将MediaPlay这个类中的个总状态进行的封装,开发人员不需要对个总状态进行处理,只需要简单的调用就可以的,废话有点多,看代码
xml文件的代码:
下面的代码就是一个简单的VideoView,和一个音量的改变大小
<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">
<VideoView
android:id="@+id/video_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="99dp"
android:layout_marginRight="46dp"
android:onClick="ZD"
android:text="音量减小" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button2"
android:layout_alignBottom="@+id/button2"
android:layout_marginRight="18dp"
android:layout_toLeftOf="@+id/button2"
android:onClick="JX"
android:text="音量增大" />
</RelativeLayout>
在看看java代码
package com.example.videodemo1;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.app.PendingIntent.OnFinished;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;
public class MainActivity extends Activity{
private static final String TAG = null;
private MediaController mMediaController;
private VideoView video_view;
private Uri mUri;
private int currentPosition;
private int currentVolume;
private AudioManager mAudioManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//设置屏幕景观
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
video_view = (VideoView) findViewById(R.id.video_view);
//创建媒体控制器
mMediaController = new MediaController(this);
video_view.setMediaController(mMediaController);
//URL地址 这个的三中SD卡的存储时一样的
System.out.println("SD卡的存储路径1:"+Environment.getExternalStorageDirectory());
System.out.println("SD卡的存储路径(getAbsolutePath):"+Environment.getExternalStorageDirectory().getAbsolutePath());
System.out.println("SD卡的存储路径(getPath):"+Environment.getExternalStorageDirectory().getPath());
mUri = Uri.parse(Environment.getExternalStorageDirectory()+"/Music/xiaojingteng.mp4");
//音量的管理者获取
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
//获取系统音量
currentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_SYSTEM);
System.out.println("我是系统音量:"+currentVolume);
// mAudioManager.setStreamVolume(streamType, index, flags)
}
//增大音量
public void ZD(View view){
System.out.println("增大音量");
mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE,
AudioManager.FX_FOCUS_NAVIGATION_UP);
}
//减小音量
public void JX(View view){
System.out.println("减小音量");
mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_LOWER,
AudioManager.FX_FOCUS_NAVIGATION_UP);
}
@Override
protected void onStart() {
// 播放视频
video_view.setVideoURI(mUri);
video_view.start();
super.onStart();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
//活动暂停
currentPosition = video_view.getCurrentPosition();
video_view.stopPlayback();
Log.i(TAG, "我是currentPosition:"+currentPosition);
Log.i(TAG, "我是getDuration:"+video_view.getDuration());
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
//重新播放
if(currentPosition >= 0){
video_view.seekTo(currentPosition);
currentPosition = -1;
}
}
public boolean onError(MediaPlayer player,int arg1,int arg2){
return false;
}
}
到这里就可以实现视频播放的.