我正在寻找一个从互联网接收一些数据的Android应用程序,并根据它,播放,暂停,跳过一首曲目或前往任何音乐播放器最后播放的前一曲目 . 理想情况下,我希望它适用于所有音乐播放器,但最糟糕的情况 - 它应该适用于亚马逊主要音乐,谷歌播放音乐,苹果音乐和Saavn .

我听说我不可能这样做 . 但我知道我们可以使用耳机按钮控制媒体播放器 . 我可以从自己的应用程序模拟这些按钮吗?

package com.example.hynd.musicchanger;
import java.lang.*;

import android.content.Intent;
import android.os.SystemClock;
import android.support.annotation.RequiresPermission;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements 
View.OnClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button previous = (Button) findViewById(R.id.previous);
    Button next = (Button) findViewById(R.id.next);
    previous.setOnClickListener(this);
    next.setOnClickListener(this);
}

public void onClick(View v)
{
    long eventtime = SystemClock.uptimeMillis();

    switch (v.getId())
    {
        case R.id.previous:

            Intent prevIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
            KeyEvent prevEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN,   KeyEvent.KEYCODE_MEDIA_PREVIOUS, 0);
            prevIntent.putExtra(Intent.EXTRA_KEY_EVENT, prevEvent);
            sendOrderedBroadcast(prevIntent, null);
            Log.d("Going to last", "Going to last");
            break;

        case R.id.next:
            Intent nextIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
            KeyEvent nextEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_NEXT, 0);
            nextIntent.putExtra(Intent.EXTRA_KEY_EVENT, nextEvent);
            sendOrderedBroadcast(nextIntent, null);
            Log.d("Going next", "Going next");

            break;

        default:
            Log.d("WTF happened?", "Nothing happened");

    }
}



}

这就是我现在所做的,但这在大多数应用程序上都不能正常运行,而在某些应用程序上根本不适用 .