Android的视频播放有两种,如果你是想自己做个视频播放软件的话用MediaPlayer加SurfaceView实现,第二种调用Android自带VideoView实现,两种比较来说第二种简单容易,但是你如果想把播放软件做的用户体验跟爱奇艺一样的话,那就用第一种吧。
第一种实现方法
1.布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_two"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<SurfaceView
android:layout_width="wrap_content"
android:layout_height="300dp"
android:id="@+id/sv_main_surface"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<SeekBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/seekBar_main"
android:layout_weight="1"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_media_play"
android:onClick="mediapl"
/>
</LinearLayout>
</LinearLayout>
2.java代码
public class TwoActivity extends AppCompatActivity {
private SeekBar seekBar_main;
private MediaPlayer mediaPlayer;
private SurfaceView sv_main_surface;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
seekBar_main = (SeekBar) findViewById(R.id.seekBar_main);
sv_main_surface = (SurfaceView) findViewById(R.id.sv_main_surface);
//给进度条设置拖动事件
seekBar_main.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
//值发生改变时调用
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
}
//开始触摸时调用
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
//停止触摸时调用
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
//获取停止拖动后的进度
int progress = seekBar.getProgress();
mediaPlayer.seekTo(progress);
}
});
}
public void mediapl(View view) throws IOException {
ImageButton imageButton = (ImageButton) view;
// if (mediaPlayer == null) {
mediaPlayer = new MediaPlayer();
//设置音频流的类型
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
//设置音频的来源
mediaPlayer.setDataSource(this, Uri.parse("file://mnt/sdcard/Movies/sister.mp4"));
//准备一下
mediaPlayer.prepare();
//将媒体播放器捕捉的画面展示
mediaPlayer.setDisplay(sv_main_surface.getHolder());
//播放音乐
mediaPlayer.start();
//当音乐播放时改变图片
imageButton.setImageResource(android.R.drawable.ic_media_pause);
//获取音乐的播放时间长度
int duration = mediaPlayer.getDuration();
//设置进度条的最大值
seekBar_main.setMax(duration);
//调用子线程
new Mythread().start();
//如果音频正在播放
}
class Mythread extends Thread {
@Override
public void run() {
while (seekBar_main.getProgress() <= seekBar_main.getMax()) {
//获取音乐的当前播放位置
int current = mediaPlayer.getCurrentPosition();
seekBar_main.setProgress(current);
}
}
}
}
第二张实现方法
1.布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_play_video"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<VideoView
android:id="@+id/vv_video"
android:layout_width="match_parent"
android:layout_height="300dp" />
</LinearLayout>
2.java代码
public class PlayVideoActivity extends AppCompatActivity {
private VideoView vv_video;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play_video);
vv_video = (VideoView) findViewById(R.id.vv_video);
//设置视频路径
vv_video.setVideoPath("file://mnt/sdcard/Movies/sister.mp4");
//实例化媒体控制器
MediaController mediaController=new MediaController(this);
//互相映射一下
mediaController.setMediaPlayer(vv_video);
vv_video.setMediaController(mediaController);
}
}
Android之实现视频播放
最新推荐文章于 2024-05-30 14:05:42 发布