com.googlecode.mp4parser fails for mp3 audio file? - android

I am using the com.googlecode.mp4parser library to merge audio files. I have an external audio mp3 file which I store in raw resources. This file fails to merge due to following exception, Below is my code :
Reading a file from raw folder :
InputStream is = context.getResources().openRawResource(R.raw.my_mp3_file);
OutputStream output = null;
try {
File file = new File(context.getFilesDir(), "silence.mp3");
if(!file.exists()) {
file.createNewFile();
}
output = new FileOutputStream(file);
byte[] buffer = new byte[4 * 1024]; // or other buffer size
int read;
while ((read = is.read(buffer)) != -1) {
output.write(buffer, 0, read);
}
output.flush();
output.close();
fileReference= file;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace(); // handle exception, define IOException and others
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Code that reads movie ( Which is failing ) :
if(fileReference.exists()) {
Movie m = new MovieCreator().build(fileReference.getAbsolutePath());
}
While getting this Movie m my code fails throwing the exception :
java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List com.coremedia.iso.boxes.MovieBox.getBoxes(java.lang.Class)' on a null object reference
It works for some mp3 files fails for raw resource files ? What's wrong here ?

Here are my conclusion and solution after a lot of research
MP4Parser for merging audio and video only use .m4a extension
String root = Environment.getExternalStorageDirectory().toString();
String audio = root + "/" + "tests.m4a";
String video = root + "/" + "output.mp4";
String output = root + "/" + "aud_vid.mp4";
mux(video, audio, output);
and here is the method
public boolean mux(String videoFile, String audioFile, String outputFile) {
Movie video;
try {
video = new MovieCreator().build(videoFile);
} catch (RuntimeException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
Movie audio;
try {
audio = new MovieCreator().build(audioFile);
} catch (IOException e) {
e.printStackTrace();
return false;
} catch (NullPointerException e) {
e.printStackTrace();
return false;
}
Track audioTrack = audio.getTracks().get(0);
video.addTrack(audioTrack);
Container out = new DefaultMp4Builder().build(video);
FileOutputStream fos;
try {
fos = new FileOutputStream(outputFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
}
BufferedWritableFileByteChannel byteBufferByteChannel = new BufferedWritableFileByteChannel(fos);
try {
out.writeContainer(byteBufferByteChannel);
byteBufferByteChannel.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
private static class BufferedWritableFileByteChannel implements WritableByteChannel {
private static final int BUFFER_CAPACITY = 1000000;
private boolean isOpen = true;
private final OutputStream outputStream;
private final ByteBuffer byteBuffer;
private final byte[] rawBuffer = new byte[BUFFER_CAPACITY];
private BufferedWritableFileByteChannel(OutputStream outputStream) {
this.outputStream = outputStream;
this.byteBuffer = ByteBuffer.wrap(rawBuffer);
}
#Override
public int write(ByteBuffer inputBuffer) throws IOException {
int inputBytes = inputBuffer.remaining();
if (inputBytes > byteBuffer.remaining()) {
dumpToFile();
byteBuffer.clear();
if (inputBytes > byteBuffer.remaining()) {
throw new BufferOverflowException();
}
}
byteBuffer.put(inputBuffer);
return inputBytes;
}
#Override
public boolean isOpen() {
return isOpen;
}
#Override
public void close() throws IOException {
dumpToFile();
isOpen = false;
}
private void dumpToFile() {
try {
outputStream.write(rawBuffer, 0, byteBuffer.position());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

Seem like this issue happens because Google devs have forgotten to handle that NullPointerException case. After several hours diving into the code base, I finally found the solution and It works very fine, you can try this:
Movie movie;
try{
movie = MovieCreator.build(videoPath);
}catch(NullPointerException e){
Log.d("AsyncTask", "Catch null getMovieBoxes");
FileDataSourceImpl fileDataSource = new FileDataSourceImpl(new File(videoPath));
IsoFile isoFile = new IsoFile(fileDataSource);
List<TrackBox> trackBoxes = isoFile.getBoxes(TrackBox.class);
for (TrackBox trackBox : trackBoxes) {
SchemeTypeBox schm = Path.getPath(trackBox, "mdia[0]/minf[0]/stbl[0]/stsd[0]/enc.[0]/sinf[0]/schm[0]");
if (schm != null && (schm.getSchemeType().equals("cenc") || schm.getSchemeType().equals("cbc1"))) {
movie.addTrack(new CencMp4TrackImplImpl(fileDataSource.toString() + "[" + trackBox.getTrackHeaderBox().getTrackId() + "]", trackBox));
} else {
movie.addTrack(new Mp4TrackImpl(fileDataSource.toString() + "[" + trackBox.getTrackHeaderBox().getTrackId() + "]" , trackBox));
}
}
}

Related

Unable to view image, video after decryption android

I am using following method to encrypt and decrypt the files. File is getting decrypted properly but I am unable to view Image or video in application. File is opening in gallery and I can view image and watch video clearly. The same I am unable to do in application using Imageview and Videoview
Below is my code :
public void run() {
boolean successful = true;
operationInProgress = true;
lastUpdateAtByteNumber = 0;
totalBytesRead = 0;
timeOperationStarted = System.currentTimeMillis();
if (operationType == OPERATION_TYPE_ENCRYPTION) {
completedMessageStringId = R.string.encryption_completed;
} else {
completedMessageStringId = R.string.decryption_completed;
}
InputStream inputStream = null;
OutputStream outputStream = null;
//get the input stream
try {
inputStream = new FileInputStream(inputFileName);
} catch (IOException ioe) {
successful = false;
ioe.printStackTrace();
}
//get the output stream
try {
outputStream = new FileOutputStream(outputFileName);
} catch (IOException ioe) {
successful = false;
ioe.printStackTrace();
}
if (inputStream != null && outputStream != null) {
//call AESCrypt
try {
fileSize = inputStream.available();
AESCrypt aesCrypt = new AESCrypt(password);
if (operationType == OPERATION_TYPE_ENCRYPTION) {
//Encrypt
aesCrypt.encrypt(version, inputStream, outputStream);
} else {
//Decrypt
aesCrypt.decrypt(fileSize, inputStream, outputStream);
}
} catch (GeneralSecurityException gse) {
successful = false;
gse.printStackTrace();
} catch (UnsupportedEncodingException uee) {
successful = false;
uee.printStackTrace();
} catch (IOException ioe) {
successful = false;
} catch (NullPointerException npe) {
successful = false;
}
}
//close the streams
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException ioe) {
successful = false;
ioe.printStackTrace();
}
}
if (outputStream != null) {
try {
outputStream.flush();
outputStream.close();
} catch (IOException ioe) {
successful = false;
}
}
operationInProgress = false;
}
The reference code is https://www.aescrypt.com/download/
AndroidCrypt (AES Crypt compatible) for Android phones (source code)
File getting encrypted and decrypted properly. When I am trying to open the load the file using Imageview for image and videoview for video.
I am using below code to view image
File url1 = new File(path to my file);
Bitmap bmp = BitmapFactory.decodeFile(url1.getAbsolutePath());
objImageView.setImageBitmap(bmp);
It gives error **"libjpeg error 105 < Ss=0, Se=63, Ah=0, Al=0> from Incomplete image data"** . or **Failed to create image decoder with message 'unimplemented'**
For Video I am using below code
getPackageName is name of package
FileProvider I have declared in manifest and also in xml I have menioned fielpath
File url1 = new File(path to video file)
videoView.setVideoURI(FileProvider.getUriForFile(
this, getPackageName(),url1));
The same image and video is clearly visible in Gallery application.
Please help me.
Thanks

java.io.FileNotFoundException: /: open failed: EISDIR (Is a directory)

File albumF = getVideoAlbumDir();
String path = albumF.getAbsolutePath();
// path =/storage/emulated/0/Pictures/.MyImages (Hidden folder)
// fileSelected.fileName()=IMG_20140417_113847.jpg
File localFile = new File(path + "/" + fileSelected.fileName());
Log.v("", "file exist===" + localFile.exists());
if (!localFile.exists()) {
Log.v("", "inside if===");
Log.v("", "Parent Filet===" + localFile.getParentFile());
localFile.getParentFile().mkdirs();
// localFile.createNewFile();
copy(fileSelected, localFile);
} else {
Log.v("", "inside else===");
mCurrentPhotoPath = localFile.getAbsolutePath();
uploadMediaFile();
}
This copy method copies data from dropbox file to my local storage.
private void copy(final Entry fileSelected, final File localFile) {
final ProgressDialog pd = ProgressDialog.show(ChatActivity.this,
"Downloading...", "Please wait...");
new Thread(new Runnable() {
#Override
public void run() {
BufferedInputStream br = null;
BufferedOutputStream bw = null;
DropboxInputStream fd;
try {
fd = mDBApi.getFileStream(fileSelected.path,
localFile.getAbsolutePath());
br = new BufferedInputStream(fd);
bw = new BufferedOutputStream(new FileOutputStream(
localFile));
byte[] buffer = new byte[4096];
int read;
while (true) {
read = br.read(buffer);
if (read <= 0) {
break;
}
bw.write(buffer, 0, read);
}
pd.dismiss();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
android.os.Message msg = new android.os.Message();
msg.arg1 = 100;
if (msg.arg1 >= 100) {
progressHandler.sendMessage(msg);
mCurrentPhotoPath = localFile.getAbsolutePath();
}
} catch (DropboxException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bw != null) {
try {
bw.close();
if (br != null) {
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}).start();
I am creating file in a folder using localFile.getParentFile().mkdirs();
I got above error when I upload this file to server.
how to fix this?
If you've tried all other options - and problem still persists - then maybe you have a case when the file you want to create matches name of already existing directory.(which might be earlier created my some call to mkdirs() maybe accidentally).
Example:
You want to save file Test\test.pdf but you already have folder Test\Test.pdf\

Reading Arabic from UTF-8 encoded Text file in android?

I am working on an application in which I have a some text in English and Arabic. For the sake of example I can say it as a words meaning application. The word is in English and user will get it's meaning in Arabic.
For Example:
Test اختبار // Test is the word and then there is it's meaning in Arabic
But when I read this local file I don't get Arabic as intended. Instead I get some strange characters. I am making sure that file is UTF-8 encoded and when I read the file I again pass encoding scheme to be UTF-8..but it does not wwork. Code snippet is as follows:
InputStream inputStream = resources.openRawResource(R.raw.textfile);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
try {
String line;
while((line = reader.readLine()) != null) {
String[] strings = TextUtils.split(line, " ");
if (strings.length < 2) continue;
addWord(strings[0].trim(), strings[1].trim());
}
} finally {
reader.close();
}
Any help is appreciated..Thanks..!!!
I actually built an helper class that handles FileIO (and is completely compatible with Hebrew) so I guess Arabic will be no problem:
/***
*
* #author Android Joker ©
* Do NOT copy without confirmation!
* Thanks!
*
*/
public class FileMethods {
private Boolean isOk;
private Context mContext;
private String fileName;
public FileMethods(Context c, String FILENAME) {
this.isOk = true;
this.mContext = c;
this.fileName = FILENAME;
}
public void reWrite(Object DATA) {
//For deleting the content of the file and then writing
try {
FileOutputStream fos = mContext.openFileOutput(this.fileName, Context.MODE_PRIVATE);
fos.write(DATA.toString().getBytes());
fos.close();
Log.i("File Writing ("+this.fileName+")", "Success!");
isOk = true;
}
catch (IOException e) {
e.printStackTrace();
Log.e("File Writing ("+this.fileName+")", "Failed!");
isOk = false;
}
}
public void Write(Object DATA) {
//For keeping the previous contents and continue writing
String data = Read("") + DATA.toString() + "\n";
try {
FileOutputStream fos = mContext.openFileOutput(this.fileName, Context.MODE_PRIVATE);
fos.write(data.getBytes());
fos.close();
Log.i("File Writing ("+this.fileName+")", "Success!");
isOk = true;
}
catch (IOException e) {
e.printStackTrace();
Log.e("File Writing ("+this.fileName+")", "Failed!");
isOk = false;
}
}
public void Clear() {
//For deleting all the file contents
try {
FileOutputStream fos = mContext.openFileOutput(this.fileName, Context.MODE_PRIVATE);
fos.write("".getBytes());
fos.close();
Log.i("Cleared"+"("+this.fileName+")", "Success!");
isOk = true;
}
catch (IOException e) {
e.printStackTrace();
Log.e("Cleared"+"("+this.fileName+")", "Failed!");
isOk = false;
}
}
public String Read(String inCaseOfFailure) {
//For reading (If reading failed for any reason, inCaseOfFailure will be written)
String info = "";
try {
FileInputStream fis = mContext.openFileInput(this.fileName);
byte[] dataArray = new byte[fis.available()];
if (dataArray.length>0) {
while(fis.read(dataArray)!=-1)
{
info = new String(dataArray);
}
fis.close();
Log.i("File Reading ("+this.fileName+")","Success!");
isOk = true;
}
else {
try {
FileOutputStream fos = mContext.openFileOutput(this.fileName, Context.MODE_PRIVATE);
fos.write(inCaseOfFailure.getBytes());
fos.close();
Log.e("File Writing In Case Of Failure ("+this.fileName+")", "Success!");
isOk = true;
}
catch (Exception e) {
e.printStackTrace();
isOk = false;
Log.e("File Writing In Case Of Failure ("+this.fileName+")", "Failed!");
Log.e("File Writing In Case Of Failure ("+this.fileName+")", "MOVING ON");
}
}
}
catch (FileNotFoundException e) {
try {
FileOutputStream fos = mContext.openFileOutput(this.fileName, Context.MODE_PRIVATE);
if (inCaseOfFailure != null) {
fos.write(inCaseOfFailure.getBytes());
fos.close();
Log.e("File Writing In Case Of Failure ("+this.fileName+")", "Success!");
isOk = true;
}
else {
Log.e("File Writing In Case Of Failure ("+this.fileName+")", "Failed!");
isOk = false;
}
}
catch (IOException e1) {
e.printStackTrace();
Log.e("File Writing In Case Of Failure ("+this.fileName+")", "Failed!");
isOk = false;
}
}
catch (IOException e) {
e.printStackTrace();
Log.e("File Reading ("+this.fileName+")", "Failed!");
isOk = false;
}
return info;
}
public Boolean GetIsOK() {
//Method that checks whether the FileIO was successfully running or not
Boolean temp = isOk;
isOk = true;
return temp;
}
}
Each instance of the class handles another file (FILENAME).
Hope this helps!

Android FTPClient - retrieveFileStream() always returns null

I am a newbie to Android. I am trying download a file from ftp server to sdcard using Apache Commons FTPClient. The line InputStream input = client.retrieveFileStream("/" + fileName); always returns null. But the file is there in Ftp location. Kindly help me to know where the mistake is.
I have set the following permissions in my manifest; android:name="android.permission.INTERNET" and android:name="android.permission.WRITE_EXTERNAL_STORAGE"
My Code
private static void downLoad(){
FTPClient client = new FTPClient();
FileOutputStream fos = null;
try {
client.connect("ftp.doamin.com");
client.login("8888", "8888");
String filePath = "/mnt/sdcard/download/CheckboxHTML.txt" ;
String fileName = "CheckboxHTML.txt";
fos = new FileOutputStream(filePath);
InputStream input = client.retrieveFileStream("/" + fileName);
byte[] data = new byte[1024];
int count = input.read(data);
while ((count = input.read(data)) != -1) {
fos.write(data, 0, count);
}
fos.close();
if(!client.completePendingCommand()) {
client.logout();
client.disconnect();
System.err.println("File transfer failed.");
}
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Thanks for your time and interest. Ananth.
AFAIK. You are suppose to finalize file transfers by calling completePendingCommand() and verifying that the transfer was indeed successful. i.e. you need to add call the function below fos.clos().
fos.close();
client.completePendingCommand()
You may also consider this, according to the API for FTPClient.retrieveFileStream(), the method returns null when it cannot open the data connection, in which case you should check the reply code (e.g. getReplyCode(), getReplyString(), getReplyStrings()) to see why it failed.
File file = new File(Environment.getExternalStorageDirectory() + "/pdf");
if(!file.exists())
file.mkdir(); //directory is created;
try {
ftp = new FTPClient();
ftp.connect("yours ftp URL",21);//don't write ftp://
try {
int reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
throw new Exception("Connect failed: " + ftp.getReplyString());
}
if (!ftp.login("username","password")) {
throw new Exception("Login failed: " + ftp.getReplyString());
}
try {
ftp.enterLocalPassiveMode();
if (!ftp.setFileType(FTP.BINARY_FILE_TYPE)) {
// Log.e(TAG, "Setting binary file type failed.");
}
transferFile(ftp);
} catch(Exception e) {
// handleThrowable(e);
} finally {
if (!ftp.logout()) {
// Log.e(TAG, "Logout failed.");
}
}
} catch(Exception e) {
// handleThrowable(e);
} finally {
ftp.disconnect();
}
} catch(Exception e) {
// handleThrowable(e);
}
}
private void transferFile(FTPClient ftp) throws Exception {
long fileSize=0;
fileSize = getFileSize(ftp, "nag.pdf");
Log.v("async","fileSize"+fileSize);
if(!(fileSize==0)){
InputStream is = retrieveFileStream(ftp, "nag.pdf");
downloadFile(is, fileSize);
is.close();
}
else
//nosuch files
if (!ftp.completePendingCommand()) {
throw new Exception("Pending command failed: " + ftp.getReplyString());
}
}
private InputStream retrieveFileStream(FTPClient ftp, String filePath)
throws Exception {
InputStream is = ftp.retrieveFileStream(filePath);
int reply = ftp.getReplyCode();
if (is == null
|| (!FTPReply.isPositivePreliminary(reply)
&& !FTPReply.isPositiveCompletion(reply))) {
throw new Exception(ftp.getReplyString());
}
return is;
}
private byte[] downloadFile(InputStream is, long fileSize)
throws Exception {
outputStream os = newFileOutputStream(Environment.getExternalStorageDirectory()
+ "/pdf/nag.pdf");
byte[] buffer = new byte[(int) fileSize];
int readCount;
while( (readCount = is.read(buffer)) > 0) {
os.write(buffer, 0, readCount);
}
Log.i("tag", "buffer = " + buffer);
return buffer; // <-- Here is your file's contents !!!
}
private long getFileSize(FTPClient ftp, String filePath) throws Exception {
long fileSize = 0;
FTPFile[] files = ftp.listFiles(filePath);
if (files.length == 1 && files[0].isFile()) {
fileSize = files[0].getSize();
}
Log.i("tag", "File size = " + fileSize);
return fileSize;
}
}
After have worked on this problem and spent hours, I've found out that the Android Apache retrieveFile() and retrieveFileStream sometimes don't work very well when the FileSize is too big.
Ensure to set the right TypeFile to BinaryFile
mFTPClient.setFileType(FTP.BINARY_FILE_TYPE);
Also never forget to pass in LocalPassiveMode for download commands
mFTPClient.enterLocalPassiveMode();

How to receive data from the server?

I am trying to fetchg data from server like MP3 files, video files, etc. in my application. The application should show the list of video files received from the server.
How can I do this?
/** this function will download content from the internet */
static int writeData(String fileurl, boolean append, String path,
String filename, Activity mContext) throws CustomException {
URL myfileurl = null;
ByteArrayBuffer baf = null;
HttpURLConnection conn = null;
String mimeType="";
final int length;
try {
myfileurl = new URL(fileurl);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
conn = (HttpURLConnection) myfileurl
.openConnection();
conn.setDoInput(true);
conn.connect();
conn.setConnectTimeout(100000);
length = conn.getContentLength();
mimeType=conn.getContentType().toString();
System.out.println("Extension..."+mimeType);
if(mimeType.equalsIgnoreCase("application/vnd.adobe.adept+xml") || mimeType.equalsIgnoreCase("text/html; charset=utf-8"))
return 0;
if (length > 0) {
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
baf = new ByteArrayBuffer(1000);
int current = 0;
while ((current = bis.read()) != -1) {
try {
baf.append((byte) current);
mBufferError=false;
} catch (Exception e){
// TODO: handle exception
mBufferError=true;
e.printStackTrace();
throw new CustomException("### memory problem ", "Buffer Error");
}
}
}
} catch (IOException e) {
mBufferError=true;
e.printStackTrace();
}
try{
if(conn.getResponseCode()==200 && mBufferError==false)
{
path = path + "/" + filename;
boolean appendData = append;
FileOutputStream foutstream;
File file = new File(path);
boolean exist = false;
try {
if (appendData)
exist = file.exists();
else
exist = file.createNewFile();
} catch (IOException e) {
try {
return 1;
} catch (Exception err) {
Log.e("SAX", err.toString());
}
}
if (!appendData && !exist) {
} else if (appendData && !exist) {
} else {
try {
foutstream = new FileOutputStream(file, appendData);
foutstream.write(baf.toByteArray());
foutstream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}catch (Exception e) {
// TODO: handle exception
throw new CustomException("### I/O problem ", "I/O Error");
}
return 1;
}
once download complete search the file with extension(.3gp) for video
hope it helps
Check this link,
https://stackoverflow.com/search?q=how+to+download+mp3+%2Cvideos+from+server+in+android
Try this code
url = "your url name+filename.jpg,mp3,etc..."
FileName = "/sdcard/savefilename" // save in your sdcard
try{
java.io.BufferedInputStream in = new java.io.BufferedInputStream(new java.net.URL(url).openStream());
java.io.FileOutputStream fos = new java.io.FileOutputStream(FileName);
java.io.BufferedOutputStream bout = new BufferedOutputStream(fos,1024);
byte[] data = new byte[1024];
int x=0;
while((x=in.read(data,0,1024))>=0){
bout.write(data,0,x);
}
fos.flush();
bout.flush();
fos.close();
bout.close();
in.close();
}
catch (Exception ex)
{
}
and after you want to use MediaPlayer
and create object of mediaplayer in your activity
and play.
mp.reset();
mp.start();

Categories

Resources