Android MediaPlayer实现音乐播放器实例代码

所属分类: 软件编程 / Android 阅读数: 32
收藏 0 赞 0 分享

Android MediaPlayer实现音乐播放器

1、布局文件

<?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" > 
 
  <TextView 
    android:id="@+id/hint" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:padding="10px" 
    android:text="单击“开始”按钮播放音频" /> 
 
  <LinearLayout 
    android:id="@+id/linearLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 
  <Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="播放" /> 
 
  <Button 
    android:id="@+id/button2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:enabled="false" 
    android:text="暂停" /> 
 
  <Button 
    android:id="@+id/button3" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:enabled="false" 
    android:text="停止" /> 
  </LinearLayout> 
</LinearLayout> 

2、MainActivity的成员变量

private MediaPlayer player;//MediaPlayer对象 
  private boolean isPause = false;//是否暂停 
  private File file;//要播放的音频文件 
  private TextView hint;//声明显示提示信息的文本框 

3、onCreate()方法中获取组件

final Button button1 = (Button)findViewById(R.id.button1);//获取“播放”按钮 
    final Button button2 = (Button)findViewById(R.id.button2);//获取“暂停/继续”按钮 
    final Button button3 = (Button)findViewById(R.id.button3);//获取“停止”按钮 
    hint = (TextView)findViewById(R.id.hint);//获取用于显示提示信息的文本框 
    file = new File("/storage/emulated/0/qqmusic/song/乔维怡 - 白月光[mqms2].mp3");//获取要播放的文件 
    if(file.exists()){ 
      player = MediaPlayer.create(this, Uri.parse(file.getAbsolutePath()));//创建MediaPlayer独享 
    }else{ 
      hint.setText("要播放的音频文件不存在!"); 
      button1.setEnabled(false); 
      return; 
    } 

4、编写play()方法

private void play(){ 
    try { 
      player.reset(); 
      player.setDataSource(file.getAbsolutePath());//重新设置要播放的音频 
      player.prepare();//预加载音频 
      player.start();//开始播放 
      hint.setText("正在播放音频....."); 
    } catch (Exception e) { 
      e.printStackTrace(); 
    } 
  } 

5、为MediaPlayer对象添加监听事件,播完重新播放

player.setOnCompletionListener(new OnCompletionListener() { 
      @Override 
      public void onCompletion(MediaPlayer mp) { 
        play();//重新开始播放 
      } 
    }); 

6、为播放添加单击事件监听器

button1.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
        play();//开始播放音乐 
        if(isPause){ 
          button2.setText("暂停"); 
          isPause = false;//设置暂停标记变量的值为false 
        } 
        button2.setEnabled(true);//“暂停/继续”按钮可用 
        button3.setEnabled(true);//"停止"按钮可用 
        button1.setEnabled(false);//“播放”按钮不可用 
      } 
    }); 

7、在“暂停/继续”按钮添加单击事件监听器

button2.setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick(View v) { 
      if(player.isPlaying()&&!isPause){ 
        player.pause();//暂停播放 
        isPause = true; 
        ((Button)v).setText("继续"); 
        hint.setText("暂停播放音频..."); 
        button1.setEnabled(true);//“播放”按钮可用 
      }else{ 
        player.start();//继续播放 
        ((Button)v).setText("暂停"); 
        hint.setText("正在播放音频..."); 
        isPause = false; 
        button1.setEnabled(false);//“播放”按钮不可用 
      } 
    } 
  }); 

8、停止按钮

button3.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
        player.stop();//停止播放 
        hint.setText("停止播放音频..."); 
        button2.setEnabled(false);//“暂停/继续”按钮不可用 
        button3.setEnabled(false);//“停止”按钮不可用 
        button1.setEnabled(true);//“播放”按钮可用 
      } 
    }); 

9、重写Activity的onDestroy()方法

@Override 
  protected void onDestroy() { 
    if(player.isPlaying()){ 
      player.stop();//停止音频的播放 
    } 
    player.release();//释放资源 
    super.onDestroy(); 
  } 

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

更多精彩内容其他人还在看

android byte[] 和short[]转换的方法代码

这篇文章主要介绍了android byte[] 和short[]转换的方法代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

Android获取应用程序大小的方法

这篇文章主要介绍了Android获取应用程序大小的方法,有需要的朋友可以参考一下
收藏 0 赞 0 分享

Android获取其他包的Context实例代码

这篇文章主要介绍了Android获取其他包的Context实例代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

Android放大镜的实现代码

这篇文章主要介绍了Android放大镜的实现代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

Android 读取Properties配置文件的小例子

这篇文章主要介绍了Android 读取Properties配置文件的小例子,有需要的朋友可以参考一下
收藏 0 赞 0 分享

Android通讯录开发之删除功能的实现方法

这篇文章主要介绍了Android通讯录开发之删除功能的实现方法,有需要的朋友可以参考一下
收藏 0 赞 0 分享

使用ViewPager实现android软件使用向导功能实现步骤

现在的大部分android软件,都是使用说明,就是第一次使用该软件时,会出现向导,可以左右滑动,然后就进入应用的主界面了,下面我们就实现这个功能
收藏 0 赞 0 分享

android在异步任务中关闭Cursor的代码方法

android在异步任务中如何关闭Cursor?在我们开发应用的时候,很多时候会遇到这种问题,下面我们就看看代码如何实现
收藏 0 赞 0 分享

Android自定义桌面功能代码实现

android自定义桌面其实很简单,看一个例子就明白了
收藏 0 赞 0 分享

android将图片转换存到数据库再从数据库读取转换成图片实现代码

有时候我们想把图片存入到数据库中,尽管这不是一种明智的选择,但有时候还是不得以会用到,下面说说将图片转换成byte[]数组存入到数据库中去,并从数据库中取出来转换成图像显示出来
收藏 0 赞 0 分享
查看更多