android簡單音樂播放器

前兩天寫了一個在activity上運行的音樂播放器,發現不能再後臺播放音樂,所以就學習了一下寫了一個基於service的播放器,雖然代碼同樣看上去亂糟糟的。但一般功能都實現了,下面是代碼。

MusicService.jave

修改了一下代碼,所有操作通過廣播來實現

public class MusicService extends Service {

private static String TAG = "ln";
private IBinder iBinder = new myBinder();
private MusicInfo musicInfo;
public MediaPlayer mediaPlayer;
private int id = 0;
private int curplaytime = 0;
private int mPlaySta;
private int mRandomSta = 1;
private int mLoopSta = 0;
private int mCount;
private long musictime = 0;
private String title = null;
BroadcastReceiver mBrodcast = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        String action = intent.getAction();
        if (AppConstant.PlayMsg.RANDOMON_MSG.equals(action)) {
            mRandomSta = 1;
        } else if (AppConstant.PlayMsg.RANDOMOFF_MSG.equals(action)) {
            mRandomSta = 0;
            if (mLoopSta == 1) {
                mLoopSta = 2;
            }
        }else if (AppConstant.PlayMsg.LOOP_MSG.equals(action)) {
            mLoopSta = 1;
            if (mRandomSta == 0) {
                mRandomSta = 1;
            }
        } else if (AppConstant.PlayMsg.SINGLESLOOP_MSG.equals(action)) {
            mLoopSta = 2;
        } else if (AppConstant.PlayMsg.LOOPOFF_MSG.equals(action)) {
            mLoopSta = 0;
        }else if (AppConstant.PlayMsg.NEXT_MSG.equals(action)) {
            next();
        }else if(AppConstant.PlayMsg.PRE_MSG.equals(action)){
            previous();
        }else if (AppConstant.PlayMsg.PAUSE_MSG.equals(action)) {
            if (mediaPlayer != null && mediaPlayer.isPlaying()) {
                mediaPlayer.pause();
            }
        }else if (AppConstant.PlayMsg.PLAY_MSG.equals(action)) {
            if (curplaytime != 0) {
                musicplay(id);
                mediaPlayer.seekTo(curplaytime);
            } else {
                musicplay(id);
            }
        }else if(AppConstant.PlayMsg.PLAYPAUSE_MSG.equals(action)){
            if (mediaPlayer.isPlaying()) {
                mediaPlayer.pause();
            }else{
                if (curplaytime != 0) {
                    musicplay(id);
                    mediaPlayer.seekTo(curplaytime);
                } else {
                    musicplay(id);
                }
            }
        }
    }
};
Handler handler = new Handler() {};
Runnable runnable = new Runnable() {
    @Override
    public void run() {
        if (mediaPlayer != null) {
            if (mediaPlayer.isPlaying()) {
                mPlaySta = 0;
            } else {
                mPlaySta = 1;
            }
        }
        title = musicInfo.getTitle();
        if (mediaPlayer != null && title != null) {
            curplaytime = mediaPlayer.getCurrentPosition();
            musictime = musicInfo.getTime();
            id = musicInfo.getId();
            autoPlay();
        }
        handler.postDelayed(this, 500);
    }
};

public void onCreate() {
    Log.i(TAG, "MusicService onCreate()");
    musicInfo = new MusicInfo();
    mediaPlayer = new MediaPlayer();
    mCount = MusicList.getMusicInfos(this).size();
    handler.postDelayed(runnable, 500);

    IntentFilter filter = new IntentFilter();
    filter.addAction(AppConstant.PlayMsg.RANDOMON_MSG);
    filter.addAction(AppConstant.PlayMsg.RANDOMOFF_MSG);
    filter.addAction(AppConstant.PlayMsg.LOOP_MSG);
    filter.addAction(AppConstant.PlayMsg.LOOPOFF_MSG);
    filter.addAction(AppConstant.PlayMsg.SINGLESLOOP_MSG);
    filter.addAction(AppConstant.PlayMsg.NEXT_MSG);
    filter.addAction(AppConstant.PlayMsg.PRE_MSG);
    filter.addAction(AppConstant.PlayMsg.PLAY_MSG);
    filter.addAction(AppConstant.PlayMsg.PAUSE_MSG);
    filter.addAction(AppConstant.PlayMsg.PLAYPAUSE_MSG);
    registerReceiver(mBrodcast, filter);
    super.onCreate();
}

public int onStartCommand(Intent intent, int flags, int startId) {
    Log.i(TAG, "MusicService onStartCommand()");
    return super.onStartCommand(intent, flags, startId);
}

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return new myBinder();
}

public class myBinder extends Binder {
    public MusicService getService() {
        return MusicService.this;
    }
}

public void next() {
    if (mRandomSta == 1) {
        if (id >= 0 && id < mCount - 1) {
            id = id + 1;
            musicplay(id);
        } else {
            musicplay(0);
        }
    } else {
        id = getRandomNumber();
        musicplay(id);
    }
}

public void previous() {
    if (mRandomSta == 1) {
        if (id > 0 && id < mCount) {
            id = id - 1;
            musicplay(id);
        } else if (id == 0) {
            musicplay(mCount - 1);
        }
    } else {
        id = getRandomNumber();
        musicplay(id);
    }
}

public void musicplay(int id) {
    try {
        musicInfo = MusicList.getMusicInfos(getApplicationContext()).get(id);
    } catch (Exception e) {
        e.printStackTrace();
    }
    String url = musicInfo.getUrl();
    if (url != null) {
        try {
            mediaPlayer.reset();
            mediaPlayer.setDataSource(url);
            mediaPlayer.prepare();
            mediaPlayer.start();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else {
        Toast.makeText(this, "沒有播放的音樂", Toast.LENGTH_SHORT).show();
    }
}

public int getRandomNumber() {
    Random random = new Random();
    int location = random.nextInt(mCount);
    return location;
}

public void autoPlay() {
    mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            setAutoPlay();
        }
    });
}
private void setAutoPlay(){
    if (mRandomSta == 0) {
        id = getRandomNumber();
        musicplay(id);
    } else if (mLoopSta == 0) {
            id = id + 1;
            if (id <= mCount - 1) {
                Log.i(TAG, "loop all list : " +  id);
                musicplay(id);
            } else {
                id = 0;
                musicplay(0);
        }
    } else if (mLoopSta == 1) {
        musicplay(id);
    } else if (mLoopSta == 2) {
        id = id + 1;
        if (id <= mCount - 1) {
            musicplay(id);
        } else {
            Toast.makeText(this,getResources().getString(R.string.music_list_end),Toast.LENGTH_SHORT).show();
            mediaPlayer.stop();
        }
    }
}
/**
 * 返回實時播放時間
 * @return 
 */
public int getCurPlaytime() {
    return curplaytime;
}

/**
 * 返回單曲總時間 long musictime
 * @return 
 */
public long getTime() {
    return musictime;
}

/**
 * 返回 單曲標題 String
 * @return 
 */
public String getTitle() {
    return title;
}

/**
 * 返回單曲id int
 * @return 
 */
public int getId() {
    return id;
}
 /**
  * Uers 拖動播放進度條
  * @param progress
  */
public void setUserTime(int progress) {
    mediaPlayer.seekTo(progress);
}

/**
 * 返回播放狀態 :0- 播放,1- 暫停
 * @return
 */
public int getPlaySta() {
    return mPlaySta;
}

/**
 * 返回隨機狀態 :0-隨機開,1- 隨機關
 * @return
 */
public int getRandomSta() {
    return mRandomSta;
}

/**
 * 返回循環狀態 : 0-全部循環, 1-單曲循環, 2-關閉循環
 * @return
 */
public int getLoopSta() {
    return mLoopSta; 
}

public void onDestroy(){
    super.onDestroy();
    unregisterReceiver(mBrodcast); //解除廣播註冊
}
}

MainActivity.java

public class MainActivity extends Activity implements OnClickListener,
    AppConstant {

private String TAG = "ln";
private MusicInfo musicInfo;
private Button mButton, mPre, mPlay, mNext, mRandom, mLoop;
private SeekBar mSeekBar;
private int mRandomNum;
private TextView mName, mCurtime, mAllTime, mCount;
private int musicCount;
private String title = null;
private int curloop = 0;
private MusicService mService = null;
private boolean isPlay = false;
private Intent serviceIntent;
private int playId;
public static final String RANDOMON_MSG = "com.music.RandomON";
public static final String RANDOMOFF_MSG = "com.music.RandomOFF";

Handler handler = new Handler() {
};
Runnable runnable = new Runnable() {

    @Override
    public void run() {
        title = mService.getTitle();
        if (title != null) {
            mName.setText(title);
            mAllTime.setText(MusicAdapater.Format(mService.getTime()));
            int time = new Long(mService.getTime()).intValue();
            mSeekBar.setMax(time);
            mCurtime.setText(MusicAdapater.Format(mService.getCurPlaytime()));
            mSeekBar.setProgress(mService.getCurPlaytime());
            playId = mService.getId() + 1;
            mCount.setText(playId + "/" + musicCount);
            int imsg = mService.getPlaySta();
            if (imsg == 0) {
                mPlay.setText(getResources().getString(R.string.pause));
                isPlay = true;
            }else if (imsg == 1) {
                mPlay.setText(getResources().getString(R.string.play));
                isPlay = false;
            }
            mRandomNum = mService.getRandomSta();
            if (mRandomNum == 0) {
                mRandom.setText(getResources().getString(R.string.randomon));
            }else if (mRandomNum == 1) {
                    mRandom.setText(getResources().getString(R.string.randomoff));
            }
            curloop = mService.getLoopSta();
            if (curloop == 0) {
                mLoop.setText(getResources().getString(R.string.loopon));
            }else if(curloop == 1){
                mLoop.setText(getResources().getString(R.string.singlesloop));
            }else if(curloop == 2){
                mLoop.setText(getResources().getString(R.string.loopoff));
            }
        }else {
            mName.setText(getResources().getString(R.string.unknow));
        }
        handler.postDelayed(this, 500);
    }
};

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.i(TAG, "MainActivity onCreate()");
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    initView();
    bindService();
}

//綁定service
public ServiceConnection conn = new ServiceConnection() {

    @Override
    public void onServiceDisconnected(ComponentName name) {
        // TODO Auto-generated method stub
        mService = null;
    }

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        // TODO Auto-generated method stub
        mService = ((MusicService.myBinder) service).getService();
    }
};
private void bindService() {
    bindService(serviceIntent, conn, Context.BIND_AUTO_CREATE);     
}
private void initView() {
    setContentView(R.layout.main_activity);
    serviceIntent = new Intent(MainActivity.this, MusicService.class);
    musicInfo = new MusicInfo();
    mName = (TextView) findViewById(R.id.name);
    mButton = (Button) findViewById(R.id.btn_list);
    mPre = (Button) findViewById(R.id.pre);
    mPlay = (Button) findViewById(R.id.play);
    mNext = (Button) findViewById(R.id.next);
    mRandom = (Button) findViewById(R.id.btn_random);
    mLoop = (Button) findViewById(R.id.loop);
    mSeekBar = (SeekBar) findViewById(R.id.time_seekbar);
    mSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
        }
        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
        }
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) {
            if (fromUser) {
                if (title != null) {
                    mService.setUserTime(progress);
                }
            }
        }
    });
    mCurtime = (TextView) findViewById(R.id.tv_curtime);
    mAllTime = (TextView) findViewById(R.id.tv_alltime);
    mCount = (TextView) findViewById(R.id.music_count);
    mAllTime.setText("00:00");
    mCurtime.setText("00:00");
    mCount.setText("0/0");
    mButton.setOnClickListener(this);
    mNext.setOnClickListener(this);
    mPre.setOnClickListener(this);
    mPlay.setOnClickListener(this);
    mRandom.setOnClickListener(this);
    mLoop.setOnClickListener(this);
    musicCount = MusicList.getMusicInfos(this).size();
    handler.postDelayed(runnable, 500);
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    Intent intent2 =new Intent();
    switch (v.getId()) {
    case R.id.btn_list:
        Intent intent = new Intent(MainActivity.this, MusicList.class);
        startActivity(intent);
        break;
    case R.id.pre:
        intent2.setAction(AppConstant.PlayMsg.PRE_MSG);
        sendBroadcast(intent2);
        break;
    case R.id.play:
        if (isPlay == false) {
            intent2.setAction(AppConstant.PlayMsg.PLAY_MSG);
            sendBroadcast(intent2);
            isPlay = true;
        } else if (isPlay == true) {
            intent2.setAction(AppConstant.PlayMsg.PAUSE_MSG);
            sendBroadcast(intent2);
            isPlay = false;
        }
        break;
    case R.id.next:
        intent2.setAction(AppConstant.PlayMsg.NEXT_MSG);
        sendBroadcast(intent2);
        break;
    case R.id.btn_random:
        if (mRandomNum == 0) {
            intent2.setAction(AppConstant.PlayMsg.RANDOMON_MSG);
            sendBroadcast(intent2);
        }else if (mRandomNum == 1){
            intent2.setAction(AppConstant.PlayMsg.RANDOMOFF_MSG);
            sendBroadcast(intent2);
        }
        break;
    case R.id.loop:
        if (curloop == 0) {
            intent2.setAction(AppConstant.PlayMsg.LOOP_MSG);
            sendBroadcast(intent2);
        }else if (curloop == 1) {
            intent2.setAction(AppConstant.PlayMsg.SINGLESLOOP_MSG);
            sendBroadcast(intent2);
        }else if (curloop == 2) {
            intent2.setAction(AppConstant.PlayMsg.LOOPOFF_MSG);
            sendBroadcast(intent2);
        }
        break;
    default:
        break;
    }
}
protected void onRestart() {
    super.onRestart();
}
public void onDestroy() {
    super.onDestroy();
}
}

AppConstant.java

public interface AppConstant {

public class PlayMsg {

    public static final String PLAY_MSG = "com.music.PLAY";// 開始播放
    public static final String PAUSE_MSG = "com.music.PAUSE";// 暫停播放
    public static final String PLAYPAUSE_MSG = "com.music.PLAYPAUSE";

    public static final String PRE_MSG = "com.music.PREVIOUS"; //上一首
    public static final String NEXT_MSG = "com.music.NEXT"; //下一首

    public static final String LOOP_MSG = "com.music.LOOPON"; //全部循環
    public static final String SINGLESLOOP_MSG = "com.music.SINGLESLOOP"; //單曲循環
    public static final String LOOPOFF_MSG = "com.music.LOOPOFF"; //關閉循環

    public static final String RANDOMON_MSG = "com.music.RandomON";//隨機開
    public static final String RANDOMOFF_MSG = "com.music.RandomOFF";//隨機關

}
}

MusicAdapater.java

public class MusicAdapater extends BaseAdapter {

private Context mContext;
private List<MusicInfo> musicInfos;
private MusicInfo musicInfo;

int mPosition;

public MusicAdapater(Context context,List<MusicInfo> musicInfos){
    this.mContext = context;
    this.musicInfos = musicInfos;
}

public int getPosition(){
    return mPosition;
}

public void setPosition(int mPosition){
    this.mPosition = mPosition;
}
@Override
public int getCount() {
    // 決定listview有多少個item
    return musicInfos.size();
}
@Override
public Object getItem(int position) {
    return position;
}
@Override
public long getItemId(int position) {
    return position;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View view;
    ViewHolder holder;
    if (convertView == null) {
        holder = new ViewHolder();
        convertView = LayoutInflater.from(mContext).inflate(R.layout.music_title, null);
        holder.title = (TextView)convertView.findViewById(R.id.music_title);
        holder.singer = (TextView)convertView.findViewById(R.id.music_singer);
        holder.time = (TextView)convertView.findViewById(R.id.music_time);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder)convertView.getTag();
    }
    musicInfo =musicInfos.get(position);
    holder.title.setText(musicInfo.getTitle());
    holder.singer.setText(musicInfo.getSinger());
    holder.time.setText(Format(musicInfo.getTime()));
    return convertView;
}

class ViewHolder {
    public TextView title; //音樂名
    public TextView singer; // 歌手名
    public TextView time; //時間
}
/**
 * 時間轉化 把音樂時間的long型數據轉化爲“分:秒”
 * @param time
 * @return String hms
 */
public static String Format(long time) {
    SimpleDateFormat format = new SimpleDateFormat("mm:ss");
    String hms = format.format(time);
    return hms;
}
}

MusicList.java

public class MusicList extends Activity implements OnClickListener{

private static final String TAG = "ln";
private ListView mListView;
MusicInfo musicInfo;
MusicAdapater adapater;
private MusicService mService;
private Intent serviceIntent;
private Button mReturn, mPreviou, mPlayPause, mNext;
private TextView mTitle;
Handler handler = new Handler(){};
Runnable runnable = new Runnable() {
String title = null;
    @Override
    public void run() {
        // TODO Auto-generated method stub
        title = mService.getTitle();
        String textView = (String) mTitle.getText();
        if (title != null) {
            if (!textView.equals(title)) {
                mTitle.setText(title);
            }
            int imsg = mService.getPlaySta();
            if (imsg == 0) {
                mPlayPause.setText(getResources().getString(R.string.pause));
            }else if (imsg == 1) {
                mPlayPause.setText(getResources().getString(R.string.play));
            }
        }
        handler.postDelayed(this, 500);
    }
};

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.music_list_activity);
    Log.i("ln", "MusicList onCreate()");
    initView(this);
    bindService();
    handler.postDelayed(runnable, 500);
}

void initView(Context context) {
    mListView = (ListView) findViewById(R.id.listview);
    mListView.setOnItemClickListener(new MusicItemCLickListener());
    musicInfo = new MusicInfo();
    adapater = new MusicAdapater(context, getMusicInfos(context));
    mListView.setAdapter(adapater);
    mService = new MusicService();

    mReturn = (Button) findViewById(R.id.btn_return);
    mPreviou = (Button) findViewById(R.id.btn_pre);
    mPlayPause = (Button) findViewById(R.id.btn_playpause);
    mNext = (Button) findViewById(R.id.btn_next);
    mTitle = (TextView) findViewById(R.id.tv_title);
    mReturn.setOnClickListener(this);
    mPreviou.setOnClickListener(this);
    mPlayPause.setOnClickListener(this);
    mNext.setOnClickListener(this);
    mTitle.setText(getResources().getString(R.string.unknow));

}

public static List<MusicInfo> getMusicInfos(Context context) {
    Cursor mCursor = context.getContentResolver().query(
            MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,
            MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
    List<MusicInfo> musicInfos = new ArrayList<MusicInfo>();
        for (int i = 0; i < mCursor.getCount(); i++) {
            MusicInfo musicInfo = new MusicInfo();
            mCursor.moveToNext();
            int id = i; // 這是自定義音樂id,方便之後使用,有規律
            // long id =
            // mCursor.getLong(mCursor.getColumnIndex(MediaStore.Audio.Media._ID));
            // // 音樂id,好像無規律
            String title = mCursor.getString((mCursor
                    .getColumnIndex(MediaStore.Audio.Media.TITLE)));// 音樂標題
            String artist = mCursor.getString(mCursor
                    .getColumnIndex(MediaStore.Audio.Media.ARTIST));// 藝術家
            long duration = mCursor.getLong(mCursor
                    .getColumnIndex(MediaStore.Audio.Media.DURATION));// 時長
            int isMusic = mCursor.getInt(mCursor
                    .getColumnIndex(MediaStore.Audio.Media.IS_MUSIC));// 是否爲音樂
            String url = mCursor.getString(mCursor
                    .getColumnIndex(MediaStore.Audio.Media.DATA)); // 路徑
            if (isMusic != 0) {
                musicInfo.setId(id);
                musicInfo.setTitle(title);
                musicInfo.setSinger(artist);
                musicInfo.setTime(duration);
                musicInfo.setUrl(url);
                musicInfos.add(musicInfo);
            }
        }
    return musicInfos;
}

/*
 * 這是listview item 的點擊事件 點擊後調用service的musicplay()方法 通過傳入的id來播放音樂
 */
private class MusicItemCLickListener implements OnItemClickListener {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        // TODO Auto-generated method stub
        if (getMusicInfos(getApplicationContext()) != null) {
            MusicInfo musicInfo = getMusicInfos(getApplicationContext()).get(position);
            int id2 = musicInfo.getId();
            mService.musicplay(id2);
        }
    }
}
private void bindService(){
    serviceIntent = new Intent(MusicList.this,MusicService.class);
    bindService(serviceIntent,connection,Context.BIND_AUTO_CREATE);
}

public ServiceConnection connection = new ServiceConnection() {

    @Override
    public void onServiceDisconnected(ComponentName name) {
        // TODO Auto-generated method stub
        mService = null;
    }

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        // TODO Auto-generated method stub
        mService = ((MusicService.myBinder) service).getService();
    }
};

protected void onRestart(){
    super.onRestart();
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    Intent intent = new Intent();
    switch (v.getId()) {
    case R.id.btn_pre:
        intent.setAction(AppConstant.PlayMsg.PRE_MSG);
        sendBroadcast(intent);
        break;
    case R.id.btn_playpause:
        intent.setAction(AppConstant.PlayMsg.PLAYPAUSE_MSG);
        sendBroadcast(intent);
        break;
    case R.id.btn_next:
        intent.setAction(AppConstant.PlayMsg.NEXT_MSG);
        sendBroadcast(intent);
        break;
    case R.id.btn_return:
        Intent intent2 = new Intent(MusicList.this,MainActivity.class);
        startActivity(intent2);
        break;
    default:
        break;
    }
}
}

MusicInfo.java

public class MusicInfo {

private int id; // 歌曲ID 
private String title; // 歌曲名稱 
private String singer; // 歌手名稱 
private long time;
private String url;
public MusicInfo(){}
public MusicInfo(int id, String title, String singer, long time, String url){
    super();
    this.id=id;
    this.title = title;
    this.singer = singer;
    this.time = time;
    this.url = url;
}
public String getUrl(){
    return url;
}
public void setUrl(String url) {
    this.url = url;
}
public int getId() {
    return id;
}
public void setId(int ld){
    this.id=ld;
}
public String getTitle(){
    return title;
}
public void setTitle(String title){
    this.title = title;
}
public String getSinger(){
    return singer;
}
public void setSinger(String singer){
    this.singer = singer; 
}
public Long getTime(){
    return time;
}
public void setTime(long time){
    this.time = time;
}
}

三個佈局

## music_title.xml ##

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/music_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="24sp" />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/music_singer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="24sp" />

    <TextView
        android:id="@+id/music_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="24sp"
        android:layout_marginLeft="20dp" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="20dp" >
</LinearLayout>
</LinearLayout>

music_list_activity.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical" >

        <Button
            android:id="@+id/btn_return"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:text="@string/tv_return"
            android:textSize="24sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical" >

        <Button
            android:id="@+id/btn_reset"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:layout_marginRight="20dp"
            android:text="@string/reset"
            android:textSize="24sp" />
    </LinearLayout>
</LinearLayout>

<ListView
    android:id="@+id/listview"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="4"
    android:divider="#000"
    android:paddingLeft="20dp"
    android:paddingRight="20dp" >
</ListView>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:gravity="center"
        android:orientation="vertical" >

        <com.example.musicmode.MarqueeTextView
            android:id="@+id/tv_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:ellipsize="marquee"
            android:singleLine="true"
            android:textSize="20sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical" >
        <Button 
            android:id="@+id/btn_pre"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="24sp"
            android:text="@string/pre"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical" >
        <Button 
            android:id="@+id/btn_playpause"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="24sp"
            android:text="@string/play"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical" >
        <Button 
            android:id="@+id/btn_next"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="24sp"
            android:text="@string/next"/>
    </LinearLayout>
</LinearLayout>
</LinearLayout>

main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:textColor="@android:color/black"
    android:textSize="24sp" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center" >

    <TextView
        android:id="@+id/tv_curtime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/black"
        android:textSize="24sp" />

    <SeekBar
        android:id="@+id/time_seekbar"
        android:layout_width="600dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:maxHeight="10dp"
        android:minWidth="10dp" />

    <TextView
        android:id="@+id/tv_alltime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/black"
        android:textSize="24sp" />
</LinearLayout>

<TextView
    android:id="@+id/music_count"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:textColor="@android:color/black"
    android:textSize="24sp" />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginTop="30dp"
    android:gravity="center"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/btn_list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/list"
        android:textSize="32sp" />

    <Button
        android:id="@+id/pre"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/pre"
        android:textSize="32sp" />

    <Button
        android:id="@+id/play"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/play"
        android:textSize="32sp" />

    <Button
        android:id="@+id/next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/next"
        android:textSize="32sp" />

    <Button
        android:id="@+id/btn_random"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/randomoff"
        android:textSize="32sp" />

    <Button 
        android:id="@+id/loop"  
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/loopon"
        android:textSize="32sp" />
</LinearLayout>
</LinearLayout>

AndroidManifest.xml

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:launchMode="singleTask"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity 
        android:name=".MusicList"
        android:launchMode="singleInstance"
        android:label="@string/music_list" >
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <service android:name=".MusicService" android:exported="true"></service>
</application>

Strings

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">我的音樂</string>
<string name="music_list">音樂列表</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="unknow">unknow</string>
<string name="list">列表</string>
<string name="pre">上一曲</string>
<string name="play">播放</string>
<string name="pause">暫停</string>
<string name="next">下一曲</string>
<string name="randomon">隨機開</string>
<string name="randomoff">隨機關</string>
<string name="singlesloop">單曲循環</string>
<string name="loopoff">關閉循環</string>
<string name="loopon">全部循環</string>
<string name="music_list_end">列表播放結束</string>
<string name="tv_return">返回</string>
<string name="reset">刷新</string>
</resources>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章