2

我在使用IMediaPlaybackServiceAndroid 时遇到问题。有人告诉我,我需要做的就是将它放入它的包中(在本例中为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 中没有 Stub。

其他人似乎已经能够轻松克服这些问题,那么我错过了什么?这可能是我没有看到的简单事情,但我一直在努力解决这个问题的时间比我应该做的要长。我需要弄乱我的项目设置吗?

所以我已经能够构建我的项目,但现在我遇到了服务本身的问题。这是我的 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
4

1 回答 1

0

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

于 2010-12-10T17:59:32.463 回答