我们用到一个SurfaceView来展示视屏效果。
这是布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
android:layout_width="match_parent" android:layout_height="match_parent"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
>
<SurfaceView
android:layout_width="match_parent"
android:layout_height="300dp"
android:id="@+id/sv_main_surfaceview"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:id="@+id/tv_main_end"
/>
<SeekBar
android:layout_width="0dp"
android:layout_weight="8"
android:layout_height="wrap_content"
android:id="@+id/sb_main_seebar"
/>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:id="@+id/tv_main_static"
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_media_play"
android:onClick="playorstp"
/>
</LinearLayout>
</LinearLayout>
Activity:
import android.media.MediaPlayer;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private SeekBar sb_main_seebar;
private MediaPlayer mediaPlayer;
private TextView tv_main_static;
private TextView tv_main_end;
private SurfaceView sv_main_surfaceview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sb_main_seebar = (SeekBar) findViewById(R.id.sb_main_seebar);
tv_main_static = (TextView) findViewById(R.id.tv_main_static);
tv_main_end = (TextView) findViewById(R.id.tv_main_end);
sv_main_surfaceview = (SurfaceView) findViewById(R.id.sv_main_surfaceview);
//给进度条设置滑动的监听
sb_main_seebar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
//获取音乐总时间
int duration2=mediaPlayer.getDuration()/1000;
//获取音乐当前播放的位置
int position=mediaPlayer.getCurrentPosition();
tv_main_end.setText(calculateTime(position/1000));
tv_main_static.setText(calculateTime(duration2));
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
int progress=seekBar.getProgress();
//在当前位置播放
mediaPlayer.seekTo(progress);
}
});
}
public void playorstp(View view){
final ImageButton imageButton= (ImageButton) view;
if(mediaPlayer==null){
//播放内存中音频
// mediaPlayer = MediaPlayer.create(this, R.raw.f);
// 播放内存卡中文件
mediaPlayer=new MediaPlayer();
// 设置音源
try {
mediaPlayer.setDataSource(this, Uri.parse("file://mnt//sdcard/aa//bb.mp4")); //内存地址
// mediaPlayer.setDataSource(this, Uri.parse("手机和电脑连接一个同一个互联网情况的IP"));//列如:http://193.168.4.135/internet.mp3
// 准备
mediaPlayer.prepare();
// mediaPlayer.prepareAsync();
} catch (Exception e) {
e.printStackTrace();
}
//准备完成的监听
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
//下载操作(复制)
mediaPlayer.setDisplay(sv_main_surfaceview.getHolder());
mediaPlayer.start();
imageButton.setImageResource(android.R.drawable.ic_media_pause);
//获取音乐的总时长
int duration=mediaPlayer.getDuration();
//将进度条设置最大值为:音乐的总时长
sb_main_seebar.setMax(duration);
String pos = String.valueOf(duration/60/10)+String.valueOf(duration/60%10)
+':'+String.valueOf(duration%60/10)+String.valueOf(duration%60%10);
Log.i("test",pos+"");
new MyThread().start();
}
});
}else if(mediaPlayer.isPlaying()){
mediaPlayer.pause();
imageButton.setImageResource(android.R.drawable.ic_media_play);
}else{
mediaPlayer.start();
imageButton.setImageResource(android.R.drawable.ic_media_pause);
}
}
class MyThread extends Thread{
@Override
public void run() {
super.run();
while(sb_main_seebar.getProgress()<=sb_main_seebar.getMax()){
//获取音乐当前播放的位置
int position=mediaPlayer.getCurrentPosition();
sb_main_seebar.setProgress(position);
}
}
}
public String calculateTime(int time){
int minute;
int second;
if(time>=60){
minute=time/60;
second=time%60;
return minute+":"+second;
}else if(time<60){
second=time;
return "0:"+second;
}
return null;
}
}
需要添加的权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>