我在Android的IMediaPlaybackService上遇到问题。有人告诉我,我需要做的就是将其放入其包中(在本例中为com.android.music),但是这样做时,我无法构建我的项目。 IMediaPlaybackService的代码如下:

/* //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();
}


但是,似乎有一些问题使我无法使用此文件构建项目:


“ in”关键字使Eclipse给出错误。确切的错误是:Syntax error on token "long", delete this token.我不确定为什么那里有“ in”关键字。它们似乎不是Java中的关键字,它们是什么?
发生的另一个错误是The type com.android.music.IMediaPlaybackService is not visible,可以通过将接口设置为公共来解决。
当我执行以下代码行时,最后的错误是Stub cannot be resolved or is not a field.this.mService = IMediaPlaybackService.Stub.asInterface(service);因为IMediaPlaybackService中没有存根。


其他人似乎已经能够轻松克服这些问题,那么我想念的是什么?我可能没有看到过一些简单的事情,但是我为此苦苦挣扎的时间比我应该做的长。我需要弄乱我的项目设置吗?

因此,我已经能够构建自己的项目,但是现在我在服务本身方面遇到了问题。这是我的MediaPlayerServiceConnection类的代码:

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!");
    }
}


...这是我活动中绑定服务的代码,但是未调用onServiceConnected方法,我是否错误地尝试使用该服务?

//  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

最佳答案

我自己尝试绑定到媒体播放器服务。我只是在比尝试使用Android所需的时间短的时间内实现了自己的播放器服务...

09-27 05:57