我的申请表有问题。 它是一个媒体录音机播放器。 播放器正在工作,但问题是,当我播放歌曲或录音机时 我关闭应用程序(退出),音乐不会停止播放。 你能帮我解决一下我的问题吗? 谢谢。
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_player);
albumart=(ImageView)findViewById(R.id.albumArt);
name=(TextView)findViewById(R.id.songName);
artist=(TextView)findViewById(R.id.artistName);
play=(ImageButton)findViewById(R.id.playButton);
prev=(ImageButton)findViewById(R.id.previousButton);
next=(ImageButton)findViewById(R.id.nextButton);
playlist=(ImageButton)findViewById(R.id.playlistButton);
sb=(SeekBar)findViewById(R.id.seekBar);
if(mPlayer!=null){
if(su!=null){
su.endthread();
su.interrupt();
su=null;
}
mPlayer.stop();
mPlayer.release();
}
fs=new FetchSongs();
if(fs.getfetchstatus()!=true){
mySongs = fs.findSongs(Environment.getExternalStorageDirectory());
}
else{
mySongs=fs.getsonglist();
mPlayer.release();
}
Intent intent=getIntent();
position=intent.getIntExtra("pos",0);
path=mySongs.get(position).toString();
u=Uri.parse(path);
setsongdata(path,position);
mPlayer=MediaPlayer.create(getApplicationContext(),u);
mPlayer.start();
mPlayer.setLooping(true);
play.setImageResource(R.drawable.icone);
sb.setMax(mPlayer.getDuration());
su=new seekUpdater(true);
su.start();
sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch (SeekBar seekBar) {
mPlayer.stop();
mPlayer.release();
}
});
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(mPlayer.isPlaying()){
mPlayer.pause();
play.setImageResource(R.drawable.icone);
}
else {
mPlayer.start();
play.setImageResource(R.drawable.icone);
}
}
});
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(su!=null){
su.endthread();
su.interrupt();
su=null;
}
mPlayer.stop();
mPlayer.release();
position=(position+1)%mySongs.size();
path=mySongs.get(position).toString();
setsongdata(path,position);
u=Uri.parse(path);
mPlayer=MediaPlayer.create(getApplicationContext(),u);
mPlayer.start();
mPlayer.setLooping(true);
play.setImageResource(R.drawable.icone);
sb.setMax(mPlayer.getDuration());
sb.setProgress(mPlayer.getCurrentPosition());
su=new seekUpdater(true);
su.start();
}
});
prev.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(su!=null){
su.endthread();
su.interrupt();
su=null;
}
mPlayer.stop();
mPlayer.release();
position=(position-1<0)? mySongs.size()-1:position-1;
path=mySongs.get(position).toString();
setsongdata(path,position);
u=Uri.parse(path);
mPlayer=MediaPlayer.create(getApplicationContext(),u);
mPlayer.start();
mPlayer.setLooping(true);
play.setImageResource(R.drawable.icone);
sb.setMax(mPlayer.getDuration());
sb.setProgress(mPlayer.getCurrentPosition());
su=new seekUpdater(true);
su.start();
}
});
playlist.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(getApplicationContext(),MainActivity.class));
}
});
}
class seekUpdater extends Thread{
private boolean running;
seekUpdater(boolean status){
running=status;
}
public void endthread(){
running=false;
}
@Override
public void run() {
try {
while (running==true) {
int dur = mPlayer.getDuration();
int cur = mPlayer.getCurrentPosition();
while (cur < dur) {
sleep(500);
cur = mPlayer.getCurrentPosition();
sb.setProgress(cur);
}
}
}
catch (InterruptedException e){
e.printStackTrace();
running=false;
}
//super.run();
}
}
public void setsongdata(String songpath, int pos){
mmr=new MediaMetadataRetriever();
mmr.setDataSource(songpath);
try {
songName=mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE);
art = mmr.getEmbeddedPicture();
songArt = BitmapFactory.decodeByteArray(art, 0, art.length);
artistName = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST);
artist.setText(artistName);
name.setText(songName);
albumart.setImageBitmap(songArt);
}
catch (Exception e){
albumart.setImageResource(R.drawable.albumart);
artist.setText("Unknown Artist");
name.setText(mySongs.get(pos).getName().toString().replace(".3gp",""));
}
}
}
答案 0 :(得分:1)
不知道你在做什么,但这应该有用
在onPause
mPlayer.stop();
像
@Override
public void onPause() {
super.onPause();
mPlayer.stop();
}
每当你的活动失去焦点时就会调用onPause。