您现在的位置: 万盛学电脑网 >> 程序编程 >> 网络编程 >> 安卓开发 >> 正文

AndroidMediaPlayer播放mp3的实例教程介绍

作者:佚名    责任编辑:admin    更新时间:2022-06-22

大家知道AndroidMediaPlayer播放mp3的实例吗?下面我们就给大家详细介绍一下吧!我们积累了一些经验,在此拿出来与大家分享下,请大家互相指正。

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout   android:layout_width="fill_parent"   android:layout_height="fill_parent"   android:background="@drawable/white">   <TextView     android:id="@+id/myTextView1"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:text="@string/hello"     android:layout_alignParentTop="true"     android:layout_alignParentLeft="true"   >   </TextView>   <ImageButton     android:id="@+id/myButton1"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:src="@drawable/play"     android:layout_below="@+id/myTextView1"   >   </ImageButton>   <ImageButton     android:id="@+id/myButton3"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:src="@drawable/pause"     android:layout_alignTop="@+id/myButton1"     android:layout_toRightOf="@+id/myButton1"   >   </ImageButton>   <ImageButton     android:id="@+id/myButton2"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:src="@drawable/stop"     android:layout_alignTop="@+id/myButton1"     android:layout_toRightOf="@+id/myButton3"   >   </ImageButton> </RelativeLayout>

Step 4 :主控制程序MediaPlayerDemo.java的实现,代码如下:

package com.android.test;   import android.app.Activity;  import android.media.MediaPlayer;  import android.os.Bundle;  import android.view.View;  import android.widget.ImageButton;  import android.widget.TextView;   public class MediaPlayerDemo extends Activity {        private ImageButton mb1,mb2,mb3;   private TextView tv;   private MediaPlayer mp;   //声明一个变量判断是否为暂停,默认为false   private boolean isPaused = false;      public void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.main);                    //通过findViewById找到资源          mb1 = (ImageButton)findViewById(R.id.myButton1);          mb2 = (ImageButton)findViewById(R.id.myButton2);          mb3 = (ImageButton)findViewById(R.id.myButton3);          tv = (TextView)findViewById(R.id.myTextView1);                    //创建MediaPlayer对象,将raw文件夹下的lovefool.mp3          mp = MediaPlayer.create(this,R.raw.lovefool);          //增加播放音乐按钮的事件           @Override    public void onClick(View v) {      try {            if(mp != null)       {        mp.stop();       }           mp.prepare();       mp.

相信大家已经学会AndroidMediaPlayer播放mp3的实例了吧!感谢大家对我们网站的支持!

相关推荐:

android动态更改屏幕方向的代码介绍