Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
音乐无法在android中播放_Android_Sharedpreferences_Android Contentprovider - Fatal编程技术网

音乐无法在android中播放

音乐无法在android中播放,android,sharedpreferences,android-contentprovider,Android,Sharedpreferences,Android Contentprovider,我正在创建一个应用程序,我在原始文件夹中存储一个名为 闹钟铃声.mp3 我把它保存在一个盒子里 SharedPreferences shared=getSharedPreferences(PREFS_NAME,0); SharedPreferences.Editor editor=shared.edit(); editor.putString("music_uri", "android.resource://" + "org.riteshmapsapplicationdem

我正在创建一个应用程序,我在原始文件夹中存储一个名为 闹钟铃声.mp3

我把它保存在一个盒子里

 SharedPreferences shared=getSharedPreferences(PREFS_NAME,0);
     SharedPreferences.Editor editor=shared.edit();
     editor.putString("music_uri", "android.resource://" + "org.riteshmapsapplicationdemo/" + R.raw.alarm_tone);
    editor.commit();
在其他活动中,我使用代码段播放此音调

player=new MediaPlayer();
try {
    player.setDataSource(pref.getString("music_uri", null));
    player.prepare();
    player.start();
} catch (IllegalArgumentException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IllegalStateException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
player=new MediaPlayer();
try {
   SharedPreferences pref=getSharedPreferences(PREFS_NAME,0);
   String struri=pref.getString("music_uri", null);
  if(struri !=null){
      Uri mUri = Uri.parse(struri);
         player.setDataSource(mUri);
         player.prepare();
         player.start();
   }
  //your code here...
它正在显示 Media.prepare()失败 java.io.IOException

您应该解析Uri:

player.setDataSource(Uri.parse(pref.getString("music_uri", null)));
将代码更改为:

 SharedPreferences shared=getSharedPreferences(PREFS_NAME,0);
     SharedPreferences.Editor editor=shared.edit();
     editor.putString("music_uri", "android.resource://" +
                          this.getPackageName() + "/raw/alarm_tone");
    editor.commit();
更改播放代码段

player=new MediaPlayer();
try {
    player.setDataSource(pref.getString("music_uri", null));
    player.prepare();
    player.start();
} catch (IllegalArgumentException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IllegalStateException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
player=new MediaPlayer();
try {
   SharedPreferences pref=getSharedPreferences(PREFS_NAME,0);
   String struri=pref.getString("music_uri", null);
  if(struri !=null){
      Uri mUri = Uri.parse(struri);
         player.setDataSource(mUri);
         player.prepare();
         player.start();
   }
  //your code here...

pref
来自哪里?在第一个片段中,您调用了SharedReferences对象
shared
,您确定在实例化
pref
时获得了正确的SharedReferences吗?你能把代码贴在哪里吗?