i want to play a sound from url :: but i cant able to do that , i have implement this code
package com.TestAudio;
import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
public class TestAudio extends Activity {
/** Called when the activity is first created. */
private MediaPlayer xmPlayer2, xmPlayer3, xmPlayer4, xmPlayer5, xmPlayer6,
xmPlayer7,xmPlayer8,xmPlayer9,xmPlayer10,xmPlayer11;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
prepareforaudio(xmPlayer2,"bike_route.mp3");
prepareforaudio(xmPlayer3,"closed.mp3");
prepareforaudio(xmPlayer4,"exit.mp3");
prepareforaudio(xmPlayer5,"no_parking.mp3");
prepareforaudio(xmPlayer6,"one_way.mp3");
prepareforaudio(xmPlayer7,"phone.mp3");
prepareforaudio(xmPlayer8,"rail_road_crossing.mp3");
prepareforaudio(xmPlayer9,"sale.mp3");
prepareforaudio(xmPlayer10,"stop.mp3");
prepareforaudio(xmPlayer11,"turn_off_tap.mp3");
}
public void prepareforaudio(MediaPlayer player,String music1)
{
try
{
player.开发者_C百科setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setDataSource("http://xyz/MRESC/images/test/"+music1);
player.prepare();
player.start();
}catch (Exception e) {
// TODO: handle exception
}
}
}
Permission :
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
initialize your media player object as MediaPlayer mplayer=new MediaPlayer();
精彩评论