2017-10-16 118 views
0

我完全新的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

回答

1

意圖是隻使用活動/服務和系統之間發送的一些數據。它不會播放音樂。除了說一些活動要做什麼以外,它什麼也不做。您需要能夠播放多媒體流的機制。您應該使用MediaPlayer類在您的應用程序中播放多媒體。

這裏的一些教程,如何從流播放音樂:http://programmerguru.com/android-tutorial/android-mediaplayer-example-play-from-internet/

+0

好,謝謝,我會檢查的時候了... –

+0

我試了一下,但沒有奏效,如果你想檢查我的編輯 –

+0

你看到在頁面末尾添加權限代碼嗎? –

1

意圖與行動標誌的目的是在大多數情況下,打開另一個應用程序。既然你不需要它。你想要你自己的自定義球員。所以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:捕捉所有的異常,並確保在清單文件的權限