I am new to android trying to make a custom video controller from an opensource snippet, i have added comments where i thought were necessary for understanding the code a little better, hope it will be helpfull
public void playVideo(){
if(videoview.isPlaying()){
videoview.stopPlayback();
}
try {
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
File file = new File(path);//path is a string
FileInputStream inputStream = new FileInputStream(file);//log says warning is here
player.setDataSource(inputStream.getFD());
inputStream.close();
player.setOnPreparedListener(this);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Thanks in advance!!
To get an InputStream for a raw resource, use openRawResource(int).
Possibly you could also use MediaPlayer.create(Context,int) to directly create a MediaPlayer with a given resource identifier.
Related
If trying to open Password Protected PDF with PdfRenderer API, gives SecurityException and handled accordingly, inside catch block and thenonDestroy basic clean up is done, and comes back to home activity and then navigating a simple non-protected PDF than again the same exception occurs.
Please note, this happens only once any protected file got opened.
Refer below code:
#Override
protected PdfRenderer doInBackground(Uri... uri) {
Uri uriToProcess = uri[0];
try {
contentResolver=getContentResolver();
parcelFileDescriptor = contentResolver.openFileDescriptor(uriToProcess, "r");
if(parcelFileDescriptor!=null && mPdfRenderer==null) {
mPdfRenderer = new PdfRenderer(parcelFileDescriptor);
}
} catch (FileNotFoundException e) {
exceptionMsg="Sorry! No such file or directory found";
handleExceptionInUI(exceptionMsg, progressDialog);
Log.e("$$$$ FNFException", e.toString());
} catch (IOException e) {
exceptionMsg="Sorry! Something went wrong with IO";
handleExceptionInUI(exceptionMsg, progressDialog);
Log.e("$$$$ IOException", e.toString());
} catch (SecurityException e) {
if (parcelFileDescriptor!=null) {
try {
parcelFileDescriptor.close();
parcelFileDescriptor = null;
contentResolver=null;
} catch (IOException e1) {
e1.printStackTrace();
}
}
if (mPdfRenderer!=null){
mPdfRenderer.close();
mPdfRenderer=null;
}
exceptionMsg="Password protected file, This can't be opened";
handleExceptionInUI(exceptionMsg, progressDialog);
Log.e("$$$$ SecurityException", e.toString());
} catch (Exception e) {
exceptionMsg="Sorry! Something went wrong.";
handleExceptionInUI(exceptionMsg, progressDialog);
Log.e("$$$$ EXCEPTION", e.toString());
}
return mPdfRenderer;
}
Any help cordially appreciated.
I had the same problem with my app. The way I solved it was using an https://github.com/TomRoush/PdfBox-Android and load the document and check for password protection. After no InvalidPasswordException has been thrown the file can be safely loaded with the PdfRenderer.
I'm trying to edit tags of audio files and for that I'm using JaudioTagger. But a problem exists and I can't figure it out.
I'm getting this error:
java.lang.NoSuchMethodError: No virtual method toPath()Ljava/nio/file/Path; in class Ljava/io/File; or its super classes (declaration of 'java.io.File' appears in /system/framework/core-oj.jar)
And my code for getting tags of audio files :
File path = new File(songInfoModel.getData());
Log.e("Path: ", path.toString());
try {
AudioFile f = AudioFileIO.read(path);
Tag tag = f.getTag();
String artist = tag.getFirst(FieldKey.ARTIST);
tag.getFirst(FieldKey.ALBUM);
tag.getFirst(FieldKey.TITLE);
tag.getFirst(FieldKey.COMMENT);
tag.getFirst(FieldKey.YEAR);
tag.getFirst(FieldKey.TRACK);
tag.getFirst(FieldKey.DISC_NO);
tag.getFirst(FieldKey.COMPOSER);
tag.getFirst(FieldKey.ARTIST_SORT);
Log.e("Artist: ",artist);
} catch (CannotReadException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (TagException e) {
e.printStackTrace();
} catch (ReadOnlyFileException e) {
e.printStackTrace();
} catch (InvalidAudioFrameException e) {
e.printStackTrace();
}
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
here is string to setDataSource
/data/data/com.player/app_player/file.mp3
getting E/Exception: setDataSource failed.
here is the code:
mediaPlayer.setDataSource(context, Uri.parse("/data/data/com.player/app_player/file.mp3"));
I stored that file using this code
getContext().getDir("player", Context.MODE_PRIVATE)
which is same as /data/data/com.player/app_player
using content://data/data/com.player/app_player/file.mp3 did not work.
This is how I solve it.
String musicUrl = "";
if (songSavedInDB()) {
musicUrl = "here is any file path(Internal or external)"
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(musicUrl);
mPlayer.setDataSource(fileInputStream.getFD());
fileInputStream.close();
mPlayer.prepare();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else {
musicUrl = "here is url";
try {
mPlayer.setDataSource(getContext(), Uri.parse(musicUrl));
mPlayer.prepareAsync();
} catch (IOException e) {
e.printStackTrace();
}
}
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();
}