Android构建音频播放器教程(四)
11.更新AndroidManifest.xml文件
添加android:configChanges=”keyboardHidden|orientation”,
AndroidManifest.xml
<?xml version="1.0"encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidhive.musicplayer"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
<activity
android:name=".AndroidBuildingMusicPlayerActivity"
android:label="@string/app_name"
android:configChanges="keyboardHidden|orientation">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name=".PlayListActivity"/>
</application>
</manifest>
<!-- AndroidBuildingMusicPlayerActivity -->
12.最后的代码
下面是AndroidBuildingMusicPlayerActivity类的完整代码
AndroidBuildingMusicPlayerActivity.java packagecom.androidhive.musicplayer;
importjava.io.IOException;
importjava.util.ArrayList;
importjava.util.HashMap;
importjava.util.Random;
importandroid.app.Activity;
importandroid.content.Intent;
importandroid.media.MediaPlayer;
importandroid.media.MediaPlayer.OnCompletionListener;
importandroid.os.Bundle;
importandroid.os.Handler;
importandroid.view.View;
importandroid.widget.ImageButton;
importandroid.widget.SeekBar;
importandroid.widget.TextView;
importandroid.widget.Toast;
publicclassAndroidBuildingMusicPlayerActivityextendsActivityimplementsOnCompletionListener, SeekBar.OnSeekBarChangeListener {
privateImageButton btnPlay;
privateImageButton btnForward;
privateImageButton btnBackward;
privateImageButton btnNext;
privateImageButton btnPrevious;
privateImageButton btnPlaylist;
privateImageButton btnRepeat;
privateImageButton btnShuffle;
privateSeekBar songProgressBar;
privateTextView songTitleLabel;
privateTextView songCurrentDurationLabel;
privateTextView songTotalDurationLabel;
// Media Player
private MediaPlayer mp;
// Handler to update UI timer, progress bar etc,.
privateHandler mHandler = newHandler();;
privateSongsManager songManager;
privateUtilities utils;
privateintseekForwardTime =5000;// 5000 milliseconds
privateintseekBackwardTime =5000;// 5000 milliseconds
privateintcurrentSongIndex =0;
privatebooleanisShuffle =false;
privatebooleanisRepeat =false;
privateArrayList<HashMap<String, String>> songsList = newArrayList<HashMap<String, String>>();
@Override
publicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.player);
// All player buttons
btnPlay = (ImageButton) findViewById(R.id.btnPlay);
btnForward = (ImageButton) findViewById(R.id.btnForward);
btnBackward = (ImageButton) findViewById(R.id.btnBackward);
btnNext = (ImageButton) findViewById(R.id.btnNext);
btnPrevious = (ImageButton) findViewById(R.id.btnPrevious);
btnPlaylist = (ImageButton) findViewById(R.id.btnPlaylist);
btnRepeat = (ImageButton) findViewById(R.id.btnRepeat);
btnShuffle = (ImageButton) findViewById(R.id.btnShuffle);
songProgressBar = (SeekBar) findViewById(R.id.songProgressBar);
songTitleLabel = (TextView) findViewById(R.id.songTitle);
songCurrentDurationLabel = (TextView) findViewById(R.id.songCurrentDurationLabel);
songTotalDurationLabel = (TextView) findViewById(R.id.songTotalDurationLabel);
// Mediaplayer
mp =newMediaPlayer();
songManager =newSongsManager();
utils =newUtilities();
// Listeners
songProgressBar.setOnSeekBarChangeListener(this);// Important
mp.setOnCompletionListener(this);// Important
// Getting all songs list
songsList = songManager.getPlayList();
// By default play first song
playSong(0);
/**
* Play button click event
* plays a song and changes button to pause image
* pauses a song and changes button to play image
* */
btnPlay.setOnClickListener(newView.OnClickListener() {
@Override
publicvoidonClick(View arg0) {
// check for already playing
if(mp.isPlaying()){
&n
补充:移动开发 , Android ,