这是service部分,整个程序运行后可以播放音乐,歌词同步更新,但当我按暂停按钮时,调用了handler.removeCallbacks(lrcThread);
通过测试,线程还在继续运行,歌词也还在更新,不知道哪出问题,求解答
package com.cwx.mp3.service; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Queue; import com.cwx.lrc.LrcProcessor;
import com.cwx.model.Mp3Info;
import com.cwx.mp3.*; import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Environment;
import android.os.Handler;
import android.os.IBinder; public class Mp3PlayerService extends Service {
private int MSG;
private boolean isPlaying=false;
private boolean isPause=false;
private boolean isRelease=true;
private MediaPlayer mediaPlayer;
private String SDPath;
private Uri uri;
private Mp3Info mp3Info;
private Handler handler;
private long begin;
private long offest;
private long pausemills;
private long nextmills;
private StringBuffer message;
private LrcThread lrcThread;
private boolean flag=true;
public int onStartCommand(Intent intent, int flags, int startId) {
SDPath=Environment.getExternalStorageDirectory().getAbsolutePath();
MSG=intent.getIntExtra("MSG", 0);
handler=new Handler();
switch(MSG){
case AppConstant.PLAY:
mp3Info=(Mp3Info) intent.getSerializableExtra("mp3Info");
play();
break;
case AppConstant.PAUSE:
pause();
break;
case AppConstant.STOP:
stop();
break;
default:
break;
}
return super.onStartCommand(intent, flags, startId);
} public class LrcThread implements Runnable{ private ArrayList<Queue> queues;
private Queue messages;
private Queue times;
public LrcThread(ArrayList<Queue> queues){
this.queues=queues;
this.messages=queues.get(0);
this.times=queues.get(1);
}
public void run() {
offest=System.currentTimeMillis()-begin;
if(flag){
nextmills=(Long) times.poll();
message=(StringBuffer) messages.poll();
flag=false;
}
if(offest>=nextmills){
Intent intent=new Intent();
intent.putExtra("lrcMessage", message+"");
intent.setAction(AppConstant.LRC_MESSAGE_ACTION);
sendBroadcast(intent);
System.out.println("OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO");
if(times.size()!=0)nextmills=(Long) times.poll();
if(messages.size()!=0)message=(StringBuffer) messages.poll();
}
handler.postDelayed(lrcThread,10);
}
}
public IBinder onBind(Intent intent) {
return null;
}
public void play(){
if(!isPlaying){
try {
prepareLrc();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
begin=System.currentTimeMillis();
handler.postDelayed(lrcThread, 5);
uri=Uri.parse("file://"+SDPath+"/"+"mp3/"+mp3Info.getMp3Name());
mediaPlayer=MediaPlayer.create(this, uri);
mediaPlayer.setLooping(false);
mediaPlayer.start();
isPlaying=true;
isRelease=false;
}
}
public void pause(){
if(mediaPlayer!=null){
if(!isPause){
handler.removeCallbacks(lrcThread);
System.out.println("111111111111111111111111111111111111");
pausemills=System.currentTimeMillis();
mediaPlayer.pause();
isPause=true;
}
else{
mediaPlayer.start();
begin=System.currentTimeMillis()-pausemills+begin;
handler.postDelayed(lrcThread, 5);
isPause=false;
}
}
}
public void stop(){
if(mediaPlayer!=null){
if(isPlaying&&!isPause){
mediaPlayer.stop();
handler.removeCallbacks(lrcThread);
mediaPlayer.release();
isPlaying=false;
isRelease=true;
}
}
}
public void prepareLrc() throws FileNotFoundException{
ArrayList<Queue> queues=null;
LrcProcessor lrcProcessor=new LrcProcessor();
System.out.println(mp3Info);
InputStream inputStream=new FileInputStream(Environment.getExternalStorageDirectory()
.getAbsoluteFile()+File.separator+"mp3"+File.separator+mp3Info.getLrcName());
try {
queues=lrcProcessor.process(inputStream);
System.out.println(queues.get(0));
lrcThread=new LrcThread(queues);
begin=0;
nextmills=0;
offest=0;
} catch (IOException e) {
e.printStackTrace();
}
} }
通过测试,线程还在继续运行,歌词也还在更新,不知道哪出问题,求解答
package com.cwx.mp3.service; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Queue; import com.cwx.lrc.LrcProcessor;
import com.cwx.model.Mp3Info;
import com.cwx.mp3.*; import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Environment;
import android.os.Handler;
import android.os.IBinder; public class Mp3PlayerService extends Service {
private int MSG;
private boolean isPlaying=false;
private boolean isPause=false;
private boolean isRelease=true;
private MediaPlayer mediaPlayer;
private String SDPath;
private Uri uri;
private Mp3Info mp3Info;
private Handler handler;
private long begin;
private long offest;
private long pausemills;
private long nextmills;
private StringBuffer message;
private LrcThread lrcThread;
private boolean flag=true;
public int onStartCommand(Intent intent, int flags, int startId) {
SDPath=Environment.getExternalStorageDirectory().getAbsolutePath();
MSG=intent.getIntExtra("MSG", 0);
handler=new Handler();
switch(MSG){
case AppConstant.PLAY:
mp3Info=(Mp3Info) intent.getSerializableExtra("mp3Info");
play();
break;
case AppConstant.PAUSE:
pause();
break;
case AppConstant.STOP:
stop();
break;
default:
break;
}
return super.onStartCommand(intent, flags, startId);
} public class LrcThread implements Runnable{ private ArrayList<Queue> queues;
private Queue messages;
private Queue times;
public LrcThread(ArrayList<Queue> queues){
this.queues=queues;
this.messages=queues.get(0);
this.times=queues.get(1);
}
public void run() {
offest=System.currentTimeMillis()-begin;
if(flag){
nextmills=(Long) times.poll();
message=(StringBuffer) messages.poll();
flag=false;
}
if(offest>=nextmills){
Intent intent=new Intent();
intent.putExtra("lrcMessage", message+"");
intent.setAction(AppConstant.LRC_MESSAGE_ACTION);
sendBroadcast(intent);
System.out.println("OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO");
if(times.size()!=0)nextmills=(Long) times.poll();
if(messages.size()!=0)message=(StringBuffer) messages.poll();
}
handler.postDelayed(lrcThread,10);
}
}
public IBinder onBind(Intent intent) {
return null;
}
public void play(){
if(!isPlaying){
try {
prepareLrc();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
begin=System.currentTimeMillis();
handler.postDelayed(lrcThread, 5);
uri=Uri.parse("file://"+SDPath+"/"+"mp3/"+mp3Info.getMp3Name());
mediaPlayer=MediaPlayer.create(this, uri);
mediaPlayer.setLooping(false);
mediaPlayer.start();
isPlaying=true;
isRelease=false;
}
}
public void pause(){
if(mediaPlayer!=null){
if(!isPause){
handler.removeCallbacks(lrcThread);
System.out.println("111111111111111111111111111111111111");
pausemills=System.currentTimeMillis();
mediaPlayer.pause();
isPause=true;
}
else{
mediaPlayer.start();
begin=System.currentTimeMillis()-pausemills+begin;
handler.postDelayed(lrcThread, 5);
isPause=false;
}
}
}
public void stop(){
if(mediaPlayer!=null){
if(isPlaying&&!isPause){
mediaPlayer.stop();
handler.removeCallbacks(lrcThread);
mediaPlayer.release();
isPlaying=false;
isRelease=true;
}
}
}
public void prepareLrc() throws FileNotFoundException{
ArrayList<Queue> queues=null;
LrcProcessor lrcProcessor=new LrcProcessor();
System.out.println(mp3Info);
InputStream inputStream=new FileInputStream(Environment.getExternalStorageDirectory()
.getAbsoluteFile()+File.separator+"mp3"+File.separator+mp3Info.getLrcName());
try {
queues=lrcProcessor.process(inputStream);
System.out.println(queues.get(0));
lrcThread=new LrcThread(queues);
begin=0;
nextmills=0;
offest=0;
} catch (IOException e) {
e.printStackTrace();
}
} }
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货