0

I am new to android and I am now doing an exercise which the application has already stored a audio file(mp4) in /res/raw/ folder and this file can be referenced in android Service class as R.raw.audiofile. In the Service class I have created three methods

onCreate();
onStartCommand();
onDestroy();

in onCreate() I have created a MediaPlayer and in onStartCommand() I have started the MediaPlayer

mp.start() and 
returned STRT_STICKY

to play it and in onDestroy() I have done this:

mp.stop();

in the xml layout I have created a Button with this attribute:

android:onClick="onClickStart"

which calls a method in the MainActivity and this action now shoud playback the audio. however, I am now stuck here on how to link this to the music file so that this button should start the audio. can anyone please give me some idea?

so I have one Main activity class and one (My)Service class (extends to Servcie) and one xml file for layout to perform this action.

4

2 回答 2

1

You need a OnclickListener for your Button like

button.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
                 // Perform action on click
             }

Look here for the MediaPlayer

于 2013-09-17T11:15:38.917 回答
1

You can communicate between the Activity and Service using an Intent or by binding to the Service and sending a Message. In this case, binding to the Service and sending a Message in the Button's click listener is probably the cleanest approach. That also gives you the opportunity to appropriately update UI by passing a Messenger/Handler in the replyTo field of the Message. I'll edit with code snippets when I get to a PC.

Edit:

Most of the code you need is in the Android docs here.

The basic idea is that you need to create a Handler class inside your Service. The proper way to do this (to avoid leaking the Handler and the accompanying Lint warning) is as follows:

private static class MyHandler extends Handler {
    private WeakReference<MyService> mService;

    public MyHandler(MyService service) {
        mService = service;
    }

    @Override
    public void handleMessage(Message msg) {
        // Your message handling here...
        // You can use the members/methods of the Service with
        //   mService.get().____
    }
}

Then you would create a Messenger member and override the onBind method in the Service, like so:

private Messenger mMessenger = new Messenger(new MyHandler(this));

@Override
public IBinder onBind(Intent intent) {
    return mMessenger.getBinder();
}

Then you need to call bindService with an appropriate Intent and ServiceConnection from your Activity. See the link for a code example. In the onServiceConnected method of the ServiceConnection you can stash a Messenger for the Service with:

@Override
public void onServiceConnected(ComponentName className, IBinder service) {
    mMessenger = new Messenger(service);
}

You can likewise create a Handler and Messenger in the Activity and supply it as the replyTo field of any Message objects sent to the Service. In that way, you can tell the Activity whether the MediaPlayer started successfully or not and update UI as appropriate.

于 2013-09-18T00:28:20.893 回答