简体   繁体   English

如何使我的Android应用程序播放手机音乐播放器中的音乐?

[英]how do i make my android application play music from the phone's music player?

I've created an application, and now want it to be able to connect to the MediaPlayer on the phone, and play the songs from there, without leaving my application. 我已经创建了一个应用程序,现在希望它能够连接到手机上的MediaPlayer ,并在不离开我的应用程序的情况下从那里播放歌曲。

I want to add a toggle button that, when switched on, will start playing the playlists on the phone. 我想添加一个切换按钮,该按钮在打开时将开始在手机上播放播放列表。

This is my code: 这是我的代码:

public class MainActivity extends Activity implements OnClickListener{
         ToggleButton tbMusic;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tbMusic=(ToggleButton) findViewById(R.id.tbMusic);
        if(tbMusic.isChecked())
        {
            //here i want the command to be start playing the music
        }
        tbMusic.setOnClickListener(this);
    }

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

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if(v.getId()==R.id.tbMusic)
        {
            if(tbMusic.isChecked())
            {
                //here i want the command to be start playing the music
            }
            else
            {
                //here i want a command that will stop the media player.
            }
        }
    }
Private MediaPalyer mp;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mp = MediaPlayer.create(this,R.raw.mymusic);

    tbMusic=(ToggleButton) findViewById(R.id.tbMusic);
    if(tbMusic.isChecked())
    {
       mp.start();
    }
    tbMusic.setOnClickListener(this);
}

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

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    if(v.getId()==R.id.tbMusic)
    {
        if(tbMusic.isChecked())
        {
            mp.start();
        }
        else
        {
            mp.pause();
        }
    }
}

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

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