如何从与Java网址串流音乐 - Android电子

问题描述:

我完全新的Android开发者和一般的Android设备,所以我不知道事情是如何在这里工作。如何从与Java网址串流音乐 - Android电子

我想打一个应用程序,从我的网址串流音乐,仍然播放歌曲后,我最小化应用程序。

我搜索我的问题,但很多答案都为MP3歌曲或其它类型的,但我的网址是从电台直播,所以它不是唯一的歌。

一,我发现是对我的问题的答案是this,并使用此代码:

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

这促使我选择音乐播放器。

有什么办法,只是按我的“播放”按钮,听音乐?

在我的iOS应用程序,我用这个代码,我可以启动和停止的流媒体音乐时,我想没有一个外部播放器:提前

编辑

func prepareToPlay() { 

     let url = URL(string: "myUrl") 

     playerItem = AVPlayerItem(url: url!) 

     player = AVPlayer(playerItem: playerItem) 

     player?.play() 
} 

谢谢建议中的意见和答案,我试图用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(); 
} 

但我得到一个错误(第四消息),我在日志中看到这一点:

无法创建媒体播放器

prepareAsync称为状态1,MPLAYER(为0x0)

开始称为在状态1时,MPlayer(为0x0)

误差(-38,0)

+0

什么[网络协议(https://developer.android.com/guide/topics/media/media-formats.html#network)为您的服务器使用? – CommonsWare

+0

@CommonsWare它使用HTTP –

+2

据我所知,这还不够具体的 - 看到我挂文档。欢迎您将URL传递给'MediaPlayer'并查看它是否可用。 – CommonsWare

意图是只使用活动/服务和系统之间发送的一些数据。它不会播放音乐。除了说一些活动要做什么以外,它什么也不做。您需要能够播放多媒体流的机制。您应该使用MediaPlayer类在您的应用程序中播放多媒体。

这里的一些教程,如何从流播放音乐:http://programmerguru.com/android-tutorial/android-mediaplayer-example-play-from-internet/

+0

好,谢谢,我会检查的时候了... –

+0

我试了一下,但没有奏效,如果你想检查我的编辑 –

+0

你看到在页面末尾添加权限代码吗? –

意图与行动标志的目的是在大多数情况下,打开另一个应用程序。既然你不需要它。你想要你自己的自定义球员。所以Android有这样的场景的媒体播放器类。

创建它的实例并传递你的流URL。现在,通过调用设置数据源和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(); 


P.S:捕捉所有的异常,并确保在清单文件的权限