软件编程
位置:首页>> 软件编程>> Android编程>> Android 使用mediaplayer播放res/raw文件夹中的音乐的实例

Android 使用mediaplayer播放res/raw文件夹中的音乐的实例

作者:lqh  发布时间:2023-12-19 08:28:11 

标签:Android,mediaplayer,res/raw,音乐

Android 使用mediaplayer播放res/raw文件夹中的音乐的实例

(1)在res文件夹中新建一个文件夹重命名为raw,并且将要播放的音乐放到raw文件夹里面

(2)修改layout目录下的xml布局文件,添加3个按钮空间和一个文本控件,用于提示当前播放状态和 播放暂停 停止等功能。具体代码如下


<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:text="单击播放开始播放音乐" />

<LinearLayout
   android:orientation="horizontal"
   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:text="暂停" />

<Button
   android:id="@+id/button3"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="停止" />
 </LinearLayout>

</LinearLayout>

(3)打开MainActivity 在该类中,定义所需的成员变量,具体代码如下


private MediaPlayer mp;//mediaPlayer对象
private Button play,pause,stop;//播放 暂停/继续 停止 按钮
private TextView hint;//显示当前播放状态
private boolean isPause=false;//是否暂停

(4)在onCreate()方法中,获取播放 暂停/继续 停止 按钮 提示当前状态的文本框,并为mediaplayer对象创建播放的对象,具体代码如下。


play=(Button) findViewById(R.id.button1);
pause=(Button) findViewById(R.id.button2);
stop=(Button) findViewById(R.id.button3);
hint=(TextView) findViewById(R.id.hint);
hint.setTextSize(20);
mp=MediaPlayer.create(MainActivity.this, R.raw.sound);//创建mediaplayer对象

(5)编写用于播放音乐的无返回值的play()方法。在该方法中首先调用mediaplayer对象的reset()方法重置mediaplayer对象,然后重新为其设置要播放的音频文件。最后调用start()方法开始播放音频


private void play(){
 try{
   mp.reset();
   mp=MediaPlayer.create(MainActivity.this, R.raw.sound);//重新设置要播放的音频
   mp.start();//开始播放
   hint.setText("正在播放音频...");
   play.setEnabled(false);
   pause.setEnabled(true);
   stop.setEnabled(true);
 }catch(Exception e){
   e.printStackTrace();//输出异常信息
 }
}

(6)为mediaplayer对象添加完成时间 * ,用于当音乐播放完毕后重新开始播放音乐


mp.setOnCompletionListener(new OnCompletionListener() {

@Override
     public void onCompletion(MediaPlayer arg0) {
       // TODO Auto-generated method stub
       play();//重新开始播放
     }
   });

(7)为播放按钮添加单击事件 *


play.setOnClickListener(new OnClickListener() {

@Override
   public void onClick(View v) {
     // TODO Auto-generated method stub
     play();
     if(isPause){
       pause.setText("暂停");
       isPause=false;
     }
   }
 });

(8)为暂停按钮添加单击事件 *


pause.setOnClickListener(new OnClickListener() {

@Override
     public void onClick(View v) {
       // TODO Auto-generated method stub
       if(mp.isPlaying()&&!isPause){
         mp.pause();
         isPause=true;
         pause.setText("继续");
         hint.setText("暂停播放音频...");
         play.setEnabled(true);
       }else{
         mp.start();
         pause.setText("暂停");
         hint.setText("继续播放音频...");
         isPause=false;
         play.setEnabled(false);
       }
     }
   });

(9)为停止按钮添加单击事件 *


stop.setOnClickListener(new OnClickListener() {

@Override
     public void onClick(View v) {
       // TODO Auto-generated method stub
       mp.stop();
       hint.setText("停止播放音频...");
       pause.setEnabled(false);
       stop.setEnabled(false);
       play.setEnabled(true);
     }
   });

(10)一定要记得这个。重写Activity的onDestroy()方法,用于在当前Activity销毁时,停止正在播放的音频,并释放mediaplayer所占用的资源,否则你每打开一次就会播放一次,并且上次播放的不会停止 你可以试试的,我解释不清楚


protected void onDestroy() {
 // TODO Auto-generated method stub
 if(mp.isPlaying()){
   mp.stop();
 }
 mp.release();//释放资源
 super.onDestroy();
}

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

来源:http://blog.csdn.net/su20145104009/article/details/50640025

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com