When I open the app first I can load the Url to the surface view but later on itemclick when i try to laod another url it cannot load.
My code to load url to player is :
private void setChannelLink(String url){
SurfaceHolder videoHolder = videoSurface.getHolder();
videoHolder.addCallback(this);
player = new MediaPlayer();
try {
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setDataSource(this, Uri.parse(url));
player.setOnPreparedListener(this);
player.setOnErrorListener(this);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
when I try to load another url i am doing player.stop() and player.reset() before calling this method.. What am i missing?
Related
I am using a MediaPlayer that grabs videos from URLs:
try {
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(this, uri);
}catch (FileNotFoundException e){
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
When I try and set a datasource with a url, that for instance, has been taken down, I get the following error in my logcat:
Couldn't open file on client side; trying server side:
java.io.FileNotFoundException: No content provider:
http://example.com/examplevideo.mp4
However, my catch{} does not trap this in the following code above, which I need it to do so I can display an error message etc...
Is there any way to catch this FileNotFoundException in my code?
Thanks in advance
I don't know how to catch this specific exception. But you can use following to catch any type of exception.
try {
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(this, uri);
}catch (FileNotFoundException e){
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
try {
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(this, uri);
}catch (FileNotFoundException e){
e.printStackTrace();
} catch (IOException e) {
Log.i("your tag",e.toString());
}
and look at the log with the tag you whant
Thanks all for replies.
I forgot to mention that the file is from an external server, but have adopted an approach similar to what Pramod suggested, but altered to suit my needs.
/**
* Checks if an external file on a server exists.
* #param urlString The file URL
* #return true if file exists, false if does not
*/
public boolean checkExternalFileExists(String urlString){
try {
URL url = new URL(urlString);
int response = ((HttpURLConnection)url.openConnection()).getResponseCode();
return response == HttpURLConnection.HTTP_OK;
}
catch (FileNotFoundException e) {
return false;
}catch (MalformedURLException e) {
return false;
} catch (IOException e) {
return false;
}
}
Thanks
I used this code to play a mp3 file. Its playing in any device without Samsung... I search a lot but can't solved this problem. Please can anyone give any method to play mp3 in any device.
//Not working in Samsung Device
MediaPlayer mMediaPlayer;
void playSound()
{
try {
mMediaPlayer = MediaPlayer.create(this, R.raw.recipe_tune);
mMediaPlayer.start();
} catch (Exception e) {
// TODO: handle exception
}
}
Call prepare method before starting player and also print exception in your logcat and also catch like this,
try {
//code lies here
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
I am using MediaPlayer in my app. I am following the following tutorial
http://blog.lemberg.co.uk/surface-view-playing-video
Here's part of the code
Surface surface = new Surface(surfaceTexture);
try {
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDataSource(getApplicationContext(), Uri.parse(FILE_URL));
mMediaPlayer.setSurface(surface);
mMediaPlayer.setLooping(true);
mMediaPlayer.prepareAsync();
// Play video when the media source is ready for playback.
mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.start();
}
});
} catch (IllegalArgumentException e) {
Log.d(TAG, e.getMessage());
} catch (SecurityException e) {
Log.d(TAG, e.getMessage());
} catch (IllegalStateException e) {
Log.d(TAG, e.getMessage());
} catch (IOException e) {
Log.d(TAG, e.getMessage());
}
Everything works fine except the video does not loop. When I set the data source a file from assets it loops just fine. But when I stream the video from URL it does not loop.
Thanks
I have listview, mediaplayer and mediacontroller. I'd like to make mediaplayer play stream musics from url when I touch list in listview.
My problem is mediaplayer plays only one song. When I touch second list while playing first list audio, it does not play second list audio.But I do not know the cause, mediaplayer plays second audio while playing first audio, when mediacontroller hides.
I want mediaplayer to stop and play another audio while mediacontroller shows and plays audio.
I tried two patterns, but these were same result.Please teach me what is wrong and sorry bad english skill.First pattern.
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
switch (position) {
case 0:
try {
mp.reset();
mp.setDataSource(url);
mp.prepare();
} catch (IllegalArgumentException e) {
//
e.printStackTrace();
} catch (IllegalStateException e) {
//
e.printStackTrace();
} catch (IOException e) {
//
e.printStackTrace();
}
break;
case 1:
try {
mp.reset();
mp.setDataSource(url2);
mp.prepare();
} catch (IllegalArgumentException e) {
//
e.printStackTrace();
} catch (IllegalStateException e) {
//
e.printStackTrace();
} catch (IOException e) {
//
e.printStackTrace();
}
break;
case 2:
try {
mp.reset();
mp.setDataSource(url3);
mp.prepare();
} catch (IllegalArgumentException e) {
//
e.printStackTrace();
} catch (IllegalStateException e) {
//
e.printStackTrace();
} catch (IOException e) {
//
e.printStackTrace();
}
break;
}
Second pattern.
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
try {
mp.reset();
mp.setDataSource(url[position]);
mp.prepare();
} catch (IllegalArgumentException e) {
//
e.printStackTrace();
} catch (IllegalStateException e) {
//
e.printStackTrace();
} catch (IOException e) {
//
e.printStackTrace();
}
Preparedlistner.
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
handler.post(new Runnable() {
#Override
public void run() {
controller.setEnabled(true);
controller.show(mp.getDuration());
mp.start();
try {
Method m = android.widget.MediaController.class.getDeclaredMethod("updatePausePlay");
m.setAccessible(true);
m.invoke(controller);
} catch (Exception e) {
}
}
});
}
});
}
onCreate
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_songs);
ItemBean sample1 = new ItemBean();
fishing.setName("sample1");
ItemBean sample2 = new ItemBean();
hana.setName("sample2");
ItemBean sample3 = new ItemBean();
takane.setName("sample3");
List<ItemBean> list = new ArrayList<ItemBean>();
list.add(sample1);
list.add(sample2);
list.add(sample3);
sampleurl = getResources().getStringArray(R.array.sampleurl);
mp = new MediaPlayer();
controller = new MediaController(this);
controller.setAnchorView(findViewById(R.id.mediaController));
controller.setMediaPlayer(this);
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
samplelist = (ListView) findViewById(R.id.song_list);
SonglistAdapter_test adapter = new SonglistAdapter_test(getApplicationContext(),list);
samplelist.setAdapter(adapter);
samplelist.setOnItemClickListener(this);
}
Do you call mcontroller = new MediaController(context); and doClean() somewhere? Most likely the controller needs to be reset before you put a new media source. Add this before your handler.post(new Runnable() {:
doClean();
mcontroller = new MediaController(getActivity());//or this, depending whats your context
public void playClickSound() {
AssetFileDescriptor afd;
MediaPlayer sound = new MediaPlayer();
try {
sound.setAudioStreamType(AudioManager.STREAM_MUSIC);
afd = getAssets().openFd("click.mp3");
sound.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
sound.prepare();
sound.start();
} catch (IllegalStateException e) {
} catch (IOException e) {
e.printStackTrace();
}
}
This is my code for playing sound. I call this method on a few buttons in mu GUI.
It works fine the first time i press the button, but second time i get IllegalStateException.
What should I do to make this work?
You need to manage the life-cycle of the Media Player. Following the below flow, should work out:
RefreshPlayer()
{
if (mediaPlayer != null) {
{
mediaPlayer.stop();
mediaPlayer.reset();
}
}
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(getSherlockActivity(),
Uri.fromFile(new File(VidPath)));
mediaPlayer.setLooping(true);
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IllegalArgumentException e)
{
e.printStackTrace();
} catch (IllegalStateException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}