I want to play a live HTTP stream in my Android application, so I installed Windows Media Encoder 9 on another computer on the same local network and used it to create an HTTP audio streaming stream.
The direct HTTP stream is fine: I tested it, and it can be played back using Windows Media Player or VLC on a PC, and it can played back VLC for Android on my mobile device.
So, in my Android app, I wrote this code:
private MediaPlayer player = null; @Override public void onCreate(Bundle savedInstanceState) { StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() .detectDiskReads() .detectDiskWrites() .detectNetwork() // or .detectAll() for all detectable problems .penaltyLog() .build()); StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder() .detectLeakedSqlLiteObjects() //.detectLeakedClosableObjects() .penaltyLog() .penaltyDeath() .build()); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnPlay = (Button)findViewById(R.id.play); address = (TextView)findViewById(R.id.address); btnPlay.setOnClickListener(new OnClickListener() { public void onClick(View v) { play(); } }); } private void play() { String serverIp; serverIp = address.getText().toString(); // get the uri address, for example http://xxx.xxx.xxx.xxx:2340 if (player == null) { player = new MediaPlayer(); } else { player.stop(); player.reset(); } try { Log.v("", "Init a new MediaPlayer"); player.setAudioStreamType(AudioManager.STREAM_MUSIC); Log.v("", "Set the stream type to STREAM_MUSIC"); player.setDataSource(this, Uri.parse(serverIp)); Log.v("", "Set the source is " + serverIp); player.setOnBufferingUpdateListener(this); player.setOnPreparedListener(this); player.setOnErrorListener(this); player.prepareAsync(); Log.v("", "After prepareAsync"); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block Log.v("IllegalArgumentException", e.toString()); } catch (SecurityException e) { // TODO Auto-generated catch block Log.v("SecurityException", e.toString()); } catch (IllegalStateException e) { // TODO Auto-generated catch block Log.v("IllegalStateException", e.toString()); } catch (IOException e) { // TODO Auto-generated catch block Log.v("IOException", e.toString()); } catch (Exception e) { Log.v("Exception",e.toString()); } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } @Override public void onPrepared(MediaPlayer mp) { // TODO Auto-generated method stub try { Log.v("onPrepared", "After prepareAsync"); mp.start(); } catch (Exception e) { Log.v("play", e.toString()); } } @Override public void onBufferingUpdate(MediaPlayer mp, int percent) { // TODO Auto-generated method stub Log.v("onBufferingUpdate", "Buffering Update"); } @Override public boolean onError(MediaPlayer mp, int what, int extra) { // TODO Auto-generated method stub Log.v("MediaPlayer onError", "what=" + what + " extra=" + extra); return true; }
But that did not work. When I press the play button, nothing happens. But if I enter another URL that I found on the Internet (e.g. http://www.example.com/song.mp3 ), it works.
So can someone help me? Information about the magazine is given below. My mobile phone is HTC s710e and the Android version is 4.0.4.
11-20 22:28:14.137: V/(580): Init a new MediaPlayer 11-20 22:28:14.137: V/(580): Set the stream type tp STREAM_MUSIC 11-20 22:28:14.147: D/MediaPlayer(580): Couldn't open file on client side, trying server side 11-20 22:28:14.178: E/Trace(39): error opening trace file: No such file or directory (2) 11-20 22:28:14.178: V/(580): After prepareAsync 11-20 22:28:14.297: V/ChromiumHTTPDataSource(39): connect on behalf of uid 10044 11-20 22:28:14.339: I/qtaguid(39): Tagging socket 27 with tag 3f500000000(1013) for uid 10044 failed errno=-2 11-20 22:28:14.629: I/ChromiumHTTPDataSourceSupport(39): Server responded with http status 400 11-20 22:28:14.648: I/qtaguid(39): Untagging socket 27 failed errno=-2 11-20 22:28:14.657: I/AwesomePlayer(39): mConnectingDataSource->connect() returned -1004 11-20 22:28:14.657: E/MediaPlayer(580): error (1, -1004) 11-20 22:28:14.667: E/MediaPlayer(580): Error (1,-1004) 11-20 22:28:14.667: V/MediaPlayer onError(580): what=1 extra=-1004