Titanium Convert TiFile Object to TiFileSystemFile Object - iphone

How to save the recorded audio file which is recorded using sound_record.js from Titanium KickenSink Source.
We have,
var file // global variable.
file = recording.stop(); // file will have recorded content which we will convert into media.sound in order to play.
var newDir = Titanium.Filesystem.getFile(Titanium.Filesystem.resourcesDirectory,'audioclips');
Ti.API.info("Created mydir: " + newDir.createDirectory());
var newFile = Titanium.Filesystem.getFile(newDir.nativePath,'newfile.wav');
newFile.write(file.read());
But i am able to save the file which is recorded?
I am not getting how to save this recorded file, I dont knw where i am going wrong.
Please Help,
Thanks in Advance.

I am guessing your are having problem with saving the file. In last line just change
newFile.write(file.read());
to
newFile.write(file);
This should do the trick for you.
Hope this helps.
Edited:
For saving the file try this code:
var newFile =Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,'your_file.wav');
// Write the data.
newFile.write( file );

Related

Problems in descompression file - Out of memory

I have a sales app develpmented in Flex Builder. For use photos by off-line way, i upload a compressed file with all photos to the site and when need i sincronize with my smartphone. In that process is made a download to the dispositive and exist a responsable plugin to descompress the archive and disponibilize it in apropriate folder to later use.
That compressed file has 500MB size - 6700 photos approximately - and in Flex i have no type of problem to do that.
I'm rewriting the app in Flutter, using a archive package, dont getting the same results. In the descompress process i have problem with Out of memory.
Somebody already faced for something like that ?
Is there an best way to do this ?
I already tried other two alternatives:
- To use the Image.network, but, one of requires is work off-line.
- To save all the photos in assets folder, but i think that is not the best alternative, because the app will get bigger
Thank you in advance for.
Follow the error message and the code
code:
unarchiveAndSave()async{
var zippedFile = await initDir();
var bytes = zippedFile.readAsBytesSync();
var archive = ZipDecoder().decodeBytes(bytes);
for (var file in archive) {
var fileName = '$_dir/${file.name}';
if (file.isFile) {
var outFile = File(fileName);
print('File:: ${outFile.path}');
outFile = await outFile.create();
await outFile.writeAsBytes(file.content);
}
}
print('terminei de descompactar os arquivos');
}

Swift 3 Mac OS Copy Audio File to NSPasteboard

I am trying to copy an audio file to the NSPasteboard so that it can be pasted somewhere else on the computer or into another program like Ableton or Pro Tools. Here is how I am getting the url of the file. (An example url d after casting to string is: file:///Users/ben/Music/Ableton/User%20Library/Vox.wav )
let url = directoryItems?[tableview.selectedRow].url
let urlString = (url?.absoluteString)! as String
let pb = NSPasteboard.general()
let pasted = pb.writeFileContents(urlString)
It is not being copied to the pasteboard (pasted is set to false) and I can not find any resources that talk about writing audio files to the NSPasteboard. Any help would be greatly appreciated.
Edit:
I also tried using the url instead of the string and had the same outcome
let pb = NSPasteboard.general()
let pasted = pb.writeObjects([url as! NSPasteboardWriting])
To copy a file in such a way that you can paste it in the Finder, you'd want a file URL, not a string.
As for copying the music into a music editor, presumably you'd need to load the music file yourself into some waveform format that can be pasted into that editor.

Lua HTTP download and save to file

I'm trying to download and then save the contents to a xml file. The code I got is:
local filePath = currentDir().."/file.xml"
local http = require("socket.http")
local xFile = io.open(filePath, "w")
local save = ltn12.sink.file(xFile)
http.request{addr, sink = save }
print("Done!")
It runs but the file is still empty. Can I get some help here please?
Its a syntactical mistake. You mixed two styles of calling http.request. Use
http.request{url = addr, sink = save }

How to delete a file while the application is running

I have developed a C# application, in the application the users choose a photo for each record. However the user should also be able to change the pre-selected photo with a newer one. When the user changes the photo the application first deletes the old photo from the application directory then copies the new photo, but when it does that the application gives an exception because the file is used by the application so it cannot be deleted while the application is running. Does any one have a clue how to sort this out? I appreciate your help
This is the exception
The process cannot access the file
'D:\My
Projects\Hawkar'sProject\Software\Application\bin\Debug\Photos\John
Smith.png' because it is being used by
another process.
//defining a string where contains the file source path
string fileSource = Open.FileName;
//defining a string where it contains the file name
string fileName = personNameTextBox.Text + ".png" ;
//defining a string which specifies the directory of the destination file
string fileDest = dir + #"\Photos\" + fileName;
if (File.Exists(fileDest))
{
File.Delete(fileDest);
//this is a picturebox for showing the images
pbxPersonal.Image = Image.FromFile(dir + #"\Photos\" + "No Image.gif");
File.Copy(fileSource, fileDest);
}
else
{
File.Copy(fileSource, fileDest);
}
imageIDTextBox.Text = fileDest;
First of all, you code is not good.
The new image is only copied if there is currently no image (else).
But if there is an old image, you only delete this image, but never copy the newer one (if).
The code should better look like this:
if (File.Exists(fileDest))
{
File.Delete(fileDest);
}
File.Copy(fileSource, fileDest);
imageIDTextBox.Text = fileDest;
This code should work, but if you are getting an exception that the file is already in use, you should check "where" you use the file. Perhaps you are reading the file at program start. Check all parts of your program you are accessing these user files if there are some handles open.
Thanks a lot for your help and sorry for the mistake in the code I just saw it, my original code is just like as you have written but I don't know maybe when I posted accidentally I've put like this. when the application runs there is a picturebox which the image of each record is shown, that is why the application is giving an exception when I want to change the picture because it has been used once by the picturebox , however I've also tried to load another picture to the picture box before deleting the original one but still the same. I've modified the above could if you want to examine it

Playing a Sound With Monotouch

No matter what I try (build -> content, NSUrl, filename) I get a 'null exception': file not found when I try to play a .caf sound file in monotouch.
//var path = NSBundle.MainBundle.PathForResource("MatchGame", "caf");
//var gameSong = SystemSound.FromFile( new NSUrl(path, false));
var gameSong = SystemSound.FromFile("MatchGame.caf");
gameSong.PlaySystemSound();
I also try combinations using the folder name "images/MatchGame.caf" and moving MatchGame.caf into the root folder.
What am I missing? Thanks a lot.
Here is a link to a video of adding the sound in monotouch. http://www.screencast.com/t/MmE0ZmFh What is wrong?
Bryan,
From looking at your screencast - you are trying to play a mp3 file and not a caf file. Mp3 files are encoded differently and will not play with the SystemSound class that's there (I can't remember if you can do this in Obj-C or not.)
You'll want to use the AVFoundation Namespace and AVAudioPlayer class.
using Monotouch.AVFoundation;
var mediaFile = NSUrl.FromFilename("myMp3.mp3");
var audioPlayer = AVAudioPlayer.FromUrl(mediaFile);
audioPlayer.FinishedPlaying += delegate { audioPlayer.Dispose(); };
audioPlayer.Play();
You might need to tweak the code above - I don't have MonoDevelop to hand but that should help you a little further.
Cheers,
ChrisNTR
You need the first line:
var path = NSBundle.MainBundle.PathForResource("MatchGame", "caf");
Then make sure that your audio file is included in the application by making sure that your CAF file is flagged as "Content" in the Properties pane, otherwise the file is not copied to the resulting application package (your .app)
You can follow the steps documented here (they are for images, but apply the same to audio files):
http://wiki.monotouch.net/HowTo/Images/Add_an_Image_to_your_Project
Additionally, this is a good resource for where you should store files:
http://wiki.monotouch.net/HowTo/Files/HowTo%3a_Store_Files