本文主要是介绍MediaPlayer播放音频文件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1,普通MediaPlayer播放音频文件
activity_sound_player.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="点击播放,播放音乐" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><Buttonandroid:id="@+id/play"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="播放" /><Buttonandroid:id="@+id/pause"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="暂停" /><Buttonandroid:id="@+id/stop"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="停止" /></LinearLayout><TextViewandroid:id="@+id/desc"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="状态描述" /></LinearLayout>
SoundPlayerActivity.java
/*** 功能描述:测试MediaPlayer播放音乐* 时间:2016/8/4* 作者:vision*/
public class SoundPlayerActivity extends AppCompatActivity {/*** 播放音乐状态控制按钮*/private Button play, pause, stop;/*** 当前状态描述*/private TextView desc;/*** 音频播放器*/private MediaPlayer mediaPlayer;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_sound_player);play = (Button) findViewById(R.id.play);pause = (Button) findViewById(R.id.pause);stop = (Button) findViewById(R.id.stop);desc = (TextView) findViewById(R.id.desc);mediaPlayer = MediaPlayer.create(SoundPlayerActivity.this, R.raw.b);play.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {if (mediaPlayer != null) {try {mediaPlayer.reset();mediaPlayer.prepare();mediaPlayer.start();} catch (Exception e) {e.printStackTrace();}} else {try {mediaPlayer = MediaPlayer.create(SoundPlayerActivity.this, R.raw.b);mediaPlayer.prepare();mediaPlayer.start();} catch (Exception e) {e.printStackTrace();}}desc.setText("播放中");}});pause.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {if (mediaPlayer.isPlaying()) {mediaPlayer.pause();}desc.setText("暂停中");}});stop.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {if (mediaPlayer.isPlaying()) {mediaPlayer.stop();}desc.setText("停止播放");}});}protected void onDestroy() {if (mediaPlayer.isPlaying()) {mediaPlayer.stop();}mediaPlayer.release();//释放资源super.onDestroy();}}
2,SurfaceView结合MediaPlayer播放音频文件
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical"android:weightSum="1"><TextViewandroid:id="@+id/text_desc"android:layout_width="match_parent"android:layout_height="wrap_content" /><SurfaceViewandroid:id="@+id/surfaceview"android:layout_width="400dip"android:layout_height="450dip"android:layout_gravity="center"android:layout_weight="0.25" /><LinearLayoutandroid:id="@+id/ll"android:layout_width="fill_parent"android:layout_height="wrap_content"android:gravity="center_horizontal"><Buttonandroid:id="@+id/bt1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="播放" /><Buttonandroid:id="@+id/bt2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="暂停" /><Buttonandroid:id="@+id/bt3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="停止" /></LinearLayout>
</LinearLayout>
VideoPlayorActivity.java
/*** 功能描述:SurfaceView播放音乐* 时间:2016/8/4* 作者:vision*/
public class VideoPlayorActivity extends AppCompatActivity implements SurfaceHolder.Callback {/*** SurfaceView作播放器*/private SurfaceView surfaceView;/*** SurfaceView控制器*/private SurfaceHolder holder;/*** 开始播放*/private Button play;/*** 暂停*/private Button pause;/*** 停止*/private Button stop;/*** 视频播放器*/private MediaPlayer mediaPlayer;private TextView path;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);surfaceView = (SurfaceView) findViewById(R.id.surfaceview);play = (Button) findViewById(R.id.bt1);pause = (Button) findViewById(R.id.bt2);stop = (Button) findViewById(R.id.bt3);path = (TextView) findViewById(R.id.text_desc);path.setText(Environment.getExternalStorageDirectory().getPath() + "/a.mp3");holder = surfaceView.getHolder();holder.addCallback(this);holder.setFixedSize(400, 450);//设置视频播放分辨率holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);//Surface类型play.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {mediaPlayer.start();}});pause.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {mediaPlayer.pause();}});stop.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {mediaPlayer.stop();}});}@Overridepublic void surfaceCreated(SurfaceHolder holder) {mediaPlayer = new MediaPlayer();
// mediaPlayer = MediaPlayer.create(VideoPlayorActivity.this, R.raw.b);mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);//设置数据源类型mediaPlayer.setDisplay(this.holder);//设置播放展示Viewtry {mediaPlayer.setDataSource(Environment.getExternalStorageDirectory() + "/a.mp3");
// mediaPlayer.setDataSource("/storage/sdcard0/a.mp3");
// mediaPlayer.setDataSource(String.valueOf(R.raw.b));mediaPlayer.prepare();} catch (Exception e) {e.printStackTrace();}}@Overridepublic void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}@Overridepublic void surfaceDestroyed(SurfaceHolder holder) {if (mediaPlayer.isPlaying()) {mediaPlayer.stop();}mediaPlayer.release();}
}
源码下载
成功的人不是赢在起点,而是赢在转折点。
这篇关于MediaPlayer播放音频文件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!