我正在尝试更新服务中的音乐播放器 SeekBar,但没有任何反应。该服务在我的 Activity 中正常启动并正确绑定(bind)(因为我可以在我的服务中使用某些方法并且音乐播放正常)。这是我的服务代码:
public class MusicPlayerService extends Service {
MediaPlayer myMusicPlayer;
String path;
Intent getMusicId = new Intent();
// Binder given to clients
private final IBinder mBinder = new LocalBinder();
/**
* Class used for the client Binder. Because we know this service always
* runs in the same process as its clients, we don't need to deal with IPC.
*/
public class LocalBinder extends Binder {
MusicPlayerService getService() {
// Return this instance of LocalService so clients can call public methods
return MusicPlayerService.this;
}
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
myMusicPlayer = new MediaPlayer();
Log.d(getClass().getSimpleName(),"onCreate()");
super.onCreate();
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
Log.d(getClass().getSimpleName(), "onStartCommand()");
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent intent) {
path = intent.getStringExtra("musicID");
Log.e("Music path on SD received from intent", path);
try {
myMusicPlayer.setDataSource(path);
myMusicPlayer.setLooping(true);
myMusicPlayer.prepare();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
myMusicPlayer.start();
Log.d(getClass().getSimpleName(), "onBind()");
return mBinder;
}
/** method for clients */
public int getMusicDuration() {
return myMusicPlayer.getDuration();
}
public int getMusicCurPos(){
return myMusicPlayer.getCurrentPosition();
}
public void pauseMusic(){
myMusicPlayer.pause();
Log.d(getClass().getSimpleName(), "pauseMusic()");
}
public void playMusic(){
myMusicPlayer.start();
Log.d(getClass().getSimpleName(), "start()");
}
public void stopMusic(){
myMusicPlayer.stop();
Log.d(getClass().getSimpleName(), "stop()");
}
public void seekToPos (int pos){
myMusicPlayer.seekTo(pos);
}
}
有趣的是,在将服务绑定(bind)到我的 Activity 并运行它之后,服务的一些方法返回空值,如 getDuration(),其中一些方法可以找到,如 onStart() 和 onDestroy()。
提前致谢。
更新:
在您的 Activity 中,您必须检查服务是否绑定(bind),因为这需要一段时间才能完成,然后使用它提供的公共(public)方法,例如:
//Bound Music service and pass the music path
Intent musicIntent = new Intent(this, MusicPlayerService.class);
musicIntent.putExtra("musicID", path); //Put Extra data for music player to play the exact file
bindService(musicIntent, mConnection, Context.BIND_AUTO_CREATE); //Bound the Music player service
//Music Handler for methods
musicMethodsHandler = new Handler();
musicRun = new Runnable() {
@Override
public void run() {
if (mBound == true){ // Check if service bounded
if (musicTotTime == null){ // Put data in it one time
musicTotTime = myMusicPlayer.getMusicDuration();
Log.v("Music Duration of Player in thread", musicTotTime.toString());
seekBar.setMax(musicTotTime);
}
musicCurTime = myMusicPlayer.getMusicCurPos();
Log.v("Music Duration of Player in thread", musicCurTime.toString());
seekBar.setProgress(musicCurTime);
}else if(mBound == false){ // if service is not bounded log it
Log.v("Still waiting to bound", Boolean.toString(mBound));
}
musicMethodsHandler.postDelayed(this, 1000);
}
};
musicMethodsHandler.postDelayed(musicRun, 1000);
/** Defines callbacks for service binding, passed to bindService() */
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className,
IBinder service) {
// We've bound to LocalService, cast the IBinder and get LocalService instance
LocalBinder binder = (LocalBinder) service;
myMusicPlayer = binder.getService();
mBound = true;
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
mBound = false;
}
};
请记住,服务的 onCreate() 方法会先运行一次。
最佳答案
您不应该直接从您的 Service 类更新 UI,这不是它的责任。
在您的Activity 中,您可以运行一个CountDownTimer 或类似的工具来定期轮询您的Service 以处理SeekBar 更新.
如果您需要从您的服务向您的 Activity 发送消息(例如轨道结束时等),您可以使用 LocalBroadcastManager
一些例子:
关于android - 更新服务中的 MusicPlayer SeekBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17247715/
我正在尝试使用ruby和Savon来使用网络服务。测试服务为http://www.webservicex.net/WS/WSDetails.aspx?WSID=9&CATID=2require'rubygems'require'savon'client=Savon::Client.new"http://www.webservicex.net/stockquote.asmx?WSDL"client.get_quotedo|soap|soap.body={:symbol=>"AAPL"}end返回SOAP异常。检查soap信封,在我看来soap请求没有正确的命名空间。任何人都可以建议我
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
我想安装一个带有一些身份验证的私有(private)Rubygem服务器。我希望能够使用公共(public)Ubuntu服务器托管内部gem。我读到了http://docs.rubygems.org/read/chapter/18.但是那个没有身份验证-如我所见。然后我读到了https://github.com/cwninja/geminabox.但是当我使用基本身份验证(他们在他们的Wiki中有)时,它会提示从我的服务器获取源。所以。如何制作带有身份验证的私有(private)Rubygem服务器?这是不可能的吗?谢谢。编辑:Geminabox问题。我尝试“捆绑”以安装新的gem..
我将应用程序升级到Rails4,一切正常。我可以登录并转到我的编辑页面。也更新了观点。使用标准View时,用户会更新。但是当我添加例如字段:name时,它不会在表单中更新。使用devise3.1.1和gem'protected_attributes'我需要在设备或数据库上运行某种更新命令吗?我也搜索过这个地方,找到了许多不同的解决方案,但没有一个会更新我的用户字段。我没有添加任何自定义字段。 最佳答案 如果您想允许额外的参数,您可以在ApplicationController中使用beforefilter,因为Rails4将参数
我尝试使用不同的ssh_options在同一阶段运行capistranov.3任务。我的production.rb说:set:stage,:productionset:user,'deploy'set:ssh_options,{user:'deploy'}通过此配置,capistrano与用户deploy连接,这对于其余的任务是正确的。但是我需要将它连接到服务器中配置良好的an_other_user以完成一项特定任务。然后我的食谱说:...taskswithoriginaluser...task:my_task_with_an_other_userdoset:user,'an_othe
最近,当我启动我的Rails服务器时,我收到了一长串警告。虽然它不影响我的应用程序,但我想知道如何解决这些警告。我的估计是imagemagick以某种方式被调用了两次?当我在警告前后检查我的git日志时。我想知道如何解决这个问题。-bcrypt-ruby(3.1.2)-better_errors(1.0.1)+bcrypt(3.1.7)+bcrypt-ruby(3.1.5)-bcrypt(>=3.1.3)+better_errors(1.1.0)bcrypt和imagemagick有关系吗?/Users/rbchris/.rbenv/versions/2.0.0-p247/lib/ru
在Rails4.0.2中,我使用s3_direct_upload和aws-sdkgems直接为s3存储桶上传文件。在开发环境中它工作正常,但在生产环境中它会抛出如下错误,ActionView::Template::Error(noimplicitconversionofnilintoString)在View中,create_cv_url,:id=>"s3_uploader",:key=>"cv_uploads/{unique_id}/${filename}",:key_starts_with=>"cv_uploads/",:callback_param=>"cv[direct_uplo
我想在Ruby中创建一个用于开发目的的极其简单的Web服务器(不,不想使用现成的解决方案)。代码如下:#!/usr/bin/rubyrequire'socket'server=TCPServer.new('127.0.0.1',8080)whileconnection=server.acceptheaders=[]length=0whileline=connection.getsheaders想法是从命令行运行这个脚本,提供另一个脚本,它将在其标准输入上获取请求,并在其标准输出上返回完整的响应。到目前为止一切顺利,但事实证明这真的很脆弱,因为它在第二个请求上中断并出现错误:/usr/b
您如何在Rails中的实时服务器上进行有效调试,无论是在测试版/生产服务器上?我试过直接在服务器上修改文件,然后重启应用,但是修改好像没有生效,或者需要很长时间(缓存?)我也试过在本地做“脚本/服务器生产”,但是那很慢另一种选择是编码和部署,但效率很低。有人对他们如何有效地做到这一点有任何见解吗? 最佳答案 我会回答你的问题,即使我不同意这种热修补服务器代码的方式:)首先,你真的确定你已经重启了服务器吗?您可以通过跟踪日志文件来检查它。您更改的代码显示的View可能会被缓存。缓存页面位于tmp/cache文件夹下。您可以尝试手动删除
最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路