taz*_*ani 15 audio android android-mediaplayer
我是Android新手,我必须创建MediaPlayer播放音频音乐.开始时我的活动歌曲开始播放.但是,当我更改我的模拟器屏幕上的方向时,MediaPlayer将重新初始化并开始播放另一个音频.我怎么能避免呢?
这是我的代码:
public class Audio_Activity extends Activity {
MediaPlayer mp = null;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
if(isLaunched)
{
setContentView(R.layout.audio);
}
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
length = settings.getInt("TheOffset", 0);
init();
prefs = PreferenceManager.getDefaultSharedPreferences(this);
mp = MediaPlayer.create(getApplicationContext(), R.raw.subhanallah);
playMusic();
mp.setOnCompletionListener(new OnCompletionListener()
{
@Override
public void onCompletion(MediaPlayer arg0)
{
// TODO Auto-generated method stub
}
});
}
private void playMusic()
{
httpGetAsynchTask httpGetAsyncTask = new httpGetAsynchTask();
httpGetAsyncTask.execute();
}
class httpGetAsynchTask extends AsyncTask<String,Integer, Void>
{
protected void onPreExdcute()
{
}
@Override
protected Void doInBackground(String... arg0)
{
// TODO Auto-generated method stub
final SharedPreferences.Editor prefsEdit = prefs.edit();
Log.e("Song is playing", "in Mediya Player ");
mp.setLooping(false);
mp.start();
int millisecond=mp.getDuration();
Log.e("Song is playing", "in Mediya Player " + millisecond);
prefsEdit.putBoolean("mediaplaying", true);
prefsEdit.commit();
//btnChapter.setEnabled(false);
return null;
}
protected void onPostExecute(Void result)
{
super.onPostExecute(result);
btnChapter.setEnabled(false);
}
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Configuration config=getResources().getConfiguration();
if(config.orientation == Configuration.ORIENTATION_PORTRAIT)
{
setContentView(R.layout.audio);
}
else if(config.orientation == Configuration.ORIENTATION_LANDSCAPE)
{
setContentView(R.layout.audio);
}
}
@Override
public void onPause() {
super.onPause();
SharedPreferences.Editor prefsEdit = prefs.edit();
boolean isPlaying = prefs.getBoolean("mediaplaying", false);
if (isPlaying)
{
mp.pause();
int position = mp.getCurrentPosition();
Log.e("Current ", "Position -> " + position);
prefsEdit.putInt("mediaPosition", position);
prefsEdit.commit();
}
}
@Override
protected void onResume() {
super.onResume();
mp.start();
boolean isPlaying = prefs.getBoolean("mediaplaying", false);
if (isPlaying) {
int position = prefs.getInt("mediaPosition", 0);
mp.seekTo(position);
// mp.start();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我在Manifest.xml文件中做了一些更改.
< android:configChanges="orientation|screenSize|keyboard" >
Run Code Online (Sandbox Code Playgroud)
并创建layout-land文件夹.
为什么音乐播放两次?
这个问题最简单的答案.
@Override
protected void onSaveInstanceState(Bundle outState)
{
outState.putInt("possition", mpbg.getCurrentPosition());
mpbg.pause();
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
int pos = savedInstanceState.getInt("possition");
mpbg.seekTo(pos);
super.onRestoreInstanceState(savedInstanceState);
}
Run Code Online (Sandbox Code Playgroud)
当我们将mediaplayer声明为静态变量时,只存在一个mp实例.假设我们的活动方向现在改变,一切都将在方向改变时重新创建,但由于静态属性,不会重新创建Mp变量.通过这种方式我们只是创造一个条件
if(mp!=null && !mp.isPlaying()){
mp = MediaPlayer.create(getApplicationContext(), R.raw.subhanallah);
playMusic();
}
Run Code Online (Sandbox Code Playgroud)
在我们的onCreate()方法中,这将检查音乐是否已经播放,如果它正在播放,那么如果条件不允许应用再次启动音乐播放器.如果音乐没有播放,那么条件将允许应用程序重启音乐.
这是您的更新代码:
public class Audio_Activity extends Activity {
static MediaPlayer mp = null;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
if(isLaunched)
{
setContentView(R.layout.audio);
}
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
length = settings.getInt("TheOffset", 0);
init();
prefs = PreferenceManager.getDefaultSharedPreferences(this);
if(mp == null)
{
initializeMP();
}
if(!mp.isPlaying())
{
playMusic();
}
mp.setOnCompletionListener(new OnCompletionListener()
{
@Override
public void onCompletion(MediaPlayer arg0)
{
// TODO Auto-generated method stub
}
});
}
private void playMusic()
{
httpGetAsynchTask httpGetAsyncTask = new httpGetAsynchTask();
httpGetAsyncTask.execute();
}
public void initializeMP()
{
mp = MediaPlayer.create(getApplicationContext(), R.raw.subhanallah);
}
class httpGetAsynchTask extends AsyncTask<String,Integer, Void>
{
protected void onPreExdcute()
{
}
@Override
protected Void doInBackground(String... arg0)
{
// TODO Auto-generated method stub
final SharedPreferences.Editor prefsEdit = prefs.edit();
Log.e("Song is playing", "in Mediya Player ");
if(mp == null)
{
initializeMP()
}
mp.setLooping(false);
mp.start();
int millisecond=mp.getDuration();
Log.e("Song is playing", "in Mediya Player " + millisecond);
prefsEdit.putBoolean("mediaplaying", true);
prefsEdit.commit();
//btnChapter.setEnabled(false);
return null;
}
protected void onPostExecute(Void result)
{
super.onPostExecute(result);
btnChapter.setEnabled(false);
}
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Configuration config=getResources().getConfiguration();
if(config.orientation == Configuration.ORIENTATION_PORTRAIT)
{
setContentView(R.layout.audio);
}
else if(config.orientation == Configuration.ORIENTATION_LANDSCAPE)
{
setContentView(R.layout.audio);
}
}
@Override
public void onPause() {
super.onPause();
SharedPreferences.Editor prefsEdit = prefs.edit();
boolean isPlaying = prefs.getBoolean("mediaplaying", false);
if (isPlaying)
{
if(mp!=null)
{
mp.pause();
}
int position = mp.getCurrentPosition();
Log.e("Current ", "Position -> " + position);
prefsEdit.putInt("mediaPosition", position);
prefsEdit.commit();
}
}
@Override
protected void onResume() {
super.onResume();
if(mp == null)
{
initializeMP();
}
mp.start();
boolean isPlaying = prefs.getBoolean("mediaplaying", false);
if (isPlaying) {
int position = prefs.getInt("mediaPosition", 0);
mp.seekTo(position);
// mp.start();
}
}
}
Run Code Online (Sandbox Code Playgroud)