MediaPlaybackservice可以用于获取android中当前正在播放的音频文件的信息吗? [英] Can MediaPlaybackservice be used for getting information of currently playing audio file in android?

查看:1137
本文介绍了MediaPlaybackservice可以用于获取android中当前正在播放的音频文件的信息吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个将在Android中处理MediaPlaybackService的类.但是代码不起作用.

I wrote a class which will handle the MediaPlaybackService in android. But the code is not working.

我用于绑定服务连接的代码如下:

My code for binding the serviceconnection is as follows:

Intent in = new Intent();                                              
  in.setClassName("com.android.playerapps","com.android.playerapps.MediaPlayerServiceConnection");
  ServiceConnection conn = new MediaPlayerServiceConnection();
  ctx.bindService(in, conn, 0);                 

ServiceConnection类如下:

ServiceConnection class is as follows:

public class MediaPlayerServiceConnection implements ServiceConnection {
IMediaPlaybackService mService; 
public void onServiceConnected(ComponentName name, IBinder service) {
    Log.i("MediaPlayerServiceConnection", "Connected! Name: " +name.getClassName());    

    // This is the important line
    mService = IMediaPlaybackService.Stub.asInterface(service);
    // Process org.videolan.vlc.android with component org.videolan.vlc.android.AudioService
     System.out.println( "---------------- track:");

    // If all went well, now we can use the interface
    try {       

        if (mService.isPlaying())
        { 
            //String str=getTrackName();    

            //String str=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).getPath()+"/"+mService.getPath();
            System.out.println( "Playing track: "+this.getTrackName());
            Log.i("MediaPlayerServiceConnection", "Music player is playing.");
        } 
        else 
        {
            Log.i("MediaPlayerServiceConnection", "Music player is not playing.");
        }
    } 
    catch (Exception e) {
        e.printStackTrace();
        //throw new RuntimeException(e);
    }
}

public void onServiceDisconnected(ComponentName name) {
    Log.i("MediaPlayerServiceConnection", "Disconnected!");
}

public String getTrackName() {
    try {
        return ( mService.getTrackName() );
    } catch (RemoteException e) {
        Log.e("MediaConnection", "Failed to get TrackName");
        return "Failed to get TrackName";
    }           
}

请帮助!

推荐答案

这里有两个问题:

1-IMediaPlaybackService无法从其他应用程序访问.这是因为自Gingerbread(Android 2.3)起,此服务未导出到Music应用程序的清单文件中(请参阅Android源代码中的packages/apps/Music/AndroidManifest.xml):

1 - The IMediaPlaybackService is not accessible from other applications. This is because this service is not exported in the manifest file of the Music app since Gingerbread (Android 2.3) (see packages/apps/Music/AndroidManifest.xml in the Android source code):

<service android:name="com.android.music.MediaPlaybackService" 
    android:exported="false" />

当值为false时,只有相同应用程序或具有相同用户ID的应用程序的组件才能启动服务或绑定到该服务.

When the value is false, only components of the same application or applications with the same user ID can start the service or bind to it.

可以重新构建Music应用程序,将XML属性更改为true,然后将其重新安装在设备上,但这将导致问题编号2

The Music app could be rebuilt changing the XML attribute to true and installed again on the device, but this would lead to the problem number 2

2-MediaPlaybackService是内置的Android音乐应用程序的一部分,并且仅由该应用程序在内部使用.如果设备上安装了第三方音乐播放器,则不会使用MediaPlaybackService,并且不会通过其API提供有关播放的信息.

2 - The MediaPlaybackService is part of the built-in Android Music app and it is used only by this application internally. If a 3rd party music player is installed on the device, this won’t use the MediaPlaybackService and no information about the playback will be available through its API.

不幸的是,没有公共系统服务提供相同的API来访问有关当前音轨的信息或与当前播放进行交互.

Unfortunately, no public system service provides the same API to get access to information about the current audio track or to interact with the current playback.

这可能是因为Android团队决定让每个音乐播放器管理有关音轨的内部信息,而不是依赖于跟踪正在播放内容的中央服务.

This is probably because the Android team decided to let each music player manage the internal information about the audio track, rather than relying on a central service that keeps track of what’s being played.

这篇关于MediaPlaybackservice可以用于获取android中当前正在播放的音频文件的信息吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆