0

我能够将 .mp3 文件剪切到所需的秒数(仅相差 1 或 2 秒)。但是当我在剪切后播放这个文件时,我仍然通过“mediaplayer.getduration();”获得它的旧持续时间。

例如。让我的歌曲时长为 4 分 6 秒,它从 0 秒缩短到 1 分钟。然后当我播放这个时,我通过 mediaplayer.getduration() 得到它的时间 4 分 6 秒,歌曲在 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())));
4

1 回答 1

0

Try rescanning the media

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse
("file://" + Environment.getExternalStorageDirectory() + "/media/audio/music")));
于 2012-12-16T14:39:36.437 回答