I need to streaming media and video from samba server to android, and I use JCIFS to get inputstream from samba file.
But in MediaPlayer only can use fileinputstream.getFD() to play media.
mp = new MediaPlayer();
FileInputStream fis = new FileInputStream(convertedFile);
mp.setDataSource(fis.getFD());
Toast.makeText(this, "Success, Path has been set", Toast.LENGTH_SHORT).show();
mp.prepare();
mp.start();
I can't getFD() from inputstream so that I need to convert inputstream to fileinputstream...
Please give me a suggest, or there hava others solution to streaming media from samba server ?
This is my SmbFileInputStream, but it's not equals than fileinputstream
SmbFileInputStream f = new SmbFileInputStream(new SmbFile(filePath,auth));
Thanks a lot.
You can create a ParcelFileDescriptor pipe using createPipe(). You would copy bytes from your FileInputStream into an OutputStream on your end of the pipe, and give the FileDescriptor from getFileDescriptor() to MediaPlayer. Whether this will work or not with your desired stream is a completely different matter, but it's the only way I know of to get a FileDescriptor that represents arbitrary content.
Here is a sample application demonstrating this, in the context of implementing openFile() on a ContentProvider.
Related
I am using xuggler to transcode videos into different formats.
If I open my IContainer directly from a file, it works perfectly, however, this time I want to open IContainer use an InputStream. Strange thing is I tried to open a mp4 format video via FileInputStream, the IContainer can be opened using this FileInputStream, but I cannot read Ipacket in this stream. Can someone give me some suggestions? Did miss something? I really need my xuggler to deal with streams
public static void main(String[] args) throws IOException {
//the file input stream
FileInputStream fi = new FileInputStream("test.mp4");
//icontainer format
IContainerFormat format = IContainerFormat.make();
format.setInputFormat("mp4");
IContainer container = IContainer.make();
//open container via FileInputStream
int data = container.open(fi, format);
IPacket packet = IPacket.make();
//read packet
int info = container.readNextPacket(packet);
System.out.println(data);
System.out.println(info);
}
results
data: 0
info: -1094995529
According to IError, an unknown error here when I tried to read a packet. I'm using xuggler 5.4
First of all, streams must be seekable(like FSDataInputStream in hadoop), otherwise Xuggler cannot handle it, FileInputStream is not such kind of stream, sad.... Actually, seek() function is important for Xuggler to read a media file
Now, to work on seekable stream.
I have found solution, Xuggler actually can handle streams via IURLPROTOCOLHANDLER, I created my custom class which implements iurlprotocolhandler and it "works".
I learned from this Example, which use stream FSDataInputStream.
I am trying to edit existing file on the device by-First- select the file using selector that retrieve the path of the file like this "mnt/sdcard/file.png". Then I pass it to reader to read the existing file then modify it by by shifting the Ascii of every char. Then Overwrite it again to replace the old one.
I have tested the code on a desktop app on PC files and it Works perfect, but does not work as an Android app. It worked on my device once but did not work again
About what I did:
1)Add writing on External source Permission in the Mainafest file
2)Select the file right and retrieve it path
3)Read the file content true
File file = f;
FileInputStream fin;
fin = new FileInputStream(file);
byte fileContent[] = new byte[(int)file.length()];
fin.read(fileContent);
4)Modify the file bytes
5)Write back (Overwrite) in the original file
FileOutputStream fos = new FileOutputStream(f.getAbsolutePath());
fos.write(enc_msg);
fos.write((byte)seed);
fin.close();
fos.close();
6)Set the file to null again
7)Call finish() in the onClickListner
Thanks in Advance
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://" + Environment.getExternalStorageDirectory())));
However, the device must be disconnected from USB. Otherwise you need to unplug and replug the device to see the changes.
After a lot of work, I have reached the final solution by:
1)Using Common-io library
http://commons.apache.org/proper/commons-io/download_io.cgi
2)Writing this simple line only after import common-io
FileUtils.writeByteArrayToFile(new File(file.getAbsolutePath()), myByteArray, false);
the last attribute (Fasle) for overriding the file .. Append flag
I'm trying to download a file using the android DownloadManager, access the file, and write it to a new location (in this example I'm downloading a database which is compiled server side and needs to be wrote to the /database/ directory).
I've been reading up and managed to download the file, and activate the BroadcastReceiver, but at this point I get stuck.
I've returned the ParcelFileDecriptor file but I'm having trouble converting it to a stream in any way. I can't decide if the ParcelFileDecriptor.AutoCloseInputStream is a red herring or not, but I'm pretty sure the ParcelFileDecriptor has relativity to a stream, but I'm really struggling to work it out. Any help would be much appreciated.
Assuming you've started the download already and set up the Broadcast Reciver, the following code will do the job...
ParcelFileDescriptor file = dMgr.openDownloadedFile(downloadId);
File dbFile = getDatabasePath(Roads.DATABASE_NAME);
InputStream fileStream = new FileInputStream(file.getFileDescriptor());
OutputStream newDatabase = new FileOutputStream(dbFile);
byte[] buffer = new byte[1024];
int length;
while((length = fileStream.read(buffer)) > 0)
{
newDatabase.write(buffer, 0, length);
}
newDatabase.flush();
fileStream.close();
newDatabase.close();
If you're looking for more information on overwriting a database with your own check this link (Also where most of the above info has come from):
http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/
Our current project requires us to send an audio file to the server and then use the audio file for further computation.
Using the Java sound api, I was able to capture the recording and save it as a wav file in my system. Then in order to pass the audio wav to the server, I am using Apache Commons HttpClient to post a request to the server. (I am using InputstreamEntity provided by apache and sending the data as a chunk).
The problem appears when i am trying to recreate/retrieve the wav file on the server. I understand that I would have to use the AudioSystem.write API to create the wav file (exactly as what was done on my system). However what I observe is that althought the file gets created , it does not play (I am using vlc media player to test it FYI). I have searched in Google for sample codes and have tried to implement it, but is unable to play it once the file gets created.
The sample code snippets indicates the approaches i have tried:
//******************************************************************
try {
InputStream is = request.getInputStream();
FileOutputStream fs = new FileOutputStream("output123.wav");
byte[] tempbuffer = new byte[4096];
int bytesRead;
while((bytesRead=is.read(tempbuffer))!=-1)
{
fs.write(tempbuffer, 0,bytesRead);
}
is.close();
fs.close();
AudioInputStream inputStream =AudioSystem.getAudioInputStream(newFile("output123.wav"));
int numofbytes = inputStream.available();
byte[] buffer = new byte[numofbytes];
inputStream.read(buffer);
int bytesWritten = AudioSystem.write(inputStream, AudioFileFormat.Type.WAVE,new File("outputtest.wav"));
System.out.println("written"+bytesWritten);
Approach 2
InputStream is = request.getInputStream();
System.out.println("inputStream obtained : "+is.toString());
ByteArrayInputStream bais = null;
byte[] audioBuffer = IOUtils.toByteArray(is);
System.out.println(" is audioBuffer empty? : length = ? "+audioBuffer.length);
try {
AudioFileFormat ai = AudioSystem.getAudioFileFormat(is);
System.out.println("ai bytelength ? "+ai.getByteLength());
System.out.println("ai frame length = "+ai.getFrameLength());
Set<Map.Entry<String,Object>> audioProperties = ai.getFormat().properties().entrySet();
System.out.println("entry set is empty ? "+audioProperties.isEmpty());
for(Map.Entry me : audioProperties){
System.out.println("key = "+me.getKey());
System.out.println("value ="+me.getValue());}
bais = new ByteArrayInputStream(audioBuffer);
AudioInputStream ais = new AudioInputStream(bais, new AudioFormat(8000,8,2,true,true), 2);
AudioSystem.write(ais, AudioFileFormat.Type.WAVE,new File("testtest.wav"));
//*************************************************************************************
The audioFormat properties all turned out to be null. Are these null values giving the problem? So while creating the wave file on the server, I tried to set the properties manually once again. But even then the wav file would not play.
I have also tried quite a few approaches already mentioned on this site, but somehow they aren't working. I am sure i am missing something, but I am unable to pinpoint the exact problem.
Would be really helpful, if you guys can point out how to go about the conversion from ServletInputStream to getting a wav.
P.S (1) I know the code is shabby, because i have been under a trial and error situation for quite some time now. But I will give more details on the approaches if needed.
2) Apologise for the clumsiness, this happens to be my first post.. )
this is not how you copy a stream (from Approach 1). you have the correct code to copy a stream just above this.:
int numofbytes = inputStream.available();
byte[] buffer = new byte[numofbytes];
inputStream.read(buffer);
If all your server wants to do is get the data and write it to a file, then you do not need to use any of the audio API: simply treat the data as a stream of bytes.
So the part of approach 1 that is before any mention of AudioInputStream should be sufficient.
Although the approach chosen might not be the perfect solution, due to time constraints, I adopted a simpler approach. Using java.util.zip i simply zipped it up and sent it over to the server and then wrote a layer wherin the file gets unzipped . then i deleted the zip files. Seems like an immature solution (bcos the original challenge was to send the audio file). now i am incurring an overhead of zipping the files, but the file transfer would hapeen relatively faster. Thanks for your help guys.
What I'm trying to do is when a user hits a link with the correct parameters, the system will retrieve the file from the MS Server 2005 db and outputs it to the user. Specifically, I stored an audio file in varbinary data type and now I have the ID to retrieve the audio file but I don't know what is the Java command to output it for the user.
My code is written in Java and I tried to search for a similar topic on here but had no luck. Any help is appreciated.
Thanks
-Bao
There is one example for file downloading here, maybe is helpful:
http://www.daniweb.com/software-development/java/threads/154128
Maybe you can try to combine that with your link handler.
I figured it out. This is the solution that did the job for me. Basically I had to use the javax.sound.sampled.* API. This is what I did below:
InputParameters parameters = parts.getParameters();
int audioFileID = parameters.getIntParameter("audiofileID");
//Retrieves Audio File
AudioFile audioFile = CallManager.getAudioFile(audioFileID);
InputStream is = new ByteArrayInputStream(audioFile.getAudio());
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(is);
ServletOutputStream out = response.getOutputStream();
ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
AudioFormat format = audioInputStream.getFormat();
audioInputStream = AudioSystem.getAudioInputStream(format, audioInputStream);
}
AudioSystem.write(audioInputStream,javax.sound.sampled.AudioFileFormat.Type.WAVE,byteOutputStream);