如何获得在Android当前播放曲目的路径 [英] How to get the path of a current playing track on Android

查看:465
本文介绍了如何获得在Android当前播放曲目的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个应用程序流的当前播放的音乐到其他设备。
两个设备之间的连接不工作,我也可以流通过WiFi一些字符串,但我有问题,获取当前曲目。

I want to write an app to stream the current playing music to another device. The connection between the two devices does work and I also can stream some strings over wifi but I got problems with getting the current track.

我用的code这个博客以获得当前正在播放的曲目的一些信息,但我无法得到的文件路径。

I used the code of this blog to get some info of the current playing track, but I couldn't get the path to the file.

我觉得应该是有可能的时候我甚至收到专辑,曲目或艺术家得到的路径。但是我试了又试,并没有发现任何东西。所以,我要问:是它甚至有可能通过一个接收器获取路径?如果不是有另一种方式来获得当前的轨道,它的路径?

I think it should be possible to get the path when I even receive the album, track or artist. But I tried and tried and didn't find anything. So I'm asking: is it even possible to get the path through a receiver? If not is there another way to get the current track and its path?

推荐答案

我寻觅了很多关于这一点,我发现很多的解决方案,但他们并没有为me.At最后工作,我已经使用所有现有的组合解决方案和它的工作。
你要添加的所有其他玩家在你的app.Then它将返回当前播放歌曲的标题(它将返回文件名,如果你的歌没有标题)为您:

I have Searched a lot for this and I have found lots of solutions but they didn't work for me.At last I have used a combination of all existing solutions and it worked. You have to add all other players in your app.Then it will return currently playing song's title(it will return file name if your song doesn't have title) for you:

intent.getStringExtra("track")

最后查询这个特定歌曲,你可以访问这首歌的所有信息,当然,这是它是否适合you.This path.Let我为我的作品:

At last query this specific song and you can access all information about this song and of course it's path.Let me if it works for you.This works for me:

public class GetMusic extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.get_music);

    IntentFilter iF = new IntentFilter();
    iF.addAction("com.android.music.playstatechanged");
    iF.addAction("com.htc.music.playstatechanged");
    iF.addAction("fm.last.android.playstatechanged");
    iF.addAction("com.sec.android.app.music.playstatechanged");
    iF.addAction("com.nullsoft.winamp.playstatechanged");
    iF.addAction("com.amazon.mp3.playstatechanged");
    iF.addAction("com.miui.player.playstatechanged");
    iF.addAction("com.real.IMP.playstatechanged");
    iF.addAction("com.sonyericsson.music.playstatechanged");
    iF.addAction("com.rdio.android.playstatechanged");
    iF.addAction("com.samsung.sec.android.MusicPlayer.playstatechanged");
    iF.addAction("com.andrew.apollo.playstatechanged");
    iF.addAction("gonemad.dashclock.music.playstatechanged");
    iF.addAction("com.piratemedia.musicmod.playstatechanged");
    iF.addAction("com.tbig.playerpro.playstatechanged");
    iF.addAction("org.abrantix.rockon.rockonnggl.playstatechanged");
    iF.addAction("com.maxmpz.audioplayer.playstatechanged");
    iF.addAction("com.doubleTwist.androidPlayer.playstatechanged");
    iF.addAction("com.lge.music.playstatechanged"); 
    registerReceiver(mReceiver, iF);
  }
    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        Uri mAudioUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        String selection = MediaStore.Audio.Media.TITLE + " == \"" + intent.getStringExtra("track") + "\"";
        String[] STAR = { "*" };
        Cursor cursor = getContentResolver().query(mAudioUri,STAR, selection, null, null);
        if (cursor != null && cursor.getCount() > 0) {
            cursor.moveToFirst();
            if (cursor.getColumnIndex(MediaStore.Audio.Media.TITLE) != -1) {
                String fullpath = cursor.getString(cursor
                        .getColumnIndex(MediaStore.Audio.Media.DATA));
                Toast.makeText(getApplicationContext(),"song path: " + fullpath,Toast.LENGTH_LONG).show();
            }
        }
    }
  };
}

这篇关于如何获得在Android当前播放曲目的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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