i have a problem with the MediaMetadataRetriever setdatasource method. i created a simple project. the main.xml has only a textview, which should display the paramter of the music.mp3. the mp3 is in the project raw file. the mainActivity looks like:
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tvMain = (TextView)findViewById(R.id.textView_songParam);
// load data file
MediaMetadataRetriever metaRetriever = new MediaMetadataRetriever();
metaRetriever.setDataSource(R.raw.music);
String out = "";
// get mp3 info
out += metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE);
out += "\n";
out += metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST);
out += "\n";
//out += metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM);
//out += "\n";
//out += metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_YEAR);
//out += "\n";
// convert duration to minute:seconds
String duration = metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
long dur = Integer.parseInt(duration);
String seconds = String.valueOf(dur % 60);
String minutes = String.valueOf(dur / 60000);
out += "Length: [ " + minutes + "m" + seconds + "s ]\n";
// close object
metaRetriever.release();
// display output
tvMain.setText(out);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
what i am doing wrong here in the line, where i set the data source?? i dont understand that?
You are passing an integer to setDataSource because R.raw.music returns the resource id related to the file, not the path to the file. You need to pass path or file descriptor as an argument to setDataSource. Try this code: