如何通过应用程序将音乐加入默认的Android音乐播放器? [英] How to enqueue music to default android music player via app?

查看:229
本文介绍了如何通过应用程序将音乐加入默认的Android音乐播放器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是android应用程序开发的新手,但是我对Java和一般的编程都有很好的了解.我正在尝试为Android编写一个应用程序,该应用程序可以将音乐文件放入android中的默认音乐播放器(例如Google Play音乐).我的应用程序决定何时播放哪首歌曲,但我不想编写功能完善的音乐播放器应用程序.我只想给现有的播放器应用程序添加新音乐.

I am new to android app development, but I have good knowledge of programming in JAVA and in general. I am trying to write an app for android which can enqueue music files to default music player in android (like Google Play Music). 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.

我正在寻找类似应用间"通信(也许使用Intent?)的东西,通过它我可以将内容馈送到音乐播放器,并可能控制播放器本身.

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.

我不确定android中是否存在这样的工具,因此也欢迎其他替代建议.另外,如果我没有正确解释问题,请告诉我.

I am not sure if such facility exist in android, so other alternative suggestions are also welcome. Also, please let me know if I didn't explain the problem properly.

推荐答案

意图是Android的一项非常强大的功能,而Java中没有任何直接的类似物.您要使用的是一种称为隐式Intent的机制.通常,当您从另一个活动启动一个活动时,您将创建一个Intent并指定要启动的活动.使用隐式Intent,您可以提供操作(Intent.ACTION_VIEW)和数据(指向音乐文件的URI).使用隐式Intent,您可以处理一段数据,而无需事先知道哪个Activity将进行处理.

Intents are a very powerful feature of Android to which there isn't any direct analog in Java. What you want to use is a mechanism known as an implicit Intent. Normally, when you launch one activity from another, you create an Intent and specify which activity to start. With an implicit Intent, you provide an action (Intent.ACTION_VIEW) and data (a URI pointing to a music file). Using an implicit Intent, you have a piece of data handled without knowing in advance which Activity will do the handling.

当您将Intent传递给startActivity()时,操作系统将尝试以最佳方式解析数据,通常会弹出可能处理您数据的应用程序列表.用户选择适当的应用程序,然后该应用程序将处理Intent,并播放您的音乐文件.任何注册为能够处理您的数据的服务的应用程序都会显示在列表中.通过Intent后,您的活动将进入后台,而处理该Intent的应用程序将进入前台.

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. The user selects the appropriate app and that app handles the Intent, playing your music file. 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.

一旦用户从活动"中选择了一个用于处理Intent的应用,该应用将始终被活动"用于处理此类Intent,直到您删除自己的应用数据为止.

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.

看看官方文档入门并开始然后在遇到更具体的问题时提出一个新问题.

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.

下面的代码示例演示了一个非常简单的隐式Intent示例,通过该示例可以打开URL,而无需知道哪个浏览器将打开它:

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:

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;
    }
}

这篇关于如何通过应用程序将音乐加入默认的Android音乐播放器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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