简体   繁体   English

如何通过应用程序将音乐加入默认的Android音乐播放器?

[英]How to enqueue music to default android music player via app?

I am new to android app development, but I have good knowledge of programming in JAVA and in general. 我是android应用程序开发的新手,但是我对Java和一般的编程都有很好的了解。 I am trying to write an app for android which can enqueue music files to default music player in android (like Google Play Music). 我正在尝试为Android编写一个应用程序,该应用程序可以将音乐文件放入android中的默认音乐播放器(例如Google Play音乐)。 My app decides which song to play when, but I don't want to write a full-blown music player app. 我的应用程序决定何时播放哪首歌曲,但我不想编写功能完善的音乐播放器应用程序。 I just want to feed the existing player app with new music. 我只想给现有的播放器应用程序添加新音乐。

I am looking for something like "inter-app" communication (perhaps using Intent?) through which I can feed content to the music player and probably control the player itself. 我正在寻找诸如“应用程序间”通信(也许使用Intent?)之类的东西,通过它我可以向音乐播放器提供内容,并可能控制播放器本身。

I am not sure if such facility exist in android, so other alternative suggestions are also welcome. 我不确定android中是否存在这样的工具,因此也欢迎其他替代建议。 Also, please let me know if I didn't explain the problem properly. 另外,如果我没有正确解释问题,请告诉我。

Intents are a very powerful feature of Android to which there isn't any direct analog in Java. 意图是Android的一项非常强大的功能,Java中没有直接的模拟功能。 What you want to use is a mechanism known as an implicit Intent. 您要使用的是一种称为隐式Intent的机制。 Normally, when you launch one activity from another, you create an Intent and specify which activity to start. 通常,从另一个活动启动一个活动时,您将创建一个Intent并指定要启动的活动。 With an implicit Intent, you provide an action ( Intent.ACTION_VIEW ) and data (a URI pointing to a music file). 使用隐式Intent,您可以提供一个动作( Intent.ACTION_VIEW )和数据(一个指向音乐文件的URI)。 Using an implicit Intent, you have a piece of data handled without knowing in advance which Activity will do the handling. 使用隐式Intent,您可以处理一条数据,而无需事先知道哪个Activity将进行处理。

When you pass your Intent to startActivity() , the OS will attempt to resolve the data in the best way possible, typically popping up a list of apps that can possibly handle your data. 当您将Intent传递给startActivity() ,操作系统将尝试以可能的最佳方式解析数据,通常会弹出可能处理您数据的应用程序列表。 The user selects the appropriate app and that app handles the Intent, playing your music file. 用户选择适当的应用程序,然后该应用程序将处理Intent,并播放您的音乐文件。 Any app that registers as a service capable of potentially handling your data will show up in the list. 任何注册为能够处理您的数据的服务的应用都会显示在列表中。 After passing the Intent, your activity will go into the background, and the app handling the intent will come to the foreground. 传递意图后,您的活动将进入后台,而处理该意图的应用程序将进入前台。

Once the user has selected an app to handle the Intent from your Activity, that app will always be used to handle that kind of Intent by your Activity until you delete your own app's data. 一旦用户从“活动”中选择了一个用于处理Intent的应用,该活动将始终被该“活动”用于处理此类Intent,直到您删除自己的应用数据为止。

Take a look at the official doc to get yourself started and then ask a new question when you have a more specific problem you're trying to address. 查看官方文档以开始使用,然后在尝试解决更具体的问题时提出一个新问题。

Here's a code sample that demonstrates a very simple implicit Intent example, by which a URL is opened without knowing which browser will open it: 下面的代码示例演示了一个非常简单的隐式Intent示例,通过该示例可以打开URL,而无需知道哪个浏览器将打开它:

package com.marsatomic.intentimplicitdemo;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity
{
    public final static Uri URI = Uri.parse("http://www.marsatomic.com");

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button buttonConnect = (Button)findViewById(R.id.button_connect);
        buttonConnect.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {
                Intent i = new Intent(Intent.ACTION_VIEW, URI);
                startActivity(i);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM