我一直在研究媒体播放器并尝试集成搜索栏,我使用Bind服务来编码MediaPlayer函数,并使用该绑定服务的引用在我的Activity中调用这些函数,我使用了Handler来协助runnable,但是,我的seekBar无法跟踪播放媒体的进度。
跟踪SeekBar的代码如下: -
Runnable runnable=new Runnable() {
@Override
public void run() {
seekUpdate();
}
};
public void seekUpdate(){
seekBar.setProgress(service.currentPosition());
handler.postDelayed(runnable,1000);
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
service.seekTo(progress);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
我的Bind服务类中的代码(已集成MediaPlayer功能的地方)如下: -
package com.daman.mediaplayer;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.IntDef;
import java.io.IOException;
public class BindMusicService extends Service {
String songToPlay,songPath;
MediaPlayer mediaPlayer;
IBinder ibRef = new MyBinder();
PlayerActivity play;
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent,int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
mediaPlayer = new MediaPlayer();
songToPlay = intent.getStringExtra("keySong");
songPath=intent.getStringExtra("songpath");
try {
mediaPlayer.setDataSource(songPath);
//mediaPlayer.setDataSource(this, Uri.parse(url));
} catch (IOException e) {
e.printStackTrace();
}
try {
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
return ibRef;
}
@Override
public boolean onUnbind(Intent intent) {
return super.onUnbind(intent);
}
public void playMusic(){
mediaPlayer.start();
}
public void pauseMusic(){
mediaPlayer.pause();
}
public void stopMusic(){
mediaPlayer.stop();
mediaPlayer.release();
}
public int currentPosition(){
return mediaPlayer.getCurrentPosition();
}
public int getDuration(){
return mediaPlayer.getDuration();
}
public void seekTo(int position){
mediaPlayer.seekTo(position);
}
class MyBinder extends Binder{
BindMusicService getServiceReference(){
return BindMusicService.this;
}
}
}
当我播放并暂停媒体时,android监视器中弹出的错误是: -
04-03 21:08:28.873 23092-23092/? E/Icon: Unable to load resource 0x00000000 from pkg=
android.content.res.Resources$NotFoundException: Resource ID #0x0
at android.content.res.Resources.getValue(Resources.java:1359)
at android.content.res.Resources.getDrawable(Resources.java:812)
at android.graphics.drawable.Icon.loadDrawableInner(Icon.java:313)
at android.graphics.drawable.Icon.loadDrawable(Icon.java:269)
at android.widget.RemoteViews$TextViewDrawableAction.apply(RemoteViews.java:1502)
at android.widget.RemoteViews.performApply(RemoteViews.java:2805)
at android.widget.RemoteViews.apply(RemoteViews.java:2764)
at android.widget.RemoteViews$ViewGroupAction.apply(RemoteViews.java:1373)
at android.widget.RemoteViews.performApply(RemoteViews.java:2805)
at android.widget.RemoteViews.reapply(RemoteViews.java:2795)
at com.android.systemui.statusbar.BaseStatusBar.updateNotificationViews(BaseStatusBar.java:2175)
at com.android.systemui.statusbar.BaseStatusBar.updateNotification(BaseStatusBar.java:2044)
at com.android.systemui.statusbar.BaseStatusBar$6$2.run(BaseStatusBar.java:513)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:168)
at android.app.ActivityThread.main(ActivityThread.java:5885)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)