设置数组以保存Android应用中的声音

时间:2011-12-28 19:35:24

标签: android arrays audio save

大家好,我一直在寻找一段时间,并且无法找到我的问题的答案。我是Android编程的新手,在大学里学习了一些但不多的Java,但是学习了其他语言,所以我理解了概念。我问我如何实现我在保存功能中创建的数组。我以为我知道如何,但只是通过saveasRing(0)我的数组包含原始值,但我收到一个力关闭,这就是我要问的原因。这是我的代码。

我不是要求别人写我的代码,而是向我解释要做什么......我想学习,希望我只是忽略了一些东西,但如果没有,请引导我走向正确的方向。 谢谢!

String[] name ={ "a1", "a", "b","c"}; //code shortened
int[] sounds = {R.raw.a1, R.raw.a };  //code shortened for ease of reading

public void function1(int id){  // when I change int to int[] Eclipse wants me 
                                // to change alot of things and then the app will 
                                // force close on start
  saveasRing(sounds);           // right here I have tried passing "0" instead 
                                // of sounds, a no go.

  Toast.makeText(this, "Saved as Ringtone", Toast.LENGTH_SHORT).show();  
}//Closes Function 1  

//Save as Ringtone Coding for Function 3  
public boolean saveasRing(int ressound){  
  byte[] buffer=null;  
  InputStream fIn = getBaseContext().getResources().openRawResource(ressound);  
  int size=0;  

  try {  
    size = fIn.available();  
    buffer = new byte[size];  
    fIn.read(buffer);  
    fIn.close();  
  } 
  catch (IOException e) {  
    // TODO Auto-generated catch block  
    return false;  
  }  

  String path="/sdcard/media/audio/ringtones/";  
  String filename= name +".mp3";  

  boolean exists = (new File(path)).exists();  
  if (!exists){new File(path).mkdirs();  }  

  FileOutputStream save;  
  try {  
    save = new FileOutputStream(path+filename);  
    save.write(buffer);  
    save.flush();  
    save.close();  
  } 
  catch (FileNotFoundException e) {  
    // TODO Auto-generated catch block  
    return false;  
  } 
  catch (IOException e) {  
    // TODO Auto-generated catch block  
    return false;  
  }      

  sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
                Uri.parse("file://"+path+filename)));  

  File k = new File(path, filename);  
  ContentValues values = new ContentValues();  
  values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());  
  values.put(MediaStore.MediaColumns.TITLE, "dosequis");  
  values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");  
  values.put(MediaStore.Audio.Media.ARTIST, "Most Interesting Man ");  
  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  
  this.getContentResolver().insert(MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath()), values);  

  return true;  
}
//Closes Save as Ringtone 

这是我长时间点击铃声时会发生什么的Logcat。上下文菜单很好,就在我选择要保存的类型时,因为我得到一个强制关闭和这些错误。再次感谢您的时间。

如果有更好的格式化方式,请告诉我。

 W/ResourceType(225):No package identifier when getting name for resource number 0x00000000

D/AndroidRuntime(225):Shutting down VM

W/dalvikvm(225): threadid=3: thread exiting with uncaught exception (group=0x4001b188)

E/AndroidRuntime(225):Uncaught handler: thread main exiting due to uncaught exception

E/AndroidRuntime(225):android.content.res.Resources$NotFoundException: Resource ID #0x0

E/AndroidRuntime(225):at android.content.res.Resources.getValue(Resources.java:891)

E/AndroidRuntime(225):at Android.content.res.Resources.openRawResource(Resources.java:816)

E/AndroidRuntime(225):at android.content.res.Resources.openRawResource(Resources.java:798)

E/AndroidRuntime(225):at tomcavell.app.DosEquisSoundboardActivity.saveasRing(DosEquisSoundboardActivity.java:270)

E/AndroidRuntime(225):at tomcavell.app.DosEquisSoundboardActivity.function1(DosEquisSoundboardActivity.java:253)

E/AndroidRuntime(225):at tomcavell.app.DosEquisSoundboardActivity.onContextItemSelected(DosEquisSoundboardActivity.java:245)

E/AndroidRuntime(225):at android.app.Activity.onMenuItemSelected(Activity.java:2174)

E/AndroidRuntime(225):at com.android.internal.policy.impl.PhoneWindow$ContextMenuCallback.onMenuItemSelected(PhoneWindow.java:2731)

E/AndroidRuntime(225):at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:139)

E/AndroidRuntime(225):at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:855)

E/AndroidRuntime(225):at com.android.internal.view.menu.MenuDialogHelper.onClick(MenuDialogHelper.java:129)

E/AndroidRuntime(225):at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:884)

E/AndroidRuntime(225):at android.widget.AdapterView.performItemClick(AdapterView.java:284)

E/AndroidRuntime(225):at android.widget.ListView.performItemClick(ListView.java:3285)

E/AndroidRuntime(225):at android.widget.AbsListView$PerformClick.run(AbsListView.java:1640)

E/AndroidRuntime(225):at android.os.Handler.handleCallback(Handler.java:587)

E/AndroidRuntime(225):at android.os.Handler.dispatchMessage(Handler.java:92)

E/AndroidRuntime(225):at android.os.Looper.loop(Looper.java:123)

E/AndroidRuntime(225):at android.app.ActivityThread.main(ActivityThread.java:4363)

E/AndroidRuntime(225):at java.lang.reflect.Method.invokeNative(Native Method)

E/AndroidRuntime(225):at java.lang.reflect.Method.invoke(Method.java:521)

E/AndroidRuntime(225):at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)

E/AndroidRuntime(225):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

E/AndroidRuntime(225):at dalvik.system.NativeStart.main(Native Method)

I/dalvikvm(225): threadid=7: reacting to signal 3

I/dalvikvm(225): Wrote stack trace to '/data/anr/traces.txt'

1 个答案:

答案 0 :(得分:0)

我想出了我的问题。我必须使用int设置代码,然后将该int设置为

j=sounds[i]; //(assuming j was the int declared)

然后使用i传递函数saveasRing,如

saveasRing[i];

感谢您寻求帮助!