This is the code am using to fetch a file name(.mp3) dynamically from some other class as am having many mp3 files in my assets folder:
playAudioButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
FileName audio=new FileName();
String audioName=audio.getAudioName(count).toString();
if(audioName=="NO Audio")
{
Toast.makeText(getApplicationContext(), "No Audio for this page", Toast.LENGTH_SHORT).show();
}
else
{
try {
afd=getAssets().openFd(audioName + ".mp3");
mp = new MediaPlayer();
mp.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
mp.prepareAsync();
mp.start();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
}
Log Cat :
03-20 11:21:28.726: E/SpannableStringBuilder(4418): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
03-20 11:21:28.726: E/SpannableStringBuilder(4418): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
03-20 11:22:24.968: D/libEGL(4579): loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so
03-20 11:22:24.976: D/libEGL(4579): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so
03-20 11:22:24.984: D/libEGL(4579): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
03-20 11:22:25.070: D/OpenGLRenderer(4579): Enabling debug mode 0
03-20 11:22:38.773: E/MediaPlayer(4579): start called in state 4
03-20 11:22:38.773: E/MediaPlayer(4579): error (-38, 0)
03-20 11:22:38.773: E/MediaPlayer(4579): Error (-38,0)
Points to be noted:
1. I have checked similar threads on stack overflow the answer is using prepareAsync()
2. I have used prepareAsync()
Thanks in advance.
You can also use below onPrepared() method to call start method so that start() is called after MediaPlayer is prepared.
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.start();
}
});
Instead of using this preparedSync/prepare method of MediaPlayer you can just pass the file path converting into URI to the MediaPlayer.create()
MediaPlayer mediaPlayer = MediaPlayer.create(context, URI.parse("file://"+filePath));
this will prepare it and start playing itself
Or another way is use onPrepareListener() of MediaPlayer and then from this on prepared start playing
state 4 means Mediaplayer is in preparing state
and we call other actions like
Mediaplayer.start()
Mediaplayer.stop()
Mediaplayer.pause()
or any other thing .
As per your code it is Mediaplayer.start();
you need to write onprepare method
MediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.start();
}
});
Once Onprepared() method called you can do further process
just remove
mp.start()
after
mp.prepareAsync();
And you will good to go
public class BackgroundSound extends AsyncTask<String, Void, Void> {
MediaPlayer mediaPlayerChatAudio;
#Override
protected Void doInBackground(String... params) {
String model = params[0];
final Uri uri = Uri.parse(model);
LogEm.e("EM", "::::::::::URI::::::::::" + model);
if (uri == null || uri == Uri.EMPTY) return null;
if (mediaPlayerChatAudio != null) mediaPlayerChatAudio.stop();
try {
mediaPlayerChatAudio = MediaPlayer.create(TextActivity.this, uri);
mediaPlayerChatAudio.setAudioStreamType(AudioManager.STREAM_MUSIC);
} catch (Exception e) {
// do nothing.
}
if (mediaPlayerChatAudio == null) return null;
mediaPlayerChatAudio.setVolume(1.0f, 1.0f);
mediaPlayerChatAudio.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mediaPlayerChatAudio.reset();
mediaPlayerChatAudio.release();
mediaPlayerChatAudio = null;
}
});
mediaPlayerChatAudio.start();
return null;
}
}
If the above solutions doesn't work for anyone facing the same problem.
Make sure you call mediaPlayer.reset() before changing dataSource for the mediaPlayer object
Related
how to stream a online radio without any buffering after the player is played. It should buffered for few sec for continue playing the radio,but it get stuck for few sec and playing.
my Aync task:
class Player extends AsyncTask {
private ProgressDialog progress;
#Override
protected Boolean doInBackground(String... params) {
// TODO Auto-generated method stub
Boolean prepared;
try {
mediaPlayer.setDataSource(params[0]);
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
intialStage = true;
playPause = false;
control.setBackgroundResource(R.drawable.play);
mediaPlayer.stop();
mediaPlayer.reset();
}
});
mediaPlayer.prepare();
prepared = true;
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
Log.d("IllegarArgument", e.getMessage());
prepared = false;
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
prepared = false;
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
prepared = false;
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
prepared = false;
e.printStackTrace();
}
return prepared;
}
#Override
protected void onPostExecute(Boolean result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
if (progress.isShowing()) {
progress.cancel();
}
Log.d("Prepared", "//" + result);
mediaPlayer.start();
media.stop();
intialStage = false;
}
public Player() {
progress = new ProgressDialog(MainActivity.this);
}
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
this.progress.setMessage("Buffering...");
media.start();
}
}
public void mute1(View v){
speaker.setVisibility(View.VISIBLE);
mute.setVisibility(View.INVISIBLE);
mediaPlayer.setVolume(0,0);
media.setVolume(0,0);
seekbar.setClickable(false);
seekbar.setFocusable(false);
seekbar.setEnabled(false);
}
public void speaker1(View v){
speaker.setVisibility(View.INVISIBLE);
mute.setVisibility(View.VISIBLE);
mediaPlayer.setVolume(1,1);
media.setVolume(1,1);
seekbar.setClickable(true);
seekbar.setFocusable(true);
seekbar.setEnabled(true);
}
how to get my player prepare for the online radio without any streaming after player is started.
Please Help me..
my source is:
if (playPause) {
control.setBackgroundResource(R.drawable.play);
mediaPlayer.stop();
new Player().cancel(true);
media.stop();
media.reset();
mediaPlayer.reset();
if (mediaPlayer.isPlaying())
mediaPlayer.stop();
mediaPlayer.reset();
media.stop();
playPause = false;
} else {
control.setBackgroundResource(R.drawable.pause);
if (intialStage) {
new Player()
.execute("http://streaming.shoutcast.com/MUKILFMRADIO");
} else {
if (!mediaPlayer.isPlaying())
mediaPlayer.start();
}
}
playPause = true;
}
};
I would suggest to use Exoplayer than media player.ExoPlayer is an application level media player for Android. It provides an alternative to Android’s MediaPlayer API for playing audio and video both locally and over the Internet. ExoPlayer supports features not currently supported by Android’s MediaPlayer API, including DASH and SmoothStreaming adaptive playbacks. Unlike the MediaPlayer API, ExoPlayer is easy to customize and extend, and can be updated through Play Store application updates. This links give you better idea about Exoplayer.
https://developer.android.com/guide/topics/media/exoplayer.html
https://github.com/google/ExoPlayer
I am using a method to initialize and prepare a MediaPlayer (defined as class variable). It takes a String which is the name of the song as its parameter and it sets the DataSource to that particular path.
public void create_and_prepare_song(String x) {
if (mp != null)
mp.release();
mp = new MediaPlayer();
String filePath = Environment.getExternalStorageDirectory().getPath() + "/songsfolder/" + x + ".mp3";
try {
mp.setDataSource(filePath);
} catch (IOException e) {
e.printStackTrace();
}
try {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Now, somewhere inside my code I use this method to initialize and prepare "1.mp3". Then, as soon as this song is finished, I use an onCompletionListener so that "2.mp3" is played after, and "3.mp3" then. I use:
create_and_prepare_song("1");
i = 2;
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
if (i <= 3) {
String int_to_String = i + "";
create_and_prepare_song(int_to_String); //1
mp.setOnCompletionListener(this);
mp.start();
i++;
}
}
});
mp.start();
Now, this causes app to stop from working! However, if I just copy and paste the body of the method to the //1 line instead, the app works fine, playing first, second and third song. Why is that so? Thanks a lot
Note that you are creating a new player inside of create_and_prepare_song() but the old one is still referenced inside of the listener.
That's because the global variable and the onCompletion parameter has the same name: mp.
Here's piece of code that works:
package com.example.simon.mplaylist;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import java.io.IOException;
/**
* Created by Simon on 2014 Jul 20.
*/
public class MainActivity extends Activity {
static int currentSong;
static MediaPlayer mediaPlayer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (mediaPlayer != null)
return;
currentSong = 1;
create_and_prepare_song(currentSong);
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
if (currentSong++ >= 3) {
mediaPlayer = null;
return;
}
create_and_prepare_song(currentSong);
mediaPlayer.setOnCompletionListener(this);
mediaPlayer.start();
}
});
mediaPlayer.start();
}
public void create_and_prepare_song(int songNum) {
if (mediaPlayer != null)
mediaPlayer.release();
mediaPlayer = new MediaPlayer();
String storage = Environment.getExternalStorageDirectory().getPath();
String filePath = storage + "/songsfolder/" + String.valueOf(songNum) + ".mp3";
try {
mediaPlayer.setDataSource(filePath);
} catch (IOException e) {
e.printStackTrace();
}
try {
mediaPlayer.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
NOTE also that I defined the MediaPlayer as static and also am checking # onCreate for it's presence. That way you avoid multiple songs playing at once if the activity is recreated (e.g. screen rotation).
You really shouldn't be starting actual playback inside create_and_prepare_song(). It should be started inside onPrepared() since there might be some delay from the call to prepare() and the media being ready to play.
I am making an app that uses sound effects. I would like to know how i can detect when a sound has finished playing and trigger an event .
i tried this :
player = new MediaPlayer();
if (player != null) {
player.reset();
player.release();
}
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
sonar.setBackgroundResource(R.drawable.sonar_off);
stopPlay();
}
});
if(state==1){
player = MediaPlayer.create(this, R.raw.sonar_slow);
}else if(state==2){
player = MediaPlayer.create(this, R.raw.sonar_medium);
}else if(state==3){
player = MediaPlayer.create(this, R.raw.sonar_fast);
}
try{
player.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
player.start();
You can set an OnCompleteListener() to the player object. The code for it is:
mPlayer.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp)
{
"your code comes here"
}
});
Here the mPlayer is the object of the MediaPlayer which is currently running.
There are multiple ways to play sounds, assuming you use the MediaPlayer class you could register a MediaPlayer.OnCompletionListener
I've just spent about 2 hours trying to get this to work. I've never had much luck with getting videos to play with MediaPlayer. What am I doing wrong here? It plays just audio the first time through, no video. And then on the second time I get IllegalStateException's when I try to re-setDataSource. I've tried not calling stop() and release() in onCompletion I've tried calling reset() before playing. I just get different StateExceptions (0, 64, 128) I've run out of ideas.
private void playVideo() {
mMediaPlaying = true;
sv.setVisibility(View.VISIBLE); //surfaceview
try {
if(mp.isPlaying()) {
mp.stop();
mp.reset();
}
mp.setDisplay(sh); //surfaceholder
mp.setOnCompletionListener(this);
mp.setOnPreparedListener(this);
mp.setDataSource(getBaseContext(), Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.test));
mp.prepare();
//mp.start();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
#Override
public void onPrepared(MediaPlayer mp) {
Log.d("", "mp prepared");
mp.start();
}
#Override
public void onCompletion(MediaPlayer arg0) {
sv.setVisibility(View.GONE);
mp.stop();
mp.release();
mMediaPlaying = false;
Log.d("", "Done playing media");
}
You can use VideoView if you want to play video.
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/media/VideoViewDemo.html
And you can use MediaController to control the video such as play,pause,forward and rewind.
Check this link for example.
http://android-coding.blogspot.in/2011/03/using-videoview-to-play-mp4-from-sdcard.html
mp.reset() must be call before setDataSource,
have a look at the life cycle of MediaPlayer
http://developer.android.com/reference/android/media/MediaPlayer.html
Ok, I basically copy/pasted this here and it seems to be working... Except the first time it plays there is still no video... But I got rid of the errors :)
try this:
private void playVideo() {
mMediaPlaying = true;
sv.setVisibility(View.VISIBLE); //surfaceview
try {
if(mp != null) {
mp.stop();
mp.release();
}
mp = new MediaPlayer();
mp.setDisplay(sh); //surfaceholder
mp.setOnCompletionListener(this);
mp.setOnPreparedListener(this);
mp.setDataSource(getBaseContext(), Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.test));
mp.prepare();
mp.start();
I've been trying to stream mp3's over http using Android's built in MediaPlayer class. The documentation would suggest to me that this should be as easy as :
MediaPlayer mp = new MediaPlayer();
mp.setDataSource(URL_OF_FILE);
mp.prepare();
mp.start();
However I am getting the following repeatedly. I have tried different URLs as well. Please don't tell me that streaming doesn't work on mp3's.
E/PlayerDriver( 31): Command PLAYER_SET_DATA_SOURCE completed with an error or info PVMFErrNotSupported
W/PlayerDriver( 31): PVMFInfoErrorHandlingComplete
E/MediaPlayer( 198): error (1, -4)
E/MediaPlayer( 198): start called in state 0
E/MediaPlayer( 198): error (-38, 0)
E/MediaPlayer( 198): Error (1,-4)
E/MediaPlayer( 198): Error (-38,0)
Any help much appreciated, thanks
S
simple Media Player with streaming example.For xml part you need one button with id button1 and two images in your drawable folder with name button_pause and button_play and please don't forget to add the internet permission in your manifest.
public class MainActivity extends Activity {
private Button btn;
/**
* help to toggle between play and pause.
*/
private boolean playPause;
private MediaPlayer mediaPlayer;
/**
* remain false till media is not completed, inside OnCompletionListener make it true.
*/
private boolean intialStage = true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button1);
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
btn.setOnClickListener(pausePlay);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
private OnClickListener pausePlay = new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
if (!playPause) {
btn.setBackgroundResource(R.drawable.button_pause);
if (intialStage)
new Player()
.execute("http://www.virginmegastore.me/Library/Music/CD_001214/Tracks/Track1.mp3");
else {
if (!mediaPlayer.isPlaying())
mediaPlayer.start();
}
playPause = true;
} else {
btn.setBackgroundResource(R.drawable.button_play);
if (mediaPlayer.isPlaying())
mediaPlayer.pause();
playPause = false;
}
}
};
/**
* preparing mediaplayer will take sometime to buffer the content so prepare it inside the background thread and starting it on UI thread.
* #author piyush
*
*/
class Player extends AsyncTask<String, Void, Boolean> {
private ProgressDialog progress;
#Override
protected Boolean doInBackground(String... params) {
// TODO Auto-generated method stub
Boolean prepared;
try {
mediaPlayer.setDataSource(params[0]);
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
intialStage = true;
playPause=false;
btn.setBackgroundResource(R.drawable.button_play);
mediaPlayer.stop();
mediaPlayer.reset();
}
});
mediaPlayer.prepare();
prepared = true;
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
Log.d("IllegarArgument", e.getMessage());
prepared = false;
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
prepared = false;
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
prepared = false;
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
prepared = false;
e.printStackTrace();
}
return prepared;
}
#Override
protected void onPostExecute(Boolean result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
if (progress.isShowing()) {
progress.cancel();
}
Log.d("Prepared", "//" + result);
mediaPlayer.start();
intialStage = false;
}
public Player() {
progress = new ProgressDialog(MainActivity.this);
}
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
this.progress.setMessage("Buffering...");
this.progress.show();
}
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
if (mediaPlayer != null) {
mediaPlayer.reset();
mediaPlayer.release();
mediaPlayer = null;
}
}
Android MediaPlayer doesn't support streaming of MP3 natively until 2.2. In older versions of the OS it appears to only stream 3GP natively. You can try the pocketjourney code, although it's old (there's a new version here) and I had trouble making it sticky — it would stutter whenever it refilled the buffer.
The NPR News app for Android is open source and uses a local proxy server to handle MP3 streaming in versions of the OS before 2.2. You can see the relevant code in lines 199-216 (r94) here: http://code.google.com/p/npr-android-app/source/browse/Npr/src/org/npr/android/news/PlaybackService.java?r=7cf2352b5c3c0fbcdc18a5a8c67d836577e7e8e3
And this is the StreamProxy class: http://code.google.com/p/npr-android-app/source/browse/Npr/src/org/npr/android/news/StreamProxy.java?r=e4984187f45c39a54ea6c88f71197762dbe10e72
The NPR app is also still getting the "error (-38, 0)" sometimes while streaming. This may be a threading issue or a network change issue. Check the issue tracker for updates.
I guess that you are trying to play an .pls directly or something similar.
try this out:
1: the code
mediaPlayer = MediaPlayer.create(this, Uri.parse("http://vprbbc.streamguys.net:80/vprbbc24.mp3"));
mediaPlayer.start();
2: the .pls file
This URL is from BBC just as an example. It was an .pls file that on linux i downloaded with
wget http://foo.bar/file.pls
and then i opened with vim (use your favorite editor ;) and i've seen the real URLs inside this file. Unfortunately not all of the .pls are plain text like that.
I've read that 1.6 would not support streaming mp3 over http, but, i've just tested the obove code with android 1.6 and 2.2 and didn't have any issue.
good luck!
Use
mediaplayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaplayer.prepareAsync();
mediaplayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mediaplayer.start();
}
});
I've had the same error as you have and it turned out that there was nothing wrong with the code. The problem was that the webserver was sending the wrong Content-Type header.
Try wireshark or something similar to see what content-type the webserver is sending.
Looking my projects:
https://github.com/master255/ImmortalPlayer http/FTP support, One thread to read, send and save to cache data. Most simplest way and most fastest work. Complex logic - best way!
https://github.com/master255/VideoViewCache Simple Videoview with cache. Two threads for play and save data. Bad logic, but if you need then use this.
No call mp.start with an OnPreparedListener to avoid the zero state i the log..