MediaPlayer通过如下两个静态方法来加载指定的音频:

1、static MediaPlayer create(Context context,Uri uri):从指定的Uri来装载音频文件,并返回新创建的MediaPlayer对象;

2、static MediaPlayer create(Context context,int resid):从resid资源ID对应的资源文件中装载音频文件,并返回新创建的MediaPlayer对象


一般如果要加载多个文件来循环播放音频文件可以通过MediaPlayer的setDataSource()方法来装载指定的音频文件

1、setDataSource(String path):指定装载path路径代表的文件。

2、setDataSource(FileDescriptor fd,long offset,long length):指定装载fd所代表的文件中从offset开始、长度为length的文件内容;

3、setDataSource(FileDescriptor fd):指定装载fd所代表的文件

4、setDataSource(Context context ,Uri uri):指定装载uri所代表的文件。


MediaPlayer通过如下几个来进行播放控制:

1、prepare():准备音频文件;真正的装载音频文件,相当于缓存

2、start():开始或恢复播放

3、stop():停止播放

4、pause():暂停播放

5、reset():重置为初始状态

6、release():MediaPlayer对象不再使用才去释放mp所占有的资源。不是一首歌完了之后要用的,而是你不再使用mp之后才调用,比如退出

tart和stop之间循环的流程应该是:reset()-->setDataSource(path)-->prepare()-->start()-->stop()--reset()-->重来上述流程一遍

实例


<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=".MainActivity" >    <ImageView        android:id="@+id/image1"        android:layout_margin="15dp"        android:layout_centerInParent="true"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:background="@drawable/cover" />    <LinearLayout        android:id="@+id/l1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:orientation="horizontal" >        <Button            android:id="@+id/start"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="播放" />        <Button            android:id="@+id/pre"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="暂停" />           <Button            android:id="@+id/reset"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="重来" />        <Button            android:id="@+id/stop"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="停止" />    </LinearLayout>        <SeekBar        android:id="@+id/seekbar1"        android:layout_above="@id/l1"        android:layout_width="match_parent"        android:layout_height="wrap_content" /></RelativeLayout>


package com.android.xiong.mediaplayers;import java.io.IOException;import java.util.Date;import java.util.Timer;import java.util.TimerTask;import android.app.Activity;import android.media.MediaPlayer;import android.media.MediaPlayer.OnCompletionListener;import android.media.MediaPlayer.OnErrorListener;import android.os.Bundle;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.SeekBar;import android.widget.SeekBar.OnSeekBarChangeListener;public class MainActivity extends Activity {private Button start, stop, pre, reset;private SeekBar seekbar1;private MediaPlayer mediaPlayer;boolean isperson;int currentPlay = 0;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);start = (Button) findViewById(R.id.start);stop = (Button) findViewById(R.id.stop);pre = (Button) findViewById(R.id.pre);reset = (Button) findViewById(R.id.reset);seekbar1 = (SeekBar) findViewById(R.id.seekbar1);mediaPlayer = MediaPlayer.create(this, R.raw.hello);SeekBarChage seekbar = new SeekBarChage();seekbar1.setOnSeekBarChangeListener(seekbar);// 设置进度条seekbar1.setMax(mediaPlayer.getDuration());start.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {//判断是否正在播放if (mediaPlayer.isPlaying()) {mediaPlayer.seekTo(0);} else {mediaPlayer = MediaPlayer.create(MainActivity.this,R.raw.hello);mediaPlayer.start();}seekbar1.setProgress(0);//更新进度new Timer().schedule(new TimerTask() {@Overridepublic void run() {if (isperson == true)return;seekbar1.setProgress(mediaPlayer.getCurrentPosition());}}, 0, 10);}});reset.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {if (mediaPlayer.isPlaying()) {mediaPlayer.seekTo(0);} else {mediaPlayer = MediaPlayer.create(MainActivity.this,R.raw.hello);mediaPlayer.start();}seekbar1.setProgress(0);}});stop.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {mediaPlayer.stop();}});pre.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {mediaPlayer.pause();}});}class SeekBarChage implements OnSeekBarChangeListener {// 当拖动条发生变化时调用该方法@Overridepublic void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {}// 当用户滑动时调用调用方法@Overridepublic void onStartTrackingTouch(SeekBar seekBar) {isperson = true;}// 鼠标松开时调用该方法@Overridepublic void onStopTrackingTouch(SeekBar seekBar) {currentPlay = seekBar.getProgress();mediaPlayer.seekTo(currentPlay);isperson = false;}}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}



更多相关文章

  1. Android使用SoundPool播放音效
  2. Android:AndroidManifest.xml
  3. Android(安卓)Studio中修改gradle插件版本和Gradle版本
  4. Android中获取全局Context
  5. Android(安卓)ExpandableListView使用小结(一)
  6. 单选菜单
  7. RecyclerView指定布局管理器
  8. Android布局(一)之基本视图View
  9. Android和iOS如何连接指定的蓝牙设备

随机推荐

  1. Android应用程序绑定服务(bindService)的过
  2. 判断网页是运行在iOS上还是Android上
  3. Android 天气预报图文字幕垂直滚动效果
  4. ubuntu出错,开机无法登录
  5. Android如何实现计算缓存空间大小和删除
  6. androidの读取短信内容
  7. Android(安卓)Task和Activity分析
  8. ImageView ScaleType 属性值的意义
  9. Android(安卓)Activity URL Scheme的介绍
  10. Android中Manifest.xml配置文件