如何将MP3设置为铃声

时间:2013-07-10 12:26:14

标签: android media-player

我正在编写一个应用程序,其中我正在显示来自Raw文件夹的MP3歌曲列表,并允许用户使用点击按钮播放歌曲,同样我希望允许用户将歌曲设置为铃声,哪一个用户点击了列表按钮,请参见下面的屏幕截图:

MP3歌曲列表:

enter image description here

在PlayActivity.java中显示所选歌曲:

enter image description here

PlayActivity:

                // Set as Ringtone
        btn5.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {                               
         Uri path = Uri.parse("android.resource://com.applenty/raw/");
         RingtoneManager.setActualDefaultRingtoneUri(
         getApplicationContext(), RingtoneManager.TYPE_RINGTONE, path);
         Log .i("Ringtone", "Ringtone Set to Resource:: "+ path.toString());
         RingtoneManager.getRingtone(getApplicationContext(), path).play();

            }
        });

logcat的:

07-10 12:06:05.569: D/dalvikvm(958): GC_FOR_ALLOC freed 37K, 7% free 2486K/2648K, paused 130ms, total 147ms
07-10 12:06:05.630: I/dalvikvm-heap(958): Grow heap (frag case) to 3.153MB for 635812-byte allocation
07-10 12:06:05.719: D/dalvikvm(958): GC_FOR_ALLOC freed 1K, 6% free 3105K/3272K, paused 90ms, total 90ms
07-10 12:06:05.869: D/dalvikvm(958): GC_CONCURRENT freed <1K, 5% free 3121K/3272K, paused 4ms+23ms, total 137ms
07-10 12:06:05.939: D/data count(958): 5//
07-10 12:06:05.979: D/data count(958): 5//
07-10 12:06:05.979: D/data count(958): 5//
07-10 12:06:06.189: D/data count(958): 5//
07-10 12:06:06.210: D/data count(958): 5//
07-10 12:06:06.319: D/data count(958): 5//
07-10 12:06:06.350: D/data count(958): 5//
07-10 12:06:06.519: D/data count(958): 5//
07-10 12:06:06.779: I/Choreographer(958): Skipped 163 frames!  The application may be doing too much work on its main thread.
07-10 12:06:06.800: D/data count(958): 5//
07-10 12:06:06.800: D/data count(958): 5//
07-10 12:06:06.889: D/data count(958): 5//
07-10 12:06:06.909: D/data count(958): 5//
07-10 12:06:06.929: D/data count(958): 5//
07-10 12:06:06.979: D/gralloc_goldfish(958): Emulator without GPU emulation detected.
07-10 12:06:07.439: D/data count(958): 5//
07-10 12:06:07.449: D/data count(958): 5//
07-10 12:06:07.579: D/data count(958): 5//
07-10 12:06:07.579: D/data count(958): 5//
07-10 12:06:07.689: D/data count(958): 5//
07-10 12:07:59.060: D/InputEventConsistencyVerifier(958): KeyEvent: ACTION_UP but key was not down.
07-10 12:07:59.060: D/InputEventConsistencyVerifier(958):   in android.view.ViewRootImpl@40ce0838
07-10 12:07:59.060: D/InputEventConsistencyVerifier(958):   0: sent at 214631000000, KeyEvent { action=ACTION_UP, keyCode=KEYCODE_MENU, scanCode=229, metaState=0, flags=0x8, repeatCount=0, eventTime=214631, downTime=214548, deviceId=0, source=0x101 }
07-10 12:07:59.939: D/data count(958): 5//
07-10 12:07:59.939: D/data count(958): 5//
07-10 12:08:00.050: D/data count(958): 5//
07-10 12:08:00.060: D/data count(958): 5//
07-10 12:08:00.119: D/data count(958): 5//
07-10 12:08:08.449: D/dalvikvm(958): GC_CONCURRENT freed 36K, 5% free 3573K/3736K, paused 15ms+58ms, total 346ms
07-10 12:08:13.849: I/Choreographer(958): Skipped 120 frames!  The application may be doing too much work on its main thread.
07-10 12:08:22.159: I/Ringtone(958): Ringtone Set to Resource:: android.resource://com.applenty/raw/
07-10 12:08:22.190: D/MediaPlayer(958): Couldn't open file on client side, trying server side
07-10 12:08:22.210: E/MediaPlayer(958): error (1, -2147483648)
07-10 12:08:22.219: D/Ringtone(958): Problem opening; delegating to remote player
07-10 12:08:55.779: I/Ringtone(958): Ringtone Set to Resource:: android.resource://com.applenty/raw/
07-10 12:08:55.779: D/MediaPlayer(958): Couldn't open file on client side, trying server side
07-10 12:08:55.799: E/MediaPlayer(958): error (1, -2147483648)
07-10 12:08:55.799: D/Ringtone(958): Problem opening; delegating to remote player
07-10 12:08:57.089: I/Ringtone(958): Ringtone Set to Resource:: android.resource://com.applenty/raw/
07-10 12:08:57.100: D/MediaPlayer(958): Couldn't open file on client side, trying server side
07-10 12:08:57.109: E/MediaPlayer(958): error (1, -2147483648)
07-10 12:08:57.119: D/Ringtone(958): Problem opening; delegating to remote player
07-10 12:09:02.779: I/Ringtone(958): Ringtone Set to Resource:: android.resource://com.applenty/raw/
07-10 12:09:02.789: D/MediaPlayer(958): Couldn't open file on client side, trying server side
07-10 12:09:02.799: E/MediaPlayer(958): error (1, -2147483648)
07-10 12:09:02.809: D/Ringtone(958): Problem opening; delegating to remote player
07-10 12:09:07.239: I/Ringtone(958): Ringtone Set to Resource:: android.resource://com.applenty/raw/
07-10 12:09:07.249: D/MediaPlayer(958): Couldn't open file on client side, trying server side
07-10 12:09:07.269: E/MediaPlayer(958): error (1, -2147483648)
07-10 12:09:07.269: D/Ringtone(958): Problem opening; delegating to remote player
07-10 12:09:08.589: I/Ringtone(958): Ringtone Set to Resource:: android.resource://com.applenty/raw/
07-10 12:09:08.589: D/MediaPlayer(958): Couldn't open file on client side, trying server side

3 个答案:

答案 0 :(得分:5)

假设路径正确,这是您可以将歌曲设置为从this post获取的铃声的一种方式:

File k = new File(path, "mysong.mp3"); // path is a file playing

ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, "My Song title"); //You will have to populate 
values.put(MediaStore.MediaColumns.SIZE, 215454);
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
values.put(MediaStore.Audio.Media.ARTIST, "Band Name"); //You will have to populate this
values.put(MediaStore.Audio.Media.DURATION, 230);
values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
values.put(MediaStore.Audio.Media.IS_ALARM, false);
values.put(MediaStore.Audio.Media.IS_MUSIC, false);

//Insert it into the database
Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath());
Uri newUri = main.getContentResolver().insert(uri, values);

RingtoneManager.setActualDefaultRingtoneUri(
  myActivity,
  RingtoneManager.TYPE_RINGTONE,
  newUri
);

答案 1 :(得分:4)

试试这个,这对你有好处

File newSoundFile = new File("pathFile");
        if (DebugUtil.DEBUG) {
            DebugUtil.logError(new Exception(), newSoundFile.getAbsolutePath());
        }
        ContentValues values = new ContentValues();
        values.put(MediaStore.MediaColumns.DATA, newSoundFile.getAbsolutePath());
        values.put(MediaStore.MediaColumns.TITLE, songVO.getTitle());
        values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");
        values.put(MediaStore.MediaColumns.SIZE, newSoundFile.length());
        values.put(MediaStore.Audio.Media.ARTIST, R.string.app_name);
        values.put(MediaStore.Audio.Media.DURATION, 230);
        values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
        values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
        values.put(MediaStore.Audio.Media.IS_ALARM, false);
        values.put(MediaStore.Audio.Media.IS_MUSIC, false);

        Uri uri = MediaStore.Audio.Media.getContentUriForPath(newSoundFile.getAbsolutePath());
        context.getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + newSoundFile.getAbsolutePath() + "\"", null);
        Uri newUri = context.getContentResolver().insert(uri, values);

        try {
            RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE, newUri);
            KitKatToast.makeText(context, R.string.msg_setAsRingTuneSuccess, KitKatToast.LENGTH_LONG).show();
        }
        catch (Exception e) {
            if (DebugUtil.DEBUG) {
                DebugUtil.logError(new Exception(), e.toString());
            }
        }

答案 2 :(得分:-1)

试试这个:

这是点击mec

btn5.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {                               
            MediaPlayer mPlayer = MediaPlayer.create(FakeCallScreen.this, R.raw.mysoundfile);
            mPlayer.start();
        }
});

public void onDestroy() {
    mPlayer.stop();
    super.onDestroy();
}

在清单文件中添加权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />