2014-10-17 77 views
0

嘿,我無法從我的本地主機服務器(以及其他網站與原始MP3文件)播放流。我的代碼爲已註釋的網站工作,所以我認爲它有一些 做與服務器,而不是Java代碼。android MediaPlayer setDataSource不抓取可播放文件

public class MainActivity extends ActionBarActivity{ 
 
\t //static final String ANCHOR = "http://www.hrupin.com/wp-content/uploads/mp3/testsong_20_sec.mp3"; 
 
\t static final String ANCHOR = "http://spintron.ddns.net/Random%20Access%20Memories/07%20-%20Touch.mp3"; 
 
\t 
 
    @Override 
 
    protected void onCreate(Bundle savedInstanceState) { 
 
     super.onCreate(savedInstanceState); 
 
     setContentView(R.layout.activity_main); 
 
     MediaPlayer mp = new MediaPlayer(); 
 
     mp.setVolume(1, 1); 
 
     try{ 
 
     \t mp.setDataSource(ANCHOR); 
 
     \t mp.prepareAsync(); 
 
     }catch(Exception e){ 
 
     \t e.printStackTrace(); 
 
     } 
 
     mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { 
 
\t \t \t @Override 
 
\t \t \t public void onPrepared(MediaPlayer mp) { 
 
\t \t \t \t Toast.makeText(MainActivity.this,"Prepared",Toast.LENGTH_LONG).show(); 
 
\t \t \t \t mp.start(); 
 
\t \t \t \t 
 
\t \t \t } 
 
\t \t }); 
 
     
 
    } 
 

 

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

 
    @Override 
 
    public boolean onOptionsItemSelected(MenuItem item) { 
 
     // Handle action bar item clicks here. The action bar will 
 
     // automatically handle clicks on the Home/Up button, so long 
 
     // as you specify a parent activity in AndroidManifest.xml. 
 
     int id = item.getItemId(); 
 
     if (id == R.id.action_settings) { 
 
      return true; 
 
     } 
 
     return super.onOptionsItemSelected(item); 
 
    } 
 
}

+0

@spinton URL可能是錯誤的或提到這個https://github.com/protocol10/SangeetPlayer/blob/master/src/com/akshay/sangeetplayer/net/StreamInput.java – 2014-10-17 11:57:37

回答

0

要播放的音樂文件參考下面的代碼。 我的res/raw文件夾包含options_music.wav音樂文件。

// Play Music 

    MediaPlayer mp = new MediaPlayer(); 
    AssetFileDescriptor descriptor; 


    try { 
     descriptor = getAssets().openFd("options_music.wav"); 
     mp.setDataSource(descriptor.getFileDescriptor(), 
       descriptor.getStartOffset(), descriptor.getLength()); 
     descriptor.close(); 
     mp.prepare(); 
     mp.start(); 

     mp.setOnCompletionListener(new OnCompletionListener() { 
      @Override 
      public void onCompletion(MediaPlayer mp) { 
       //Log.e("Music", "Music Completed........"); 
       mp.start(); 
      } 
     }); 

    } catch (Exception e) { 
     Log.e("ErrorMusic", "Cant Play Music File"+e); 
    } 

對於您的網址,請確保您的網址經過了正確的網址編碼並可以從您的手機訪問。

+0

謝謝你的努力。我相信我的問題是由於端口轉發問題。 – spintron 2014-10-17 21:17:00