Play Audio From Server Path
I want to play an audio in background on click of an image, below is XML code of the image Copy
see below link for more detail:-
How to play Audio file Mp3 from the server
Solution 2:
first your intialize the media player in your app.
MediaPlayer mp ;
mp = newMediaPlayer();
set the path and file name and then start your media in bg.
mp.setDataSource(path+fileName);
mp.prepare();
mp.start();
Solution 3:
Trythis way,hope this will help you to solve your problem.
Customclassfor audio play
publicclassMyAudioPlayer {
privateMediaPlayer audioPlayer;
privateAudioListener audioListener;
staticMyAudioPlayer myAudioPlayer;
/**
* This method used to set audio action listener.
*
* @paramaudioListener
* represented audio listener
*/publicvoidsetAudioListener(AudioListener audioListener) {
this.audioListener = audioListener;
}
/**
* This method used to play audio.
*
* @paramurl
* represented audio path
*/publicvoidplayAudio(String url) {
if (myAudioPlayer != null) {
myAudioPlayer.stopAudio();
myAudioPlayer = null;
}
myAudioPlayer = this;
audioPlayer = newMediaPlayer();
audioPlayer.setOnErrorListener(newMediaPlayer.OnErrorListener() {
@OverridepublicbooleanonError(MediaPlayer mp, int what, int extra) {
if (audioListener != null) {
audioListener.onComplete();
}
returnfalse;
}
});
audioPlayer.setOnCompletionListener(newMediaPlayer.OnCompletionListener() {
@OverridepublicvoidonCompletion(MediaPlayer mp) {
if (audioListener != null) {
audioListener.onComplete();
}
}
});
audioPlayer.setOnPreparedListener(newMediaPlayer.OnPreparedListener() {
@OverridepublicvoidonPrepared(MediaPlayer mp) {
audioPlayer.start();
if (audioListener != null) {
audioListener.onPrepared();
}
}
});
try {
audioPlayer.setDataSource(url);
audioPlayer.prepareAsync();
} catch (IOException e) {
if (audioListener != null) {
audioListener.onComplete();
}
e.printStackTrace();
}
}
/**
* This method used to stop audio.
*/publicvoidstopAudio() {
if (audioPlayer != null) {
if (audioPlayer.isPlaying()) {
audioPlayer.reset();
audioPlayer.release();
audioPlayer = null;
if (audioListener != null) {
audioListener.onComplete();
}
} else {
audioPlayer.reset();
audioPlayer.release();
audioPlayer = null;
}
}
}
/**
* Inner Interface
*
* @authortasol
*
*/publicinterfaceAudioListener {
voidonComplete();
voidonPrepared();
voidonPlayClicked(boolean isplaying);
}
/**
* This method used to check is audio playing.
*
* @return represented {@link Boolean}
*/publicbooleanisPlaying() {
if (audioPlayer != null) {
if (audioPlayer.isPlaying()) {
returntrue;
}
}
returnfalse;
}
}
How to play audio from server url path
MyAudioPlayer audioPlayer;
MyAudioPlayer.AudioListener audioListener;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
audioPlayer = newMyAudioPlayer();
audioPlayer.setAudioListener(newMyAudioPlayer.AudioListener() {
@OverridepublicvoidonComplete() {
audioListener.onComplete();
}
@OverridepublicvoidonPrepared() {
audioListener.onPrepared();
}
@OverridepublicvoidonPlayClicked(boolean isplaying) {
audioListener.onPlayClicked(isplaying);
}
});
audioPlayer.playAudio("yourserverurlaudiopath");
}
Solution 4:
MediaPlayermp=newMediaPlayer();
// Set data source -
setDataSource("/sdcard/path_to_song");
// Play audio
mp.start();
// Pause audio
mp.pause();
// Reset mediaplayer
mp.reset();
// Get song length duration - in milliseconds
mp.getDuration();
// Get current duration - in milliseconds
mp.getCurrentDuration();
// Move song to particular second - used for Forward or Backward
mp.seekTo(positon); // position in milliseconds// Check if song is playing or not
mp.isPlaying(); // returns true or false
Refer this link for step by step tutorial to build audio player inside your app.
Post a Comment for "Play Audio From Server Path"