I am getting a duration of my audio file , convert it to int , then convert int to string and then string to something like this 00:32 . Converted string is correct, but then is a problem with convert that string to data
Here is my code:
File file = new File(Environment.getExternalStorageDirectory()+"/MyImages/.audio1.wav");
MediaPlayer mp = new MediaPlayer();
FileInputStream fs = null;
FileDescriptor fd = null;
try {
fs = new FileInputStream(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fd = fs.getFD();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
mp.setDataSource(fd);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int length = mp.getDuration()/1000;
mp.release();
String audiotime = String.valueOf(length);
SimpleDateFormat sdf = new SimpleDateFormat("mmss");
try {
Date d = sdf.parse(audiotime);
textView1.setText(audiotime);
} catch (ParseException ex) {
}
Two things, most important, you use the original String as text for your TextView. You should do something like textView1.setText(d.toString());
Secondly, you should use "mm:ss" as your format, instead of "mmss".
Note you could also use the format function of SimpleDateFormat. This will return a StringBuffer, which might be more appropriate since you only need the String representation
source: android documentation
Related
I am extracting metadata information (Exif infor) by using Id3 library in android, but I am getting null pointer exception and getting null in log Message it is a reason because value in src_set is null .I am not getting why it is returning null. my code is :
File src = new File(pathdata);
MusicMetadataSet src_set = null;
try {
src_set = new MyID3().read(src);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} // read metadata
if (src_set == null) // perhaps no metadata
{
Log.i("NULL", "NULL");
}
else
{
try{
IMusicMetadata metadata = src_set.getSimplified();
String artist = metadata.getArtist();
String album = metadata.getAlbum();
String song_title = metadata.getSongTitle();
Number track_number = metadata.getTrackNumber();
Log.i("artist", artist);
Log.i("album", album);
}catch (Exception e) {
e.printStackTrace();
}
File dst = new File(pathdata);
MusicMetadata meta = new MusicMetadata("name");
meta.setAlbum("Chirag");
meta.setArtist("CS");
try {
new MyID3().write(src, dst, src_set, meta);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ID3WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // write updated metadata
}
I'm trying to save a custom object when I click a menu item. The problem is its crashing. Before we start, when I try to read that data in another activity, that also crashes. Here is my code where I'm trying to save the data:
Here's a pastebin link.
my write function in the menu
myInfo.setOnMenuItemClickListener(new OnMenuItemClickListener()
{
public boolean onMenuItemClick(MenuItem item)
{
Intent ourIntent = new Intent(Results.this, listTimes.class);
ourIntent.putExtra("Meeting", meetingObj);
try {
fos = new FileOutputStream(filename);
oos = new ObjectOutputStream(fos);
oos.writeObject(businesses.get(positionChecked));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
oos.close();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//startActivity(ourIntent);
return true;
}
// My read function
private void getData()
{
try {
fis = openFileInput(filename);
ois = new ObjectInputStream(fis);
business = (Business) ois.readObject();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (StreamCorruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
ois.close();
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
How to read the particular data from the internal storage file.
For eg., I have stored
1. Device 2. Time(epoch format) 3. button text
CharSequence cs =((Button) v).getText();
t = System.currentTimeMillis()/1000;
s = cs.toString();
buf = (t+"\n").getBytes();
buf1 = (s+"\n").getBytes();
try {
FileOutputStream fos = openFileOutput(Filename, Context.MODE_APPEND);
fos.write("DVD".getBytes());
fos.write(tab.getBytes());
fos.write(buf);
fos.write(tab.getBytes());
fos.write(buf1);
//fos.write(tab.getBytes());
//fos.write((R.id.bSix+"\n").getBytes());
fos.write(newline.getBytes());
//fos.flush();
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
then while reading, how can we read only price from the file? (using fos.read())
Thanks
I would suggest writing the file in a more structured way, like this:
long t = System.currentTimeMillis()/1000;
String s = ((Button) v).getText();
DataOutputStream dos = null;
try {
dos = new DataOutputStream(openFileOutput(Filename, Context.MODE_APPEND));
dos.writeUTF("DVD");
dos.writeLong(t); // Write time
dos.writeUTF(s); // Write button text
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (dos != null) {
try {
dos.close();
} catch (IOException e) {
// Ignore
}
}
}
To read it back, something like this:
DataInputStream dis = null;
try {
dis = new DataInputStream(openFileInput(Filename));
String dvd = dis.readUTF();
long time = dis.readLong();
String buttonText = dis.readUTF();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (dis != null) {
try {
dis.close();
} catch (IOException e) {
// Ignore
}
}
}
error :
Couldn't open file on client side, trying server side
Unable to to create media player
protected void onListItemClick(ListView l, View v, int position, long id) {
//get selected items
String selectedValue = (String) getListAdapter().getItem(position);
try {
MediaPlayer objMediaPlayer = new MediaPlayer();
objMediaPlayer = new MediaPlayer();
objMediaPlayer.setDataSource("http://192.168.1.3:3000/songs/WakaWaka.mp3");
objMediaPlayer.prepare();
objMediaPlayer.start();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Check the Android Media Player State Diagram.
You have to call reset() e prepare() before calling start().
Try this:
private MediaPlayer objMediaPlayer = new MediaPlayer();
objMediaPlayer = new MediaPlayer();
try {
objMediaPlayer.setDataSource(path);
objMediaPlayer.prepare();
objMediaPlayer.start();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
This may help some people.. in order to stream a video from online, you must
add to 'AndroidManifest.xml'
<uses-permission android:name="android.permission.INTERNET" />
i went thru your problem. But couldn't get much of it. One thing for sure is, the url what you have specified here doesn't exist. So I made use of my own and did a sample. Check it out,
mediaPlayer mp=new MediaPlayer();
try {
mp.setDataSource("http://182.71.230.252/developers/blind_willie.mp3");
mp.prepare();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.start();
I want play streaming radio( .m3u format ), but i do not know how do it.
This example how i try playing:
final MediaPlayer mp = new MediaPlayer();
try {
mp.setDataSource("url.m3u");
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
mp.prepare();
mp.start();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this code does not work.
help please.
You have to download the M3U file first. It's just a text file, read it line by line. Each line will have a link which you can read in your media player.
Use something like this,
public ArrayList<String> readURLs(String url) {
if(url == null) return null;
ArrayList<String> allURls = new ArrayList<String>();
try {
URL urls = new URL(url);
BufferedReader in = new BufferedReader(new InputStreamReader(urls
.openStream()));
String str;
while ((str = in.readLine()) != null) {
allURls.add(str);
}
in.close();
return allURls ;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
I had the same issue with streaming radio. But in my case I've just removed .m3u from url and it worked!
Try to do this:
mp.setDataSource("url");
instead
mp.setDataSource("url.m3u");