2011-11-29 74 views
0

我有一个音板,我试图将声音保存为铃声或通知,您可以看到下面的代码,我遇到的问题是当我尝试保存应用程序部队关闭在我身上的声音。Android Soundboard Force关闭

有时代码工作并保存,但其他时间强制关闭,但现在它似乎强制关闭所有的时间。这也是我已经恢复应用程序后?

如果任何人都可以看看它会appriciated。

谢谢。

public class Activity2 extends Activity { 


int selectedSoundId; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity2); 

    final MediaPlayer player = new MediaPlayer(); 
    final Resources res = getResources(); 

    //just keep them in the same order, e.g. button01 is tied to backtoyou 
    final int[] buttonIds = { R.id.PlaySound1, R.id.PlaySound2, R.id.PlaySound3, R.id.PlaySound4, R.id.PlaySound5,}; 
    final int[] soundIds = { R.raw.bentonirate, R.raw.bentonlong, R.raw.bentonshort, R.raw.ohjesuschrist, R.raw.ohjesuschristbenton, }; 

    View.OnClickListener listener = new View.OnClickListener() { 
     public void onClick(View v) { 
      //find the index that matches the button's ID, and then reset 
      //the MediaPlayer instance, set the data source to the corresponding 
      //sound effect, prepare it, and start it playing. 
      for(int i = 0; i < buttonIds.length; i++) { 
       if(v.getId() == buttonIds[i]) { 
        selectedSoundId = soundIds[i]; 
        AssetFileDescriptor afd = res.openRawResourceFd(soundIds[i]); 
        player.reset(); 
        try { 
         player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength()); 
        } 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(); 
        } 
        try { 
         player.prepare(); 
        } catch (IllegalStateException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } catch (IOException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
        player.start(); 
        break; 
       } 
      } 
     } 
    }; 



    //set the same listener for every button ID, no need 
    //to keep a reference to every button 
    for(int i = 0; i < buttonIds.length; i++) { 
     Button soundButton = (Button)findViewById(buttonIds[i]); 
     registerForContextMenu(soundButton); 
     soundButton.setOnClickListener(listener); 

    } 



} 

@Override 
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) { 
super.onCreateContextMenu(menu, v, menuInfo); 
menu.setHeaderTitle("Save as..."); 
menu.add(0, v.getId(), 0, "Ringtone"); 
menu.add(0, v.getId(), 0, "Notification"); 
} 
@Override 
public boolean onContextItemSelected(MenuItem item) { 
if(item.getTitle()=="Ringtone"){function1(item.getItemId());} 
    else if(item.getTitle()=="Notification"){function2(item.getItemId());} 
    else {return false;} 
return true; 
} 

public void function1(int id){ 

    if 
    (savering(selectedSoundId)){ 
     // Code if successful 
     Toast.makeText(this, "Saved as Ringtone", Toast.LENGTH_SHORT).show(); 
    }   
    else   
    { 
     // Code if unsuccessful 
     Toast.makeText(this, "Failed - Check your SDCard", Toast.LENGTH_SHORT).show(); 
    } 

    } 
    public void function2(int id){ 
    if 
    (savenot(selectedSoundId)){ 
     // Code if successful 
     Toast.makeText(this, "Saved as Notification", Toast.LENGTH_SHORT).show(); 
    }   
    else   
    { 
     // Code if unsuccessful 
     Toast.makeText(this, "Failed - Check your SDCard", Toast.LENGTH_SHORT).show(); 
    } 
    } 



//Save into Ring tone Folder 

public boolean savering(int ressound){ 
byte[] buffer=null; 
InputStream fIn = getBaseContext().getResources().openRawResource(ressound); 
int size=50; 

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=Environment.getExternalStorageDirectory().getPath()+"/media/ringtone/"; 


String filename="Benton"+".ogg"; 


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, "Benton"); 
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg"); 
values.put(MediaStore.Audio.Media.ARTIST, "weee"); 
values.put(MediaStore.Audio.Media.IS_RINGTONE, true); 
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false); 
values.put(MediaStore.Audio.Media.IS_ALARM, true); 
values.put(MediaStore.Audio.Media.IS_MUSIC, false);  

Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath()); 
getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + k.getAbsolutePath() + "\"", null); 
Uri newUri = getContentResolver().insert(uri, values); 
RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE, newUri); 




return true; 
} 

//Save in Notification Folder 

public boolean savenot(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=Environment.getExternalStorageDirectory().getPath()+"/media/notification/"; 


String filename="Benton"+".ogg"; 

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, "Benton"); 
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg"); 
values.put(MediaStore.Audio.Media.ARTIST, "weee"); 
values.put(MediaStore.Audio.Media.IS_RINGTONE, false); 
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true); 
values.put(MediaStore.Audio.Media.IS_ALARM, true); 
values.put(MediaStore.Audio.Media.IS_MUSIC, false);  

Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath()); 
getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + k.getAbsolutePath() + "\"", null); 
Uri newUri = getContentResolver().insert(uri, values); 
RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_NOTIFICATION, newUri); 


return true; 



} 
} 

logcat的武力密切:

  11-29 11:26:28.963: W/ResourceType(3312): No package identifier when getting value for resource number 0x00000000 
    11-29 11:26:28.963: W/dalvikvm(3312): threadid=1: thread exiting with uncaught exception (group=0x2aacc7d0) 
    11-29 11:26:28.963: E/AndroidRuntime(3312): Uncaught handler: thread main exiting due to uncaught exception 
    11-29 11:26:28.963: I/global(3312): Default buffer size used in BufferedWriter constructor. It would be better to be explicit if an 8k-char buffer is required. 
    11-29 11:26:29.003: I/global(3312): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required. 
    11-29 11:26:29.003: W/System.err(3312): java.io.IOException: Permission denied 
    11-29 11:26:29.033: I/global(3312): Default buffer size used in BufferedWriter constructor. It would be better to be explicit if an 8k-char buffer is required. 
    11-29 11:26:29.033: E/AndroidRuntime(3312): FATAL EXCEPTION: main 
    11-29 11:26:29.033: E/AndroidRuntime(3312): android.content.res.Resources$NotFoundException: Resource ID #0x0 
    11-29 11:26:29.033: E/AndroidRuntime(3312):  at android.content.res.Resources.getValue(Resources.java:895) 
    11-29 11:26:29.033: E/AndroidRuntime(3312):  at android.content.res.Resources.openRawResource(Resources.java:820) 
    11-29 11:26:29.033: E/AndroidRuntime(3312):  at android.content.res.Resources.openRawResource(Resources.java:802) 
    11-29 11:26:29.033: E/AndroidRuntime(3312):  at com.Tutorial.Sound.Activity2.savenot(Activity2.java:210) 
    11-29 11:26:29.033: E/AndroidRuntime(3312):  at com.Tutorial.Sound.Activity2.function2(Activity2.java:131) 
    11-29 11:26:29.033: E/AndroidRuntime(3312):  at com.Tutorial.Sound.Activity2.onContextItemSelected(Activity2.java:110) 
    11-29 11:26:29.033: E/AndroidRuntime(3312):  at android.app.Activity.onMenuItemSelected(Activity.java:2199) 
    11-29 11:26:29.033: E/AndroidRuntime(3312):  at com.android.internal.policy.impl.PhoneWindow$ContextMenuCallback.onMenuItemSelected(PhoneWindow.java:2744) 
    11-29 11:26:29.033: E/AndroidRuntime(3312):  at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:143) 
    11-29 11:26:29.033: E/AndroidRuntime(3312):  at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:855) 
    11-29 11:26:29.033: E/AndroidRuntime(3312):  at com.android.internal.view.menu.MenuDialogHelper.onClick(MenuDialogHelper.java:137) 
    11-29 11:26:29.033: E/AndroidRuntime(3312):  at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:874) 
    11-29 11:26:29.033: E/AndroidRuntime(3312):  at android.widget.AdapterView.performItemClick(AdapterView.java:284) 
    11-29 11:26:29.033: E/AndroidRuntime(3312):  at android.widget.ListView.performItemClick(ListView.java:3382) 
    11-29 11:26:29.033: E/AndroidRuntime(3312):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:1696) 
    11-29 11:26:29.033: E/AndroidRuntime(3312):  at android.os.Handler.handleCallback(Handler.java:587) 
    11-29 11:26:29.033: E/AndroidRuntime(3312):  at android.os.Handler.dispatchMessage(Handler.java:92) 
    11-29 11:26:29.033: E/AndroidRuntime(3312):  at android.os.Looper.loop(Looper.java:123) 
    11-29 11:26:29.033: E/AndroidRuntime(3312):  at android.app.ActivityThread.main(ActivityThread.java:4635) 
    11-29 11:26:29.033: E/AndroidRuntime(3312):  at java.lang.reflect.Method.invokeNative(Native Method) 
    11-29 11:26:29.033: E/AndroidRuntime(3312):  at java.lang.reflect.Method.invoke(Method.java:521) 
    11-29 11:26:29.033: E/AndroidRuntime(3312):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 
    11-29 11:26:29.033: E/AndroidRuntime(3312):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
    11-29 11:26:29.033: E/AndroidRuntime(3312):  at dalvik.system.NativeStart.main(Native Method) 
    11-29 11:26:29.073: I/global(3312): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required. 
    11-29 11:26:29.083: W/System.err(3312): java.io.FileNotFoundException: /data/plog.log (Permission denied) 
    11-29 11:26:29.143: I/global(3312): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required. 
    11-29 11:26:29.143: W/System.err(3312): java.io.IOException: Permission denied 

回答

1

当选择上下文菜单上的项目你会遇到这种崩溃。 。

实际传递给savering()的是实际上是整数(0,1,2 ...)的菜单项ID,您应该以某种方式将这些位置转换为soundIds中的索引。 我看不到在您的代码中完成此映射的位置。

+0

感谢您的答案男人是否有可能你可以帮我出什么代码添加和添加它? – Matt