设置IMediaPlaybackService

时间:2010-09-02 03:11:22

标签: android service playback serviceconnection

我在使用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中没有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

1 个答案:

答案 0 :(得分:0)

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