本文介绍了如何设置MP3为铃声的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个应用程序中,我显示从原始文件夹中的MP3歌曲的列表,并允许用户通过点击按钮来播放歌曲,以同样的方式我想允许用户设置歌曲作为铃声,其中一个用户点击一个列表按钮,请参见下面的屏幕截图:

MP3歌曲名单:

显示所选歌曲在PlayActivity.java:

PlayActivity:

  //设置为铃声
        btn5.setOnClickListener(新View.OnClickListener(){
            公共无效的onClick(视图v){
         URI路径= Uri.parse(android.resource://com.applenty/raw/);
         RingtoneManager.setActualDefaultRingtoneUri(
         getApplicationContext(),RingtoneManager.TYPE_RINGTONE,路径);
         登录。我(铃声,铃声设置为资源::+ path.toString());
         RingtoneManager.getRingtone(getApplicationContext(),路径).play();            }
        });

logcat的:

  12月7日至10日:06:05.569:D / dalvikvm(958):GC_FOR_ALLOC释放37K,7%的游离2486K / 2648K,暂停130MS,总147ms
十二月7日至10日:06:05.630:I / dalvikvm堆(958):成长堆(frag的情况下),以3.153MB为635812字节分配
十二月7日至10日:06:05.719:D / dalvikvm(958):GC_FOR_ALLOC释放1K,6%免费3105K / 3272K,暂停90毫秒,90毫秒总
十二月7日至10日:06:05.869:D / dalvikvm(958):GC_CONCURRENT释放< 1K,5%的游离3121K / 3272K,暂停为4ms + 23MS,总137ms
07-10 12:06:05.939:D /数据计数(958):5 //
07-10 12:06:05.979:D /数据计数(958):5 //
07-10 12:06:05.979:D /数据计数(958):5 //
07-10 12:06:06.189:D /数据计数(958):5 //
07-10 12:06:06.210:D /数据计数(958):5 //
07-10 12:06:06.319:D /数据计数(958):5 //
07-10 12:06:06.350:D /数据计数(958):5 //
07-10 12:06:06.519:D /数据计数(958):5 //
十二月7日至10日:06:06.779:I /编舞(958):跳过163帧!该应用程序可能会做它的主线程的工作太多了。
07-10 12:06:06.800:D /数据计数(958):5 //
07-10 12:06:06.800:D /数据计数(958):5 //
07-10 12:06:06.889:D /数据计数(958):5 //
07-10 12:06:06.909:D /数据计数(958):5 //
07-10 12:06:06.929:D /数据计数(958):5 //
十二月7日至10日:06:06.979:D / gralloc_goldfish(958):无仿真模拟GPU检测。
07-10 12:06:07.439:D /数据计数(958):5 //
07-10 12:06:07.449:D /数据计数(958):5 //
十二月7日至10日:06:07.579:D /数据数(958):5 //
十二月7日至10日:06:07.579:D /数据数(958):5 //
07-10 12:06:07.689:D /数据计数(958):5 //
十二月7日至10日:07:59.060:D / InputEventConsistencyVerifier(958):KeyEvent的:ACTION_UP但关键是不下来。
十二月7日至10日:07:59.060:D / InputEventConsistencyVerifier(958):在android.view.ViewRootImpl@40ce0838
十二月7日至10日:07:59.060:D / InputEventConsistencyVerifier(958):0:2146.31亿派,KeyEvent的{行动= ACTION_UP,关键code = KEY code_MENU,扫描code = 229,亚状态= 0,旗帜= 0x8中,的repeatCount = 0,EVENTTIME = 214631,停工= 214548,DEVICEID = 0,源= 0x101}
十二月7日至10日:07:59.939:D /数据数(958):5 //
十二月7日至10日:07:59.939:D /数据数(958):5 //
07-10 12:08:00.050:D /数据计数(958):5 //
07-10 12:08:00.060:D /数据计数(958):5 //
07-10 12:08:00.119:D /数据计数(958):5 //
十二月7日至10日:08:08.449:D / dalvikvm(958):GC_CONCURRENT释放36K,5%的游离3573K / 3736K,暂停为15ms + 58ms,总346ms
十二月7日至10日:08:138.49:I /编舞(958):跳过120帧!该应用程序可能会做它的主线程的工作太多了。
十二月7日至10日:08:22.159:I /铃声(958):铃声设置为资源:: android.resource://com.applenty/raw/
十二月7日至10日:08:22.190:D / MediaPlayer的(958):可以在客户端无法打开文件,试图服务器端
十二月7日至10日:08:22.210:E / MediaPlayer的(958):错误(1,-2147483648)
十二月7日至10日:08:22.219:D /铃声(958):开放的问题;委托给远程播放器
十二月7日至10日:08:55.779:I /铃声(958):铃声设置为资源:: android.resource://com.applenty/raw/
十二月7日至10日:08:55.779:D / MediaPlayer的(958):可以在客户端无法打开文件,试图服务器端
十二月7日至10日:08:55.799:E / MediaPlayer的(958):错误(1,-2147483648)
十二月7日至10日:08:55.799:D /铃声(958):开放的问题;委托给远程播放器
十二月7日至10日:08:57.089:I /铃声(958):铃声设置为资源:: android.resource://com.applenty/raw/
十二月7日至10日:08:57.100:D / MediaPlayer的(958):可以在客户端无法打开文件,试图服务器端
十二月7日至10日:08:57.109:E / MediaPlayer的(958):错误(1,-2147483648)
十二月7日至10日:08:57.119:D /铃声(958):开放的问题;委托给远程播放器
十二月7日至10日:09:02.779:I /铃声(958):铃声设置为资源:: android.resource://com.applenty/raw/
十二月7日至10日:09:02.789:D / MediaPlayer的(958):可以在客户端无法打开文件,试图服务器端
十二月7日至10日:09:02.799:E / MediaPlayer的(958):错误(1,-2147483648)
十二月7日至10日:09:02.809:D /铃声(958):开放的问题;委托给远程播放器
十二月7日至10日:09:07.239:I /铃声(958):铃声设置为资源:: android.resource://com.applenty/raw/
十二月7日至10日:09:07.249:D / MediaPlayer的(958):可以在客户端无法打开文件,试图服务器端
十二月7日至10日:09:07.269:E / MediaPlayer的(958):错误(1,-2147483648)
十二月7日至10日:09:07.269:D /铃声(958):开放的问题;委托给远程播放器
十二月7日至10日:09:08.589:I /铃声(958):铃声设置为资源:: android.resource://com.applenty/raw/
十二月7日至10日:09:08.589:D / MediaPlayer的(958):可以在客户端无法打开文件,试图服务器端


解决方案

假设路径是正确的,这是一种方式可以将歌曲设置为从this帖子:

 文件k =新的文件(路径,mysong.mp3); //路径是一个文件播放ContentValues​​值=新ContentValues​​();
values​​.put(MediaStore.MediaColumns.DATA,k.getAbsolutePath());
values​​.put(MediaStore.MediaColumns.TITLE,我的歌曲标题); //你必须填写
values​​.put(MediaStore.MediaColumns.SIZE,215454);
values​​.put(MediaStore.MediaColumns.MIME_TYPE,音频/ MP3);
values​​.put(MediaStore.Audio.Media.ARTIST,乐队的名字); //你将不得不填充此
values​​.put(MediaStore.Audio.Media.DURATION,230);
values​​.put(MediaStore.Audio.Media.IS_RINGTONE,真);
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(k.getAbsolutePath());
乌里newUri = main.getContentResolver()插入(URI,价值观)。RingtoneManager.setActualDefaultRingtoneUri(
  myActivity,
  RingtoneManager.TYPE_RINGTONE,
  newUri
);

I am writing an app in which i am showing list of MP3 Songs from Raw folder, and allowing user to Play song by using click on button, in a same way i want to allow user to set song as Ringtone, which one user has clicked in a List Button, please see below screen shot:

List of MP3 Songs:

Showing selected song in PlayActivity.java:

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
解决方案

Assuming that the path is correct this is one way you can set the song to a ringtone taken from 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
);

这篇关于如何设置MP3为铃声的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 18:00