我能够将.mp3文件剪切到所需的秒数(仅相差1或2秒)。 但是当我在切割后播放这个文件时,我仍然可以使用它的旧时间 “mediaplayer.getduration();”
EG。让我的歌曲持续时间为4分6秒,并将其从0秒切换到1分钟。 然后当我玩这个时,我得到它的时间4分6秒由mediaplayer.getduration()和歌曲停止播放58秒或59秒或1分钟我设置其他细节也像我设置艺术家名称“麦当娜”但得到“未知”。
所以请帮助我如何在切割时设置正确的时间并设置其他细节。
这是我的代码:
path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC);
String str = "xyz3.mp3";
File f = new File(path, str); // new file
File file = new File("/sdcard/media/audio/music/mysong.mp3") //i am cutting this song
CheapSoundFile csf = CheapSoundFile.create(file.getAbsolutePath(), listener );
int mSampleRate = csf.getSampleRate();
int mSamplesPerFrame = csf.getSamplesPerFrame();
int duration = (int)(seekBar.getSelectedMaxValue() - seekBar.getSelectedMinValue()+0.5);
int startframe = (int)(1.0 * (double)(seekBar.getSelectedMinValue()/1000) * mSampleRate / mSamplesPerFrame + 0.5);
int endframe = (int)(1.0 * (double)(seekBar.getSelectedMaxValue()/1000) * mSampleRate / mSamplesPerFrame + 0.5);
csf.WriteFile(f, startframe, endframe - startframe);
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, f.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, "My Song title");
values.put(MediaStore.MediaColumns.SIZE, f.length());
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
values.put(MediaStore.Audio.Media.ARTIST, "Madonna");
values.put(MediaStore.Audio.Media.DURATION, duration);
values.put(MediaStore.Audio.Media.IS_RINGTONE, false);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
values.put(MediaStore.Audio.Media.IS_ALARM, false);
values.put(MediaStore.Audio.Media.IS_MUSIC, true);
//Insert it into the database
Uri uri = MediaStore.Audio.Media.getContentUriForPath(f.getAbsolutePath());
getContentResolver().insert(uri, values);
sendBroadcast(new Intent("android.intent.action.MEDIA_MOUNTED",Uri.parse("file://" + Environment.getExternalStorageDirectory())));
答案 0 :(得分:0)
尝试重新扫描媒体
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse
("file://" + Environment.getExternalStorageDirectory() + "/media/audio/music")));