简体   繁体   English

如何使用Java从URL流音乐-Android

[英]How to stream music from url with Java - Android

I'm totally new to Android Development and to Android devices in general, so I don't know how things are working here. 我对Android开发和Android设备完全陌生,所以我不知道这里的工作方式。

I want to make an app that will stream music from my url and still playing the song after I minimize the application. 我想制作一个可以从我的网址流式播放音乐并在我最小化该应用程序之后仍然播放歌曲的应用程序。

I searched my question but a lot of answers were for mp3 songs or other types, but my url is from a live radio so it isn't one song only. 我搜索了我的问题,但是很多答案是关于mp3歌曲或其他类型的,但是我的网址来自现场广播,因此它不仅是一首歌。

One of the answers that I found and were good for my problem was this and uses this code: 一,我发现,是对我的问题是答案和使用此代码:

Uri myUri = Uri.parse("your url here");
Intent intent = new Intent(android.content.Intent.ACTION_VIEW); 
intent.setDataAndType(myUri, "audio/*"); 
startActivity(intent);

This prompt me to choose a music player. 这提示我选择音乐播放器。

Is there any way to just press my "play" button and hear the music? 有什么方法可以只按我的“播放”按钮并听音乐吗?

In my iOS app I use this code and I can start and stop the streaming music whenever I want without an external player: 在我的iOS应用中,我使用以下代码,并且可以在不需要外部播放器的情况下随时启动和停止流音乐:

func prepareToPlay() {

        let url = URL(string: "myUrl")

        playerItem = AVPlayerItem(url: url!)

        player = AVPlayer(playerItem: playerItem)

        player?.play()
}

Thanks in advance 提前致谢

EDIT 编辑

After suggested in comments and answer I tried to play it with MPlayer , I made a function and I called it when I tapped my button like this: 在评论和建议中提出建议之后,我尝试使用MPlayer进行播放,我做了一个函数,当我按下按钮时调用了它,如下所示:

public void playM() {
    String url = "http://android.programmerguru.com/wp-content/uploads/2013/04/hosannatelugu.mp3";

    mPlayer = new MediaPlayer();
    mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    try {
        mPlayer.setDataSource(url);
    } catch (IllegalArgumentException e) {
        Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
    } catch (SecurityException e) {
        Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
    } catch (IllegalStateException e) {
        Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        mPlayer.prepare();
    } catch (IllegalStateException e) {
        Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
    } catch (IOException e) {
        Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
    }
    mPlayer.start();
}

But I get an error (the fourth message) and I saw in the logs this: 但是我收到一条错误消息(第四条消息),并且在日志中看到以下内容:

Unable to create media player 无法创建媒体播放器

prepareAsync called in state 1, mPlayer(0x0) 在状态1中调用prepareAsync,mPlayer(0x0)

start called in state 1, mPlayer(0x0) 在状态1中开始调用,mPlayer(0x0)

error (-38, 0) 错误(-38,0)

Intent with ACTION flag is intended to open another app in most cases. 在大多数情况下,带有ACTION标志的Intent旨在打开另一个应用程序。 Since you don't need it. 由于您不需要它。 You want your own custom player. 您需要自己的自定义播放器。 So Android has a Media Player class for such scenarios. 因此,Android在此类情况下具有Media Player类。

Create instance of it and pass your stream-URL. 创建它的实例并传递您的流URL。 Now, set the data-source and call prepare() after that in onBtnClickListener() start the music by calling mp.start() 现在,设置数据源,然后在onBtnClickListener()调用prepare(), onBtnClickListener()通过调用mp.start()开始音乐

Uri myUri = ....; // initialize Uri here
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(getApplicationContext(), myUri);
mediaPlayer.prepare();
mediaPlayer.start();


PS: Catch all the exceptions and make sure the PERMISSIONS in manifest file PS:捕获所有异常,并确保清单文件中的权限

Intent is using only for sending some data between activities/services and system. Intent仅用于在活动/服务与系统之间发送一些数据。 It won't play the music. 它不会播放音乐。 It don't do anything except saying to some activity what to do. 除了对某些活动说要做什么外,它什么也没做。 You need the mechanism which will play your multimedia stream. 您需要可以播放多媒体流的机制。 You should use MediaPlayer class for playing multimedia inside your application. 您应该使用MediaPlayer类在应用程序内部播放多媒体。

Here's some tutorial, how to play music from stream: http://programmerguru.com/android-tutorial/android-mediaplayer-example-play-from-internet/ 这是一些教程,如何播放流中的音乐: http : //programmerguru.com/android-tutorial/android-mediaplayer-example-play-from-internet/

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

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