我有一个活动,我通过意图调用服务类并将uri传递给服务类,在onstartcommand()的服务类中,我获取uri并创建mediaplayer对象并使用mp.start()播放歌曲。在另一个片段类中,我使用intent并获取相同的uri,哪个活动传递给服务类,并生成mp对象,使用此对象,我能够获得getduration(),但我无法getCurrentposition()在服务类
中播放的mediaplayer对象MusicService.java
public class MusicService extends Service implements MediaPlayerControl { public MediaPlayer mp=new MediaPlayer(); @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Override public int onStartCommand(Intent intent, int flags,int startId) { // TODO Auto-generated method stub try{ Bundle extras=intent.getExtras(); if(extras!=null) { if(mp.isPlaying()) { String f=extras.getString("musicsrv"); Uri uri=Uri.parse(f); mp.stop(); mp.release(); mp=MediaPlayer.create(getApplicationContext(),uri); mp.start(); } else { String f=extras.getString("musicsrv"); Uri uri=Uri.parse(f); mp=MediaPlayer.create(getApplicationContext(),uri); mp.start(); } } } catch(Exception e0) {} return super.onStartCommand(intent, flags, startId); } @Override public boolean canPause() { // TODO Auto-generated method stub return false; }}
Fragment.java
public class Fraggy extends Fragment {
ImageView iv;
MusicService m;
TextView endpos,startpos;
SeekBar seekbar;
public MediaPlayer mp1;
private double startTime=0;
private double endTime=0;
private Handler myhandler=new Handler();;
private int forwardTime=5000;
private int backwardTime=5000;
int oneTimeOnly;
int c;
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onViewCreated(view, savedInstanceState);
Toast.makeText(getActivity(), "hello",Toast.LENGTH_LONG).show();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View myview=inflater.inflate(R.layout.activity_mainlayout,container,false);
endpos=(TextView) myview.findViewById(R.id.textView2);
startpos=(TextView) myview.findViewById(R.id.textView1);
seekbar=(SeekBar) myview.findViewById(R.id.seekBar1);
Bundle extras1=getActivity().getIntent().getExtras();
if(extras1!=null)
{
String d=extras1.getString("uri");
Uri uri=Uri.parse(d);
try {
mp1=MediaPlayer.create(getActivity(), uri);
startTime=mp1.getCurrentPosition();// Here i m not able to get the current position
if(oneTimeOnly==0)
{
seekbar.setMax((int) endTime);
oneTimeOnly=1;
}
c=mp1.getDuration();//This works fine
endpos.setText(String.format("%d min,%d sec",TimeUnit.MILLISECONDS.toMinutes((long)c),TimeUnit.MILLISECONDS.toSeconds((long)c)-TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long)c))));
startpos.setText(String.format("%d min,%d sec",TimeUnit.MILLISECONDS.toMinutes((long)startTime),TimeUnit.MILLISECONDS.toSeconds((long)startTime)-TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long)startTime))));
seekbar.setProgress((int)startTime);
myhandler.postDelayed(upDateSong,100);
} catch (IllegalArgumentException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SecurityException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IllegalStateException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
TextView tv=(TextView) myview.findViewById(R.id.textView3);
Bundle extras=getActivity().getIntent().getExtras();
if(extras!=null)
{
String d=extras.getString("name");
tv.setText(d);
}
}
return myview;
}
private Runnable upDateSong=new Runnable()
{
public void run()
{
startTime=mp1.getCurrentPosition();
startpos.setText(String.format("%d min,%d sec",TimeUnit.MILLISECONDS.toMinutes((long)startTime),TimeUnit.MILLISECONDS.toSeconds((long)startTime)-TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long)startTime))));
seekbar.setProgress((int)startTime);
myhandler.postDelayed(upDateSong,100);
}
}};