I'm having trouble with the IMediaPlaybackService
for Android. I'm told that all I need to do is place it into its package (in this case, com.android.music
), however I can't get my project to build when I do this. The code for IMediaPlaybackService is as follows:
/* //device/samples/SampleCode/src/com/android/samples/app/RemoteServiceInterface.java
**
** Copyright 2007, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
package com.android.music;
interface IMediaPlaybackService
{
void openFile(String path, boolean oneShot);
void openFileAsync(String path);
void open(in long [] list, int position);
int getQueuePosition();
boolean isPlaying();
void stop();
void pause();
void play();
void prev();
void next();
long duration();
long position();
long seek(long pos);
String getTrackName();
String getAlbumName();
long getAlbumId();
String getArtistName();
long getArtistId();
void enqueue(in long [] list, int action);
long [] getQueue();
void moveQueueItem(int from, int to);
void setQueuePosition(int index);
String getPath();
long getAudioId();
void setShuffleMode(int shufflemode);
int getShuffleMode();
int removeTracks(int first, int last);
int removeTrack(long id);
void setRepeatMode(int repeatmode);
int getRepeatMode();
int getMediaMountedCount();
}
However there appear to be several problems that prevent me from building my project with this file:
- The "in" keywords are causing Eclipse to give an error. The exact error is:
Syntax error on token "long", delete this token.
I'm not sure why there are "in" keywords there. They don't appear to be keywords in Java, what are they? - Another error that occurs is
The type com.android.music.IMediaPlaybackService is not visible
, which is remedied by making the interface public. - The final error is
Stub cannot be resolved or is not a field.
when I execute this line of code:this.mService = IMediaPlaybackService.Stub.asInterface(service);
because there is not a Stub in IMediaPlaybackService.
Others seem to have been able to overcome these problems with ease, so what am I missing? It's probably something simple that I'm not seeing, but I've been wrestling with this for longer than I should have. Do I need to mess with my project setup?
So I've been able to build my project, but now I'm having issues with the service itself. Here is the code fo开发者_StackOverflow社区r my MediaPlayerServiceConnection class:
import android.content.ComponentName;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import com.android.music.IMediaPlaybackService;
public class MediaPlayerServiceConnection implements ServiceConnection {
// Tag for debugging.
private static final String TAG = "MediaPlayerServiceConnection";
// The service.
private IMediaPlaybackService mService;
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i(TAG, "MediaPlaybackService is connected! Name: " + name.getClassName());
// This is the important line.
this.mService = IMediaPlaybackService.Stub.asInterface(service);
// If all went well, then we can use the interface.
try {
Log.i(TAG, "Current track: " + this.mService.getTrackName());
Log.i(TAG, "Artist: " + this.mService.getArtistName());
} catch(RemoteException e) { e.printStackTrace(); }
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.i(TAG, "MediaPlaybackService disconnected!");
}
}
...and here is the code in my activity that binds the service, but the onServiceConnected method is not being called, am I incorrectly trying to use the service?
// TESTING MEDIAPLAYBACK SERVICE
Intent intent = new Intent();
intent.setClassName("com.android.music", "com.android.music.MediaPlaybackService");
MediaPlayerServiceConnection conn = new MediaPlayerServiceConnection();
this.bindService(intent, conn, Context.BIND_AUTO_CREATE);
// END TEST
I tried binding to the media player service myself....never was able to. I simply implemented my own player service in less time than it took to try and use Androids...
精彩评论