此知识点比较简单,直接上图和代码:





MainActivity:

import java.io.File;import android.app.Activity;import android.media.MediaPlayer;import android.os.Bundle;import android.os.Environment;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity implements OnClickListener {private Button play;private Button pause;private Button stop;private MediaPlayer mediaPlayer = new MediaPlayer();@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);play = (Button) findViewById(R.id.play);pause = (Button) findViewById(R.id.pause);stop = (Button) findViewById(R.id.stop);play.setOnClickListener(this);pause.setOnClickListener(this);stop.setOnClickListener(this);initMediaPlayer();//初始化MediaPlayer}//初始化MediaPlayerprivate void initMediaPlayer() {try {//首先通过File对象指定音频文件的路径//此处示例中sd卡中的文件名为“music.mp3”File file = new File(Environment.getExternalStorageDirectory(), "music.mp3");mediaPlayer.setDataSource(file.getPath());//让medieplayer进入准备状态mediaPlayer.prepare();} catch (Exception e) {e.printStackTrace();}}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.play:if (!mediaPlayer.isPlaying()) {mediaPlayer.start();//开始播放}break;case R.id.pause:if (mediaPlayer.isPlaying()) {mediaPlayer.pause();//暂停播放}break;case R.id.stop:if (mediaPlayer.isPlaying()) {//此处是将mediaplayer重置到刚刚创建的状态,也就是还没有设置文件路径的状态mediaPlayer.reset();//停止播放//重新调用重置方法initMediaPlayer();//此处也是可以直接调用stop()方法,用reset仅仅为了演示}break;default:break;}}//在activity被摧毁的时候将mediaPlayer停止并且释放掉@Overrideprotected void onDestroy() {super.onDestroy();if (mediaPlayer != null) {mediaPlayer.stop();mediaPlayer.release();}}}


activity_main:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <Button        android:id="@+id/play"        android:layout_width="0dp"        android:layout_height="wrap_content"        android:layout_weight="1"        android:text="Play" />    <Button        android:id="@+id/pause"        android:layout_width="0dp"        android:layout_height="wrap_content"        android:layout_weight="1"        android:text="Pause" />    <Button        android:id="@+id/stop"        android:layout_width="0dp"        android:layout_height="wrap_content"        android:layout_weight="1"        android:text="Stop" /></LinearLayout>


更多相关文章

  1. Android中Adapter中edittext,checkbox记住状态解决方案(一)
  2. Android-透明状态栏
  3. Android(安卓)NDK 实现视音频播放器源码
  4. Android(安卓)WifiManager 常量
  5. android TelephonyManager
  6. Android进入suspend状态(goToSleep)
  7. android状态机statemachine详解
  8. android 游戏开发入门
  9. 【Android】debug 状态下其签名文件 debug.keystore 相关(如何获

随机推荐

  1. Logstash 之 重要插件
  2. Python是什么?Python有哪些框架?
  3. 自动修改centos服务器IP地址
  4. YUM变量缺失导致的问题小记
  5. 如何去使用Python爬虫来爬取B站的弹幕数
  6. Rancher面板权限配置
  7. Docker遇到Syslog的踩坑小记
  8. Linux主要应用范围有哪些?Linux学习
  9. 【3.15-3.21】上周精彩回顾
  10. Python是什么?为什么Python受欢迎?