2012-07-19 149 views
1

當用戶轉到此活動時播放一首歌曲。當他離開這個活動時,一切正常,但是當用戶再次進入這個活動時,一首新歌曲將播放而不會停止較老的歌曲。當用戶再次訪問此活動時,如何停止播放第一首歌曲?無法停止媒體播放器?

公共類MediaPlay1延伸活動{

private SeekBar seekvolume, seekcontrol; 
private TextView txtcrnt, txtremain, txtcrntbig,txttitle; 
private ImageButton btnpre, btnnext, btnplay, btnrepeat; 
private AudioManager audioManager; 
private MediaPlayer mediaPlayer; 
private String kg1; 
private String MixName1; 
private Button btnback, btnplayer; 
String MixReplace; 
private boolean isDownloading = true; 
Handler handler = new Handler(); 
private boolean isRepeat=false; 
private int i = 0; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.media3); 

    mediaPlayer = new MediaPlayer(); 
    initViews(); 

    if(kg1 != null && MixName1 != null) 
    { 
    Intent i1 = getIntent(); 
    kg1 = i1.getStringExtra("Kggg1"); 
    MixName1 = i1.getStringExtra("mixname1"); 
    MixReplace=MixName1.replace(".mp3", " "); 
    }else{ 

    } 

} 
private void initViews() { 

    audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); 
    txtcrnt = (TextView) findViewById(R.id.txtcurnt); 
    txtremain = (TextView) findViewById(R.id.txtremain); 
    txtcrntbig = (TextView) findViewById(R.id.txtcrntbig); 
    txttitle=(TextView) findViewById(R.id.title); 
    btnpre = (ImageButton) findViewById(R.id.btnpre); 
    btnnext = (ImageButton) findViewById(R.id.btnnext); 
    btnplay = (ImageButton) findViewById(R.id.btnplay); 
    btnrepeat=(ImageButton) findViewById(R.id.btnrepeat); 
    btnback = (Button) findViewById(R.id.btnlout); 
    btnplayer = (Button) findViewById(R.id.btnedit); 
    btnback.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      ((MyMixes)getParent()).goBack(); 
     } 
    }); 

    btnplayer.setClickable(true); 
    btnplayer.setEnabled(false); 
    btnplayer.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 

     } 
    }); 

    if(btnrepeat.isPressed()) 
    { 
     mediaPlayer.setLooping(true); 
    } 
    txttitle.setText(MixReplace); 

    seekcontrol=(SeekBar) findViewById(R.id.seekbarcontrol); 

    seekcontrol.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { 

     public void onStopTrackingTouch(SeekBar seekBar) { 
     } 

     public void onStartTrackingTouch(SeekBar seekBar) { 

     } 

     public void onProgressChanged(SeekBar seekBar, int progress, 
       boolean fromUser) { 
      if(fromUser){ 
       mediaPlayer.seekTo(progress); 
       seekcontrol.setProgress(progress); 
      } 

     } 
    }); 

    btnplay.setOnClickListener(new OnClickListener() { 

     public void onClick(View paramView) { 
        btnClick(); 

     } 
    }); 

    new Thread(new Runnable() { 

     @Override 
     public void run() { 
      try { 
       Thread.sleep(3000); 
       btnplay.setClickable(false); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
    btnplay.setClickable(true); 

    btnnext.setOnClickListener(new OnClickListener() { 

     public void onClick(View paramView) { 

     } 
    }); 

    btnpre.setOnClickListener(new OnClickListener() { 

     public void onClick(View paramView) { 



     } 
    }); 

    seekvolume = (SeekBar) findViewById(R.id.seekbarvolume); 

    new Thread() 
    { 
     public void run() 
     { 
      while (isDownloading) 
      { 
       if (Data.filenames != null&& Data.filenames.size() >= (getIntent().getExtras().getInt("index") + 1) && Data.filenames.get(getIntent().getExtras().getInt("index")) != null) 
       { 
        try 
        { 

         String path = Data.filenames.get(getIntent().getExtras().getInt("index")); 
         mediaPlayer.setDataSource(path); 
         mediaPlayer.prepare(); 
         mediaPlayer.start(); 
         startPlayProgressUpdater(); 
         mediaPlayer.setOnCompletionListener(new OnCompletionListener() 
         { 
          @Override 
          public void onCompletion(MediaPlayer mp) 
          { 
           mediaPlayer.seekTo(0); 
          } 
         }); 
         mediaPlayer.setOnErrorListener(new OnErrorListener() { 

          @Override 
          public boolean onError(MediaPlayer mp, int what, int extra) { 
           return false; 
          } 
         }); 
        } catch (IllegalArgumentException e) { 
         e.printStackTrace(); 
        } catch (IllegalStateException e) { 
         e.printStackTrace(); 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } catch (Exception e) { 
         e.printStackTrace(); 
        } finally { 
         isDownloading = false; 
        } 

        if (mediaPlayer != null) 
         seekcontrol.setMax(mediaPlayer.getDuration()); 

        int maxvolume = audioManager 
          .getStreamMaxVolume(AudioManager.STREAM_MUSIC); 
        int curntvolume = audioManager 
          .getStreamVolume(AudioManager.STREAM_MUSIC); 

        seekvolume.setMax(maxvolume); 
        seekvolume.setProgress(curntvolume); 

        seekvolume 
          .setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { 

           public void onStopTrackingTouch(
             SeekBar paramSeekBar) { 
           } 

           public void onStartTrackingTouch(
             SeekBar paramSeekBar) { 
           } 

           public void onProgressChanged(
             SeekBar paramSeekBar, int progress, 
             boolean paramBoolean) { 
            audioManager.setStreamVolume(
              AudioManager.STREAM_MUSIC, 
              progress, 0); 
           } 
          }); 
        seekcontrol.setMax(mediaPlayer.getDuration()); 

        seekcontrol.setOnTouchListener(new OnTouchListener() { 
         public boolean onTouch(View v, 
           MotionEvent paramMotionEvent) { 
          seekChange(v); 
          return false; 
         } 
        }); 
       } 
      } 
     }; 
    }.start(); 
} 

protected void btnClick() { 
    i++; 
    i = i % 2; 
    startSong(i); 
} 

private void startSong(int i) { 
    if (i == 0) { 
     System.out.println("11111" + i); 
     btnplay.setImageResource(R.drawable.pause); 
     try { 
      System.out.println("start try chech------"); 
      mediaPlayer.start(); 
      startPlayProgressUpdater(); 
     } catch (Exception e) { 
      mediaPlayer.pause(); 
     } 
    } 
    if (i == 1) { 
     btnplay.setImageResource(R.drawable.play); 
     mediaPlayer.pause(); 
     System.out.println("00000" + i); 
    } 
} 



private Handler my_handler = new Handler() { 
    public void handleMessage(Message msg) { 
     seekcontrol.setProgress(msg.arg1); 
     super.handleMessage(msg); 
    } 
}; 

public void startPlayProgressUpdater() 
    { 
    Message objmessage = my_handler.obtainMessage(); 

    boolean isPlaying = mediaPlayer.isPlaying(); 

    if (isPlaying) { 

     objmessage.obj = mediaPlayer.getCurrentPosition(); 
     Runnable notification = new Runnable() { 
      public void run() { 
       startPlayProgressUpdater(); 

       int curtimeplay = mediaPlayer.getCurrentPosition(); 
       int curntseconds = (int) (curtimeplay/1000) % 60; 
       int curntminutes = (int) ((curtimeplay/(1000 * 60)) % 60); 
       txtcrnt.setText(curntminutes + ":" + curntseconds); 
       txtcrntbig.setText(curntminutes + ":" + curntseconds); 

       int duratplay = mediaPlayer.getDuration(); 
       int totaldurationseconds = (int) (duratplay/1000) % 60; 
       int totaldurationminutes = (int) ((duratplay/(1000 * 60)) % 60); 
       int remianingtime = duratplay - curtimeplay; 
       int remianingtimeseconds = (int) (remianingtime/1000) % 60; 
       int remianingtimeminutes = (int) ((remianingtime/(1000 * 60)) % 60); 
       txtremain.setText(remianingtimeminutes + ":" + remianingtimeseconds); 
      } 
     }; 
     handler.postDelayed(notification, 1000); 
    } 
    else 
    { 

     mediaPlayer.pause(); 

     btnplay.setImageResource(R.drawable.play); 

    } 

    objmessage.arg1 = mediaPlayer.getCurrentPosition(); 
    my_handler.sendMessage(objmessage); 
} 

protected void seekChange(View v) { 
     if (mediaPlayer.isPlaying()) { 
      SeekBar sb = (SeekBar) v; 
      mediaPlayer.seekTo(sb.getProgress()); 
     } 
    } 
@Override 
public void onBackPressed() { 
    ((MyMixes)getParent()).goBack(); 
} 

}

+0

我承認審查所有的代碼對我來說太長了,但是,根據您的描述,聽起來您需要使用服務而不是將邏輯放入您的活動中。 – 2012-07-19 09:20:02

回答

1

要調用mediaPlayer.start()啓動媒體播放器。你在哪裏打電話mediaPlayer.stop()來阻止它?通過單擊按鈕或根據需要在某個地方呼叫停止,或在該活動的onStop()onPause()方法中呼叫停止。

+0

我想在活動再次開始時停止mediaplayer,並且想要再次啓動mediaplayer。 – bkshukla 2012-07-19 09:23:01

+0

在onPause()調用mediaplayer.stop()和onResume()時再次調用mediaplayer.start()... – pixelscreen 2012-07-19 09:27:44

+0

發生了什麼事?工作? – pixelscreen 2012-07-19 09:59:24