概述
最进想通过MediaPlayer实现播放一个视频,并且SeekBar可拖动的功能,记录下来,以便查阅!
实现效果如下:
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
android:id="@+id/surfaceview"
android:layout_width="fill_parent"
android:layout_height="600dp" />
android:id="@+id/seekBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="100"/>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="播放" />
android:id="@+id/pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="暂停" />
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="停止" />
package com.Javen;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class VideoPlayTestActivity extends Activity implements OnClickListener{
/** Called when the activity is first created. */
private SurfaceView surfaceView = null;
private Button startButton, pauseButton, stopButton;
private SeekBar seekBar = null;
private Player player = null;
private static final String TAG = "VideoPlayTestActivity";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
findView();
player = new Player(surfaceView, seekBar);
}
private void findView()
{
surfaceView = (SurfaceView)findViewById(R.id.surfaceview);
seekBar = (SeekBar)findViewById(R.id.seekBar);
startButton = (Button)findViewById(R.id.start);
pauseButton = (Button)findViewById(R.id.pause);
stopButton = (Button)findViewById(R.id.stop);
startButton.setOnClickListener(this);
pauseButton.setOnClickListener(this);
stopButton.setOnClickListener(this);
seekBar.setOnSeekBarChangeListener(SeekBarLinstener);
}
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
int id = v.getId();
switch (id)
{
case R.id.start:
player.playPath("/mnt/usb/sda1/test.rmvb");
break;
case R.id.pause:
player.pause();
break;
case R.id.stop:
player.stop();
break;
default:
break;
}
}
OnSeekBarChangeListener SeekBarLinstener = new OnSeekBarChangeListener()
{
int progress;
@Override
public void onStopTrackingTouch(SeekBar seekBar)
{
// seekTo()的参数是相对与影片时间的数字,而不是与seekBar.getMax()相对的数字
player.mediaPlayer.seekTo(progress);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar)
{
}
@Override
public void onProgressChanged(SeekBar seekBar, int arg0,
boolean fromUser)
{
/**
* 这里的arg0是百分比,即拖动位置暂全部视频的百分位置
* this.progress则是相当影片的总体时间的长度
*/
this.progress = arg0 * player.mediaPlayer.getDuration()/seekBar.getMax();
Log.d(TAG,"arg0 ="+arg0+" this.progress="+this.progress);
}
};
}
package com.Javen;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Handler;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.SeekBar;
public class Player implements SurfaceHolder.Callback, OnBufferingUpdateListener, OnCompletionListener, MediaPlayer.OnPreparedListener
{
public MediaPlayer mediaPlayer;
public SurfaceHolder surfaceHolder;
private SeekBar playerSeekBar;
private int videoWidth, videoHeight;
private Timer timer;
public Player(SurfaceView surfaceView, SeekBar seekBar)
{
super();
this.playerSeekBar = seekBar;
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
timer = new Timer();
timer.schedule(timerTask, 0, 1000);//使用TimerTask实现更新seekBar的进度条
}
TimerTask timerTask = new TimerTask()
{
public void run()
{
if(mediaPlayer == null)
return;
if(mediaPlayer.isPlaying() && playerSeekBar.isPressed()== false)
{
handlerProgress.sendEmptyMessage(0);
}
};
};
Handler handlerProgress = new Handler()
{
public void handleMessage(android.os.Message msg)
{
switch (msg.what)
{
case 0:
int position = mediaPlayer.getCurrentPosition();//获取当前播放的影片文件位置
int duration = mediaPlayer.getDuration();//获取影片总文件大小位置
if(duration > 0)
{
long pos = playerSeekBar.getMax() * position/duration;
playerSeekBar.setProgress((int)pos);
}
break;
default:
break;
}
};
};
public void pause()
{
if(mediaPlayer != null)
{
mediaPlayer.pause();
}
}
public void start()
{
if(mediaPlayer != null)
{
mediaPlayer.start();
}
}
public void stop()
{
if(mediaPlayer != null)
{
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
}
}
public void playPath(String path)
{
try
{
if(mediaPlayer != null)
{
mediaPlayer.reset();
mediaPlayer.setDataSource(path);//设置播放影片路径
mediaPlayer.prepare();//做播放影片前的准备
}
} catch (IllegalArgumentException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException 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();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height)
{
}
@Override
public void surfaceCreated(SurfaceHolder holder)
{
mediaPlayer = new MediaPlayer();
mediaPlayer.setDisplay(surfaceHolder);//mediaPlayer绑定surfaceHolder
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);//设置音频流类型
mediaPlayer.setOnBufferingUpdateListener(this);//监听网络变化
mediaPlayer.setOnPreparedListener(this);//监听影片是否ready OK
mediaPlayer.setOnCompletionListener(this);//监听影片是否播放完全
}
@Override
public void surfaceDestroyed(SurfaceHolder holder)
{
}
@Override
public void onPrepared(MediaPlayer mp)
{
//影片ready好后,通过mediaPlayer的长宽判断是否有影片
videoHeight = mediaPlayer.getVideoHeight();
videoWidth = mediaPlayer.getVideoWidth();
if(videoHeight != 0 && videoWidth != 0)
{
mp.start();//start播放视频
}
}
@Override
public void onCompletion(MediaPlayer mp)
{
//播放完影片后调用
}
@Override
public void onBufferingUpdate(MediaPlayer mp, int percent)
{
// TODO Auto-generated method stub
playerSeekBar.setSecondaryProgress(percent);//seekBar的二级进度条
}
}
最后
以上就是尊敬夏天为你收集整理的android通过点击播放视频,Android通过MediaPlayer实现播放视频实例的全部内容,希望文章能够帮你解决android通过点击播放视频,Android通过MediaPlayer实现播放视频实例所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复