1
我在爲Android安裝IMediaPlaybackService
時遇到問題。我被告知,我需要做的就是將其放入包裝中(在這種情況下,com.android.music
),但是當我這樣做時,我無法讓我的項目生成。對於IMediaPlaybackService的代碼如下:設置適用於Android的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();
}
但是似乎有是阻止我建設我的項目,這個文件的幾個問題:
- 的「在」關鍵字導致Eclipse來給出錯誤。確切的錯誤是:
Syntax error on token "long", delete this token.
我不知道爲什麼有「在」關鍵字那裏。它們似乎不是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中沒有存根。
其他人似乎已經能夠輕鬆克服這些問題,所以我錯過了什麼?這可能很簡單,我沒有看到,但我一直在摔跤比這更久。我需要弄亂我的項目設置嗎?
在此先感謝!
問候,celestialorb。
編輯:
所以我已經能夠建立我的項目,但現在我在與服務本身的問題。下面是我的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
類似的代碼適合我。但是,只有在服務運行時纔可以連接到服務。 您需要查看錯誤才能知道爲什麼您無法聯繫onServiceConnected。 – 2011-03-04 08:21:59
這可能有所幫助:http://stackoverflow.com/questions/9310266/can-mediaplaybackservice-be-used-for-getting-information-of-currently-playing-au/10620084#10620084 – 2012-05-16 13:58:03