How to run Music Service in different process

Xiky

I write a Streaming Music app, I work ok until I'm try to run the music Service in the different process. following the service of Android Developer, I've added the process of Service in the Manifest like here:

<service android:name=".MyPlayer"
            android:process=":myPlayer"
            android:enabled="true"/>

Then in the MainActivity I call the start Service:

if(_playIntent == null) {
            _playIntent = new Intent(getApplicationContext(), MyPlayer.class);
            startService(_playIntent);
            bindService(_playIntent, musicConnection, Context.BIND_AUTO_CREATE);
        }

The bindService will call to ServiceConnection:

MyPlayer _player;
private ServiceConnection musicConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            MyPlayer.MusicBinder binder = (MyPlayer.MusicBinder)service; 
            _player = binder.getService();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            isConnection = false;
        }
    };

Then the app stop working after this line:

MyPlayer.MusicBinder binder = (MyPlayer.MusicBinder)service;

The app show the message "Unfortunately, Mx Dance Music has stopped", Stack trace: "FATAL EXCEPTION: main java.lang.ClassCastException: android.os.BinderProxy cannot be cast to com.xiKy.mxMusic.MyPlayer$MusicBinder at com.xiKy.mxMusic.MainActivity$3.onServiceConnected(MainActivity.java:356) at android.app.LoadedApk$ServiceDispatcher.doConnected(LoadedApk.java:1097) at android.app.LoadedApk$ServiceDispatcher$RunConnection.run(LoadedApk.java:1114) at android.os.Handler.handleCallback(Handler.java:615) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4823) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556) at dalvik.system.NativeStart.main(Native Method) ClassLoader.loadClass:

The class loader returned by Thread.getContextClassLoader() may fail for processes that host multiple applications. You should explicitly specify a context class loader. For example: Thread.setContextClassLoader(getClass().getClassLoader());

And then if I remove the line:

bindService(_playIntent, musicConnection, Context.BIND_AUTO_CREATE);

The application isn't crash. How to fix this bug ? I'm appreciate any help from everybody.

CommonsWare
MyPlayer.MusicBinder binder = (MyPlayer.MusicBinder)service;

This works for local bound services only. Once you switch to a remote service:

  • You need to define your API in AIDL
  • Your service needs to implement its Binder as a subclass of the .Stub class generated from that AIDL
  • The client needs to use asInterface() to convert the IBinder into a client-side proxy generated from that AIDL

All of this is covered in the documentation on AIDL.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to run a function in new process?

From Dev

What are the Advantages and Disadvantages of running a service in different process?

From Dev

How to pause different music players in android?

From Dev

Android how to run a service in a different thread

From Dev

How is Process.Start different from Start > Run?

From Dev

make service to run on different times a day

From Dev

How to stop music in my service when youtube app resumes video?

From Dev

How to run process on multiple records?

From Dev

WCF service method creates a Process to run MsTest, the process will not execute

From Dev

How to kill process (created by run-process)

From Dev

How to detect if a process is installing as a service

From Dev

Trying to process different scenarios in Service

From Dev

Android: How to control music service play/pause from bluetooth device?

From Dev

Run delegate in different process

From Dev

How to run a Service Fabric application with different security permissions?

From Dev

How do I prepare my music for syncing with a cloud service?

From Dev

How to Communicate between Activity and Service using LocalBroadcastManager in a different Process

From Dev

Run py.test test in different process

From Dev

How to run a program as a service

From Dev

How do I run a process that wants to become root from a systemd service which is a regular user?

From Dev

How do I prepare my music for syncing with a cloud service?

From Dev

make service to run on different times a day

From Dev

How to stop music in my service when youtube app resumes video?

From Dev

How to kill a service process?

From Dev

How to process selections from different providers via the Eclipse Selection Service while maintaining loose coupling?

From Dev

How to run service not as root

From Dev

How to make a program/process a service ?

From Dev

How to use Launchd to run applescript when a music file is opened?

From Dev

Wavesurfer How to speed up the process of playing music

Related Related

  1. 1

    How to run a function in new process?

  2. 2

    What are the Advantages and Disadvantages of running a service in different process?

  3. 3

    How to pause different music players in android?

  4. 4

    Android how to run a service in a different thread

  5. 5

    How is Process.Start different from Start > Run?

  6. 6

    make service to run on different times a day

  7. 7

    How to stop music in my service when youtube app resumes video?

  8. 8

    How to run process on multiple records?

  9. 9

    WCF service method creates a Process to run MsTest, the process will not execute

  10. 10

    How to kill process (created by run-process)

  11. 11

    How to detect if a process is installing as a service

  12. 12

    Trying to process different scenarios in Service

  13. 13

    Android: How to control music service play/pause from bluetooth device?

  14. 14

    Run delegate in different process

  15. 15

    How to run a Service Fabric application with different security permissions?

  16. 16

    How do I prepare my music for syncing with a cloud service?

  17. 17

    How to Communicate between Activity and Service using LocalBroadcastManager in a different Process

  18. 18

    Run py.test test in different process

  19. 19

    How to run a program as a service

  20. 20

    How do I run a process that wants to become root from a systemd service which is a regular user?

  21. 21

    How do I prepare my music for syncing with a cloud service?

  22. 22

    make service to run on different times a day

  23. 23

    How to stop music in my service when youtube app resumes video?

  24. 24

    How to kill a service process?

  25. 25

    How to process selections from different providers via the Eclipse Selection Service while maintaining loose coupling?

  26. 26

    How to run service not as root

  27. 27

    How to make a program/process a service ?

  28. 28

    How to use Launchd to run applescript when a music file is opened?

  29. 29

    Wavesurfer How to speed up the process of playing music

HotTag

Archive