android 播放音乐示例

转载自 http://byandby.iteye.com/blog/845089

这个例子是播放音乐的一个小例子也比较简单,大家需要去下载2个.mp3结尾的歌曲,然后放到SD卡里边 进行测试,就可以看到效果了。 运行效果如下


android 播放音乐示例

main.xml

<?xml version="1.0" encoding="utf-8"?>  <AbsoluteLayout     xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    >    <ListView     android:id="@id/android:list"    android:layout_width="fill_parent"     android:layout_height="fill_parent"    android:layout_weight="1"    android:drawSelectorOnTop="false"/>    <ImageButton    android:id="@+id/LastImageButton"    android:layout_width="wrap_content"    android:layout_height="wrap_content"      android:layout_x="10px"    android:layout_y="70px"    android:src="@drawable/last"      />    <ImageButton    android:id="@+id/StopImageButton"     android:layout_height="wrap_content"      android:layout_width="wrap_content"    android:layout_x="70px"    android:layout_y="70px"    android:src="@drawable/stop"     />      <ImageButton    android:id="@+id/StartImageButton"     android:layout_height="wrap_content"      android:layout_width="wrap_content"     android:layout_x="130px"    android:layout_y="70px"    android:src="@drawable/start"     />    <ImageButton    android:id="@+id/PauseImageButton"     android:layout_height="wrap_content"      android:layout_width="wrap_content"     android:layout_x="190px"    android:layout_y="70px"     android:src="@drawable/pause"     />      <ImageButton    android:id="@+id/NextImageButton"    android:layout_width="wrap_content"    android:layout_height="wrap_content"     android:layout_x="250px"    android:layout_y="70px"    android:src="@drawable/next"      />  </AbsoluteLayout>

musicitme.xml

<?xml version="1.0" encoding="utf-8"?>  <TextView xmlns:android="http://schemas.android.com/apk/res/android"      android:id="@+id/TextView01"      android:layout_width="fill_parent"       android:layout_height="wrap_content"       android:text="@string/hello"/> 

只有一个类 Activity01

package com.yarin.android.Examples_07_01;    import java.io.File;  import java.io.FilenameFilter;  import java.io.IOException;  import java.util.ArrayList;  import java.util.List;  import android.app.ListActivity;  import android.media.MediaPlayer;  import android.media.MediaPlayer.OnCompletionListener;  import android.os.Bundle;  import android.view.KeyEvent;  import android.view.View;  import android.widget.ArrayAdapter;  import android.widget.ImageButton;  import android.widget.ListView;    public class Activity01 extends ListActivity   {      /* 几个操作按钮 */      private ImageButton mFrontImageButton   = null;      private ImageButton mStopImageButton    = null;      private ImageButton mStartImageButton   = null;      private ImageButton mPauseImageButton   = null;      private ImageButton mNextImageButton    = null;        /* MediaPlayer对象 */      public MediaPlayer  mMediaPlayer        = null;            /* 播放列表 */      private List<String> mMusicList = new ArrayList<String>();            /* 当前播放歌曲的索引 */      private int currentListItme = 0;            /* 音乐的路径 */      private static final String MUSIC_PATH = new String("/sdcard/");        /** Called when the activity is first created. */      @Override      public void onCreate(Bundle savedInstanceState)      {          super.onCreate(savedInstanceState);          setContentView(R.layout.main);          /* 更新显示播放列表 */          musicList();          /* 构建MediaPlayer对象 */          mMediaPlayer        = new MediaPlayer();            mFrontImageButton = (ImageButton) findViewById(R.id.LastImageButton);           mStopImageButton = (ImageButton) findViewById(R.id.StopImageButton);          mStartImageButton = (ImageButton) findViewById(R.id.StartImageButton);           mPauseImageButton = (ImageButton) findViewById(R.id.PauseImageButton);          mNextImageButton = (ImageButton) findViewById(R.id.NextImageButton);                     //停止按钮          mStopImageButton.setOnClickListener(new ImageButton.OnClickListener()           {              @Override              public void onClick(View v)              {                  /* 是否正在播放 */                  if (mMediaPlayer.isPlaying())                  {                      //重置MediaPlayer到初始状态                      mMediaPlayer.reset();                  }              }          });                     //开始按钮          mStartImageButton.setOnClickListener(new ImageButton.OnClickListener()           {              @Override              public void onClick(View v)              {                  playMusic(MUSIC_PATH + mMusicList.get(currentListItme));              }          });                      //暂停          mPauseImageButton.setOnClickListener(new ImageButton.OnClickListener()           {              public void onClick(View view)              {                  if (mMediaPlayer.isPlaying())                  {                      /* 暂停 */                      mMediaPlayer.pause();                  }                  else                   {                      /* 开始播放 */                      mMediaPlayer.start();                  }              }          });                    //下一首          mNextImageButton.setOnClickListener(new ImageButton.OnClickListener()           {              @Override              public void onClick(View arg0)              {                  nextMusic();              }          });          //上一首          mFrontImageButton.setOnClickListener(new ImageButton.OnClickListener()           {              @Override              public void onClick(View arg0)              {                  FrontMusic();              }          });      }            /*<----------------------------------------------------------------->*/            public boolean onKeyDown(int keyCode, KeyEvent event)      {          if ( keyCode ==  KeyEvent.KEYCODE_BACK)          {              mMediaPlayer.stop();              mMediaPlayer.release();              this.finish();              return true;          }          return super.onKeyDown(keyCode, event);      }                  @Override      /* 当我们点击列表时,播放被点击的音乐 */      protected void onListItemClick(ListView l, View v, int position, long id)      {          currentListItme = position;          playMusic(MUSIC_PATH + mMusicList.get(position));      }          /* 播放列表 */      public void musicList()      {          //取得指定位置的文件设置显示到播放列表          File home = new File(MUSIC_PATH);          if (home.listFiles(new MusicFilter()).length > 0)          {              for (File file : home.listFiles(new MusicFilter()))              {                  mMusicList.add(file.getName());              }              ArrayAdapter<String> musicList = new ArrayAdapter<String>(Activity01.this,R.layout.musicitme, mMusicList);              setListAdapter(musicList);          }      }            private void playMusic(String path)      {          try          {              /* 重置MediaPlayer */              mMediaPlayer.reset();              /* 设置要播放的文件的路径 */              mMediaPlayer.setDataSource(path);              /* 准备播放 */              mMediaPlayer.prepare();              /* 开始播放 */              mMediaPlayer.start();              mMediaPlayer.setOnCompletionListener(new OnCompletionListener()               {                  public void onCompletion(MediaPlayer arg0)                  {                      //播放完成一首之后进行下一首                      nextMusic();                  }              });          }catch (IOException e){}      }        /* 下一首 */      private void nextMusic()      {          if (++currentListItme >= mMusicList.size())          {              currentListItme = 0;          }          else          {              playMusic(MUSIC_PATH + mMusicList.get(currentListItme));          }      }            /* 上一首 */      private void FrontMusic()      {          if (--currentListItme >= 0)          {              currentListItme = mMusicList.size();          }          else          {              playMusic(MUSIC_PATH + mMusicList.get(currentListItme));          }      }    }    /* 过滤文件类型 */  class MusicFilter implements FilenameFilter  {      public boolean accept(File dir, String name)      {          //这里还可以设置其他格式的音乐文件          return (name.endsWith(".mp3"));      }  } 

源码见附件,大家需要下载2首.mp3 结尾的歌曲进行测试

更多相关文章

  1. 阅读《Android 从入门到精通》(7)——图片按钮
  2. h5页面点击按钮,触发手机自带的发短信
  3. android 2.3 4.0 添加开机音乐
  4. Android RadioButton 单选按钮
  5. 如何在Android中在fragment中实现点击按钮事件?
  6. Android 之 添加按钮添加声音
  7. Android:week 13总结 绑定服务、音乐播放器(服务)
  8. Android 设置圆角按下改变颜色按钮

随机推荐

  1. 2018 Android 框架汇总(转)
  2. Android 加速度传感器 (G-Sensor) 收
  3. android之Menu 实例与详解
  4. maven 学习笔记(二)-创建简单的eclipse+and
  5. android菜鸟学习笔记15----Android Junit
  6. android 4.4.3 css hack 写法
  7. AndroidStudio-Eat-Guide—— 1.下载安装
  8. 通过Titanium Studio为Android APK签名
  9. xUtils3注解方式编程
  10. android 9.0接收静态广播