开发者

Play song in android in default media player.... i dont want to create my own [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

开发者_JS百科

Closed 3 years ago.

Improve this question

Hi guys I want to make a simple application. It will have one button which when clicked will play the song who's URI is hard coded. So i basically want the user to click a button and some song will play in the default media player so that i don't have to code for media player buttons.

Is this possible ? do u have a better approach?

this whole situation arises from the problem of fetching album art basically :)


Maybe this helps ya:

        MediaPlayer mp = new MediaPlayer();     
    mp.setLooping(true);
    try {
        mp.setDataSource(mFile);
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        mp.prepare();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    mp.start();

mFile is the path to a mp3-file. maybe you can share an uri instead.


Android has a media controller which can bind the media player object. It can be achieved by :

MediaPlayer mediaPlayer = new MediaPlayer()
MediaController mc = new MediaController(mediaPlayer);
mc.setDataSource(Path);
mc.prepare();
mc.start();


If you have the location of the file you can do something as below.

Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File(data);
intent.setDataAndType(Uri.fromFile(file), "audio/*");
startActivity(intent);

Here data is your file path. This however opens a lite version of the player. I am myself looking for a better solution but this will do fine for now.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜