一、本地音频的播放:
资料:
1、本地资源需在res目录下创建一个文件夹,命名为raw,来保存音频。
2、创建一个文件布局,命名activity_main.xml
<?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:layout_width="match_parent" android:layout_height="match_parent"
tools:context=".MainActivity"> <!--本地文件音频播放--> <!--滚动条--> <SeekBar
android:id="@+id/main_sb" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_weight="5" />
<!--控制播放和暂停--> <ImageButton android:id="@+id/main_ib"
android:layout_width="0dp" android:layout_height="wrap_content"
android:layout_weight="1" /> </LinearLayout>
3、创建一个Activity,命名为MainActivity.java
package zking.com.android6; import android.media.MediaPlayer; import
android.support.v7.app.AppCompatActivity; import android.os.Bundle; import
android.util.Log; import android.view.View; import android.widget.ImageButton;
import android.widget.SeekBar; import android.widget.TextView; import
android.widget.Toast; import org.w3c.dom.Text; public class MainActivity
extends AppCompatActivity { private ImageButton main_ib; private SeekBar
main_sb; private MediaPlayer mediaPlayer; @Override protected void
onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //得到按钮对象 main_ib =
findViewById(R.id.main_ib); //设置播放图标
main_ib.setImageResource(android.R.drawable.ic_media_play); //实现播放
main_ib.setOnClickListener(new View.OnClickListener() { @Override public void
onClick(View v) { //判断是否为第一次 if (mediaPlayer == null) { //开始创建MediaPlayer
//pp为音频文件名 mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.pp);
//如果为第一次则播放 mediaPlayer.start();
main_ib.setImageResource(android.R.drawable.ic_media_pause); //获取当前音频时长
//把最大音频时长赋给滚动条 main_sb.setMax(mediaPlayer.getDuration()); //调用线程(start启动线程) new
myThread().start(); } else if (mediaPlayer.isPlaying()) { //如果处于正在播放状态中,则暂停
mediaPlayer.pause();
main_ib.setImageResource(android.R.drawable.ic_media_play); } else { //暂停后点击为播放
mediaPlayer.start();
main_ib.setImageResource(android.R.drawable.ic_media_pause); } } }); //得到滚动条对象
main_sb = findViewById(R.id.main_sb); //滚动条实现快进效果
main_sb.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)
{ //得到当前播放时长,赋值给mediaPlayer mediaPlayer.seekTo(main_sb.getProgress()); } }); }
//创建一个线程 class myThread extends Thread { @Override public void run() {
//判断当前的位置是不是小于播放总长 while (main_sb.getProgress() <= main_sb.getMax()) {
//得到当前音频播放位置 //设置滚动条当前位置 main_sb.setProgress(mediaPlayer.getCurrentPosition());
} } } }
二、本地视频的播放:
1、本地资源需在res目录下创建一个文件夹,命名为raw,来保存视频。
2、创建一个文件布局,命名activity_main2.xml
<?xml version="1.0" encoding="utf-8"?> <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"
tools:context=".Main2Activity"> <SurfaceView android:id="@+id/surfaceView"
android:layout_width="match_parent" android:layout_height="400dp" />
<ProgressBar android:id="@+id/progressBar" android:layout_width="wrap_content"
android:layout_height="wrap_content" /> <!--进度条--> <SeekBar
android:id="@+id/seekBar" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_below="@+id/surfaceView"/>
</RelativeLayout>
3、创建一个Activity,命名为Main2Activity.java
package zking.com.android6; import android.media.MediaPlayer; import
android.net.Uri; import android.support.v7.app.AppCompatActivity; import
android.os.Bundle; import android.view.SurfaceHolder; import
android.view.SurfaceView; import android.view.View; import
android.widget.ProgressBar; import android.widget.SeekBar; public class
Main3Activity extends AppCompatActivity { private SurfaceView surfaceView;
private ProgressBar progressBar; private SurfaceHolder surfaceHolder; private
MediaPlayer mediaPlayer; private SeekBar seekBar; @Override protected void
onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2); surfaceView =
findViewById(R.id.surfaceView); progressBar = findViewById(R.id.progressBar);
//得到滚动条对象 seekBar = findViewById(R.id.seekBar); mediaPlayer = new
MediaPlayer(); //raw为文件夹名,ofo为视频名 String uri = "android.resource://" +
getPackageName() + "/raw/" + R.raw.ofo; try { mediaPlayer.setDataSource(this,
Uri.parse(uri)); surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(new MyCallBack()); mediaPlayer.prepare();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override public void onPrepared(MediaPlayer mp) {
progressBar.setVisibility(View.INVISIBLE); mediaPlayer.start();
mediaPlayer.setLooping(true); //获取当前音频时长 //把最大音频时长赋给滚动条
seekBar.setMax(mediaPlayer.getDuration()); //调用线程(start启动线程) new
myThread().start(); } }); } catch (Exception e) { e.printStackTrace(); }
//滚动条实现快进效果 seekBar.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) {
//得到当前播放时长,赋值给mediaPlayer mediaPlayer.seekTo(seekBar.getProgress()); } }); }
private class MyCallBack implements SurfaceHolder.Callback { @Override public
void surfaceCreated(SurfaceHolder holder) { mediaPlayer.setDisplay(holder); }
@Override public void surfaceChanged(SurfaceHolder holder, int format, int
width, int height) { } @Override public void surfaceDestroyed(SurfaceHolder
holder) { } } //创建一个线程 class myThread extends Thread { @Override public void
run() { //判断当前的位置是不是小于播放总长 while (seekBar.getProgress() <= seekBar.getMax()) {
//得到当前音频播放位置 //设置滚动条当前位置 seekBar.setProgress(mediaPlayer.getCurrentPosition());
} } } }
热门工具 换一换