Android music player

2010-05-25  来源:本站原创  分类:Mobile  人气:457 

The basis of its own music player in the materials to do some of the extensions, tidy for later use, relatively simple functions, including playlist, play list click function, the progress bar, loop and so on. Like to refer to write more chaotic, embarrassing. References to "Google Android SDK development paradigm Filmography"

import java.io.IOException;
import java.util.List;

import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.TextView;

public class MyMusicPlayer extends Activity
{
  private ImageButton mPause, mNext, mBefore, mStart, mStop;
  private TextView mTextView1;
  private ImageView mImageView1;
  private boolean bIsReleased = false;
  private boolean bIsPaused = false;
  public SeekBar seekBr;
  public ListView musics;
  public MediaPlayer myPlayer1 = new MediaPlayer();
  public int musicIndex = 0;
  private String _DIR = "/sdcard/";
  private List<String> musicList = FindMusics.getSD();
  private int count = musicList.size();

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main); 

    mStart = (ImageButton) findViewById(R.id.myImageButton1);
    mStop = (ImageButton) findViewById(R.id.myImageButton2);
    mPause = (ImageButton) findViewById(R.id.pause);
    mNext = (ImageButton) findViewById(R.id.next);
    mBefore = (ImageButton) findViewById(R.id.before);
    mImageView1 = (ImageView) findViewById(R.id.myImageView1);
    mTextView1 = (TextView) findViewById(R.id.myTextView1);
    seekBr = (SeekBar) findViewById(R.id.seekb);
    musics = (ListView) findViewById(R.id.musicList);

    ArrayAdapter< String> adapter = new ArrayAdapter< String>(
       this,
       android.R.layout.simple_list_item_1);

    for(int i = 0; i < count; i++){
      adapter.add(musicList.get(i).toString());
    }

    musics.setAdapter(adapter);
    musics.setOnItemClickListener(new AdapterView.OnItemClickListener(){

      public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
          long arg3)
      {
        startMusic(arg2);
      }

    });
    seekBr.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){

      public void onProgressChanged(SeekBar seekBar, int progress,
          boolean fromUser)
      {
        // TODO Auto-generated method stub

      }

      public void onStartTrackingTouch(SeekBar seekBar)
      {
        // TODO Auto-generated method stub

      }

      public void onStopTrackingTouch(SeekBar seekBar)
      {
        // TODO Auto-generated method stub
          myPlayer1.seekTo(seekBr.getProgress());
      }

    });

    /* Open the ﹍ ku   */
    mStart.setOnClickListener(new ImageButton.OnClickListener()
    {
      public void onClick(View v)
      {
        // TODO Auto-generated method stub
        mStart.setImageResource(R.drawable.stars);
        mImageView1.setImageResource(R.drawable.dance);
        mPause.setImageResource(R.drawable.pause);

        startMusic(musicIndex);
        process();

        myPlayer1.setOnCompletionListener(new OnCompletionListener()
        {
          // @Override
          public void onCompletion(MediaPlayer arg0)
          {
            mTextView1.setText(R.string.str_finished);
            mStart.setImageResource(R.drawable.star);

            if(musicIndex >= count -1 ){
              musicIndex = 0;
            }else{
              musicIndex ++;
            }
            myPlayer1.reset();
            startMusic(musicIndex);
            process();
            mTextView1.setText(musicList.get(musicIndex).toString());
          }
        });
      }
    });     

    mPause.setOnClickListener(new ImageButton.OnClickListener()
    {
      public void onClick(View view)
      {
        if (myPlayer1 != null)
        {
          if(bIsReleased == false)
          {
            if(bIsPaused==false)
            {
              myPlayer1.pause();
              bIsPaused = true;
              mTextView1.setText(R.string.str_pause);
              mStart.setImageResource(R.drawable.star);
              mPause.setImageResource(R.drawable.pause_2);
            }
            else if(bIsPaused==true)
            {
              myPlayer1.start();
              bIsPaused = false;
              mTextView1.setText(R.string.str_start);
              mStart.setImageResource(R.drawable.stars);
              mPause.setImageResource(R.drawable.pause);
            }
          }
        }
      }
    }); 

    mNext.setOnClickListener(new ImageButton.OnClickListener()
    {

      public void onClick(View arg0)
      {
        // TODO Auto-generated method stub
        mStart.setImageResource(R.drawable.stars);
        mImageView1.setImageResource(R.drawable.dance);    

        if(musicIndex >= count-1){
          musicIndex = 0;
        }else{
          musicIndex++;
        }
        startMusic(musicIndex);
        process();
      }
    });

    mBefore.setOnClickListener(new ImageButton.OnClickListener()
    {

      public void onClick(View arg0)
      {
        // TODO Auto-generated method stub
        mStart.setImageResource(R.drawable.stars);
        mImageView1.setImageResource(R.drawable.dance);

        startMusic(musicIndex);
        process();
      }
    });

    mStop.setOnClickListener(new ImageButton.OnClickListener()
    {
      public void onClick(View v)
      {
        // TODO Auto-generated method stub 

        if(myPlayer1.isPlaying()==true)
        {
          myPlayer1.reset();
          mTextView1.setText(R.string.str_stopped);
          mStart.setImageResource(R.drawable.star);
          mPause.setImageResource(R.drawable.pause);
          mImageView1.setImageResource(R.drawable.black);
        }
      }
    });
  } 

  public void process(){
    seekBr.setMax(myPlayer1.getDuration());
    new Thread(new Runnable(){
      public void run() {
        while(true){
        seekBr.setProgress(myPlayer1.getCurrentPosition()); // Get the current playback progress value
        if(seekBr.getMax() == myPlayer1.getCurrentPosition()){
          seekBr.setProgress(0);
        }
        try {
          Thread.sleep(1000);
          } catch (InterruptedException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
          }
        }
    }}).start();
  }

  public void startMusic(int musicIndex){
    try
    {
      if(myPlayer1.isPlaying()==true)
      {
        myPlayer1.reset();
      }

      myPlayer1.setDataSource(_DIR + musicList.get(musicIndex).toString());
      myPlayer1.prepare();
      myPlayer1.start();
      mTextView1.setText(musicList.get(musicIndex).toString());
    }
    catch (IllegalStateException e)
    {
      // TODO Auto-generated catch block
      mTextView1.setText(e.toString());
      e.printStackTrace();
    }
    catch (IOException e)
    {
      // TODO Auto-generated catch block
      mTextView1.setText(e.toString());
      e.printStackTrace();
    }
  }

}

Songs in the class to read sdCard

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class FindMusics
{

  public static List<String> getSD()
  {
    List<String> musicList = new ArrayList<String>();
    File f = new File("/sdcard");
    File[] files = f.listFiles();
    for (int i = 0; i < files.length; i++)
    {
      File file = files[i];
      if (getMusicFile(file.getName()))
      {
        musicList.add(file.getName());
      }
    }
    return musicList;
  }

  public static boolean getMusicFile(String path)
  {

   // boolean rs;
    String end = path.substring(path.lastIndexOf(".") + 1, path.length())
        .toLowerCase();

    if (end.equals("mp3"))
      return true;
    else
      return false;
  }
}

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"
  >
  <!--  The establishment of a  TextView -->
  <TextView
  android:id="@+id/myTextView1"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:layout_y="236px"
  android:text="@string/hello"
  />

  <!--  The establishment of the first [citation needed]  ImageButton -->
  <ImageButton
  android:id="@+id/myImageButton1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_x="111px"
  android:layout_y="206px"
  android:background="@drawable/black"
  android:src="@drawable/star"
  />
  <!--  The establishment of a second. [citation needed]  ImageButton -->
  <ImageButton
  android:id="@+id/pause"
  android:layout_height="wrap_content"
  android:layout_width="wrap_content"
  android:background="@drawable/black"
  android:layout_x="141px"
  android:layout_y="206px"
  android:src="@drawable/pause"
  />
  <!--  The establishment of the first three  ImageButton -->
  <ImageButton
  android:id="@+id/before"
  android:layout_height="wrap_content"
  android:layout_width="wrap_content"
  android:background="@drawable/black"
  android:layout_x="61px"
  android:layout_y="206px"
  android:src="@drawable/before"
  />
  <!--  The establishment of the first four  ImageButton -->
  <ImageButton
  android:id="@+id/next"
  android:layout_height="wrap_content"
  android:layout_width="wrap_content"
  android:background="@drawable/black"
  android:layout_x="181px"
  android:layout_y="206px"
  android:src="@drawable/next"
  />
  <!--  The establishment of the 5 tonal symbols above or below  ImageButton -->
  <ImageButton
  android:id="@+id/myImageButton2"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_x="206px"
  android:layout_y="206px"
  android:background="@drawable/black"
  android:src="@drawable/stop"
  />
  <!--  The establishment of a  ImageView -->
  <ImageView
  android:id="@+id/myImageView1"
  android:layout_width="207px"
  android:layout_height="170px"
  android:layout_x="72px"
  android:layout_y="32px"
  />
  <SeekBar android:id="@+id/seekb"
  android:max="100"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  />
  <ListView android:id="@+id/musicList"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:layout_y="260px"
  />
</AbsoluteLayout>
相关文章
  • Home Android Flash Player 2010-07-20

    Download http://hiapk.com/bbs/viewthread.php?tid=357231&page=1 # pid4680456 android flash player (I made, the original), can play local swf file, there are two installation files: 1.FlashPlayer-release-3 support android 1.5 and 1.6. 2.FlashPlayer-rel

  • Android music player 2010-05-25

    The basis of its own music player in the materials to do some of the extensions, tidy for later use, relatively simple functions, including playlist, play list click function, the progress bar, loop and so on. Like to refer to write more chaotic, emb

  • Android music player - the procedure exit 2010-03-29

    I made a menu option, so that is used to quit running programs from the start I used the finish (), but when music is playing time to quit, then just out of this activity, the background music still continues to play and did not withdraw from the ent

  • Android music player - the program exits 2010-03-17

    I had a menu option to be used to exit the running programs, at first I used a finish (), but the time when the song is being played out, then just out of this activity, the background music still continues to play and did not quit the application, t

  • Android Video Player 2010-07-05

    1. Using the system comes with VideoView play MP4, 3pg video files, etc. <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="verti

  • Android music player - a playlist of the realization of 2010-03-29

    After a morning of time, playlist regarded as a preliminary Gao Ding, and Bug, but there are definitely to be gradually solved, Here is the code I hope everyone has pointed out shortcomings and mistakes. . (I have not used mediaStore multimedia datab

  • Android music player - Playlists realization of 2010-03-16

    After a morning of time, play list initially be buttoned up, but there are definitely Bug to slowly resolve, the following is the code, hope that we identify shortcomings and errors. . (I did not use mediaStore multimedia database, be less than the m

  • Android Training Videos 2010-10-18

    The video from the Innovation Office (www.innovation-office.net) production Official Watch Address: http://www.innovation-office.net/video.htm Contact: QQ: 1203740654 Phone :029 -68,062,269 E-mail: [email protected] Android Application Devel

  • Some examples of learning android 2010-10-29

    Here are ten good packing case, for all to download the study. 1, the Picture Viewer (Picture Viewer) 2, the file browser (File Browser) 3, Address Book (Contact) 4, the Task Manager (Task Manager) 5, music player (Rock Player) 6, the weather (Androi

  • Android learning products 2010-11-08

    Here are ten good packing case, for all to download the study. 1, the Picture Viewer (Picture Viewer) 2, the file browser (File Browser) 3, Address Book (Contact) 4, the Task Manager (Task Manager) 5, music player (Rock Player) 6, the weather (Androi

  • Android multimedia framework OpenCore (PacketVideo) Introduction 2010-11-17

    Overview of the first part of OpenCore OpenCore Another common call is PacketVideo, which is Android's multimedia core. In the process of development, PacketVideo is the name of a company, but this multimedia framework OpenCore is the name of the sof

  • [转]Android的多媒体框架OpenCore(PacketVideo)介绍 2010-12-15

    本文为个人总结,不代表官方观点. 分为几个阶段: 1.整个Android的多媒体框架OpenCore 2.Player和Author的详细介绍 2.OpenCore和Android其他部分的集成 第一部分 OpenCore概述 (hanchao3c Android开发者论坛原创,转载请注明) OpenCore的另外一个常用的称呼是PacketVideo,它是Android的多媒体核心.在防站的过程中,PacketVideo是一家公司的名称,而OpenCore是这套多媒体框架的软件层的名称.在An

  • The use of the OnAudioFocusChangeListener AudioManager 2011-05-20

    Today modify the MP3's a small problem: when in a text message automatically pause music playback, audio playback when the message is finished, or manually view the notice led to the end of message tone, music plays automatically. View source android

  • 自己收集来的 不同手机适配 2013-10-16

    图像显示所需的概念: 像素:一幅图片的显示就是由许多显示着不同颜色的小方格组成的,这样的小方格就被称为像素,是构成图片的最小单位.但是这个像素的具体大小是多少呢?这需要取决于显示这张图片的具体的物理设备显示一个图片像素点的荧光点的大小.图片文件只是记录着它自身有多少个像素点,每个像素点显示什么颜色,至于它自身物理尺寸有多大,它自身也无法得知,例如一张480*800像素的图片在电脑显示器上显示明显要比在手机屏幕上显示大很多,而这张图片本身并没有改变.只是手机的屏比电脑显示器的屏要精细许多,也就是每

  • MP3 player with an application to Android experience in the Service 2010-03-29

    Cell phones are indispensable in people's lives a useful tool. We are using the phone, often multi-threaded, and is suddenly interrupted. For instance, we will side browse the web while listening to MP3. Or is text messaging, when a sudden incoming c

  • MP3 player of small applications (and writing to learn Android 1) 2010-03-29

    "In the information age, the objective obstacles no longer exists, the so-called barriers are subjective. If you want to develop any new technology, you do not need millions of dollars, you only need to put in the refrigerator full of pizza and Coke,

  • android development of music player, the progress of the 2010-03-29

    I just do not learn android long few days ago, made an android version of the music player, did not expect the first problem encountered is the play progress bar, through access to documents, found a solution, my method does not must be the best, jus

  • music player for android development progress bar 2010-03-13

    I just do not learn how long android, android few days ago, made a version of the music player, did not think the first problem encountered is the player's progress bar, through access to documents, to find a solution, my method does not be the best,

  • Own use android to do a simple player 2010-05-15

    Because of the need to participate in software design contest What, precisely selected knowledge of the subject use android, so the unpleasant task learned. Through the study of some of the knowledge, I tried to do a simple player android, although v

  • android MMI (multimedia) interface - music player 2010-07-04

    android MMI (multimedia) interface - music player (Mainly the Activity class with drawing code button and textview other omitted public class mainActivity extends Activity ( / / To play music is to be the object of a media player private MediaPlayer