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

查看:30
本文介绍了如何通过应用程序将音乐排入默认的 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.

推荐答案

Intents 是 Android 的一个非常强大的特性,在 Java 中没有任何直接的类似物.您要使用的是一种称为隐式 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 后,您的 Activity 将进入后台,处理 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.

一旦用户从您的 Activity 中选择了一个应用来处理 Intent,该应用将始终用于处理您的 Activity 的那种 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天全站免登陆