修改 的
好的,我发现如果我在尝试保存之前播放任何声音,当你试图保存它们时它会正常工作?????
修改 的
我有一个声音板,我已经制作了一个小问题,我将声音保存到手机的SDCARD,在Android 2.2安卓平板电脑上工作正常。
但是当我在使用Android 2.3的手机上运行并尝试保存声音时,我会收到强制关闭错误?
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;
}
}
不确定是否与我保存声音的位置有关,如果这对于不同版本而言是不同的?也许这一行?
String path=Environment.getExternalStorageDirectory().getPath()+"/media/ringtone/";
logcat的:
12-02 16:03:28.521: D/dalvikvm(7148): GC_EXTERNAL_ALLOC freed 42K, 50% free 2714K/5379K, external 0K/0K, paused 34ms
12-02 16:03:28.571: V/SoundPool-JNI(7148): android_media_SoundPool_native_setup
12-02 16:03:28.571: V/SoundPool(7148): SoundPool constructor: maxChannels=4, streamType=3, srcQuality=0
12-02 16:03:28.571: V/SoundPool-JNI(7148): android_media_SoundPool_load_FD
12-02 16:03:28.571: V/SoundPool(7148): load: fd=42, offset=865776, length=39946, priority=1
12-02 16:03:28.571: V/SoundPool(7148): create sampleID=1, fd=43, offset=39946, length=865776
12-02 16:03:28.571: V/SoundPool(7148): doLoad: loading sample sampleID=1
12-02 16:03:28.571: V/SoundPool-JNI(7148): android_media_SoundPool_load_FD
12-02 16:03:28.571: V/SoundPool(7148): load: fd=42, offset=905774, length=98155, priority=1
12-02 16:03:28.571: V/SoundPool(7148): create sampleID=2, fd=44, offset=98155, length=905774
12-02 16:03:28.571: V/SoundPool(7148): doLoad: loading sample sampleID=2
12-02 16:03:28.571: V/SoundPool-JNI(7148): android_media_SoundPool_load_FD
12-02 16:03:28.571: V/SoundPool(7148): load: fd=42, offset=1003982, length=28948, priority=1
12-02 16:03:28.571: V/SoundPool(7148): create sampleID=3, fd=45, offset=28948, length=1003982
12-02 16:03:28.571: V/SoundPool(7148): doLoad: loading sample sampleID=3
12-02 16:03:28.571: V/SoundPool-JNI(7148): android_media_SoundPool_load_FD
12-02 16:03:28.571: V/SoundPool(7148): load: fd=42, offset=1032985, length=23394, priority=1
12-02 16:03:28.571: V/SoundPool(7148): create sampleID=4, fd=46, offset=23394, length=1032985
12-02 16:03:28.571: V/SoundPool(7148): doLoad: loading sample sampleID=4
12-02 16:03:28.571: V/SoundPool-JNI(7148): android_media_SoundPool_load_FD
12-02 16:03:28.571: V/SoundPool(7148): load: fd=42, offset=1056440, length=73700, priority=1
12-02 16:03:28.571: V/SoundPool(7148): create sampleID=5, fd=47, offset=73700, length=1056440
12-02 16:03:28.571: V/SoundPool(7148): doLoad: loading sample sampleID=5
12-02 16:03:28.571: V/SoundPoolThread(7148): beginThread
12-02 16:03:28.581: V/SoundPoolThread(7148): run
12-02 16:03:28.581: V/SoundPoolThread(7148): Got message m=2, mData=1
12-02 16:03:28.581: V/SoundPool(7148): Start decode
12-02 16:03:29.031: V/SoundPool(7148): close(43)
12-02 16:03:29.031: V/SoundPool(7148): pointer = 0x45c82000, size = 552960, sampleRate = 48000, numChannels = 2
12-02 16:03:29.041: V/SoundPool-JNI(7148): callback: (1, 1, 0, 0x32fab0, 0x4051f8f0)
12-02 16:03:29.041: V/SoundPoolThread(7148): Got message m=2, mData=2
12-02 16:03:29.041: V/SoundPool(7148): Start decode
12-02 16:03:29.482: V/SoundPool(7148): close(44)
12-02 16:03:29.482: V/SoundPool(7148): pointer = 0x45d82000, size = 1048576, sampleRate = 48000, numChannels = 2
12-02 16:03:29.482: V/SoundPool-JNI(7148): callback: (1, 2, 0, 0x32fab0, 0x4051f8f0)
12-02 16:03:29.482: V/SoundPoolThread(7148): Got message m=2, mData=3
12-02 16:03:29.482: V/SoundPool(7148): Start decode
12-02 16:03:29.702: V/SoundPool(7148): close(45)
12-02 16:03:29.702: V/SoundPool(7148): pointer = 0x45e82000, size = 372736, sampleRate = 48000, numChannels = 2
12-02 16:03:29.702: V/SoundPool-JNI(7148): callback: (1, 3, 0, 0x32fab0, 0x4051f8f0)
12-02 16:03:29.702: V/SoundPoolThread(7148): Got message m=2, mData=4
12-02 16:03:29.702: V/SoundPool(7148): Start decode
12-02 16:03:29.822: V/SoundPool(7148): close(46)
12-02 16:03:29.822: V/SoundPool(7148): pointer = 0x45f82000, size = 286720, sampleRate = 48000, numChannels = 2
12-02 16:03:29.822: V/SoundPool-JNI(7148): callback: (1, 4, 0, 0x32fab0, 0x4051f8f0)
12-02 16:03:29.822: V/SoundPoolThread(7148): Got message m=2, mData=5
12-02 16:03:29.822: V/SoundPool(7148): Start decode
12-02 16:03:30.213: V/SoundPool(7148): close(47)
12-02 16:03:30.213: V/SoundPool(7148): pointer = 0x46082000, size = 1048576, sampleRate = 48000, numChannels = 2
12-02 16:03:30.213: V/SoundPool-JNI(7148): callback: (1, 5, 0, 0x32fab0, 0x4051f8f0)
12-02 16:03:31.914: D/dalvikvm(7148): GC_EXTERNAL_ALLOC freed 12K, 50% free 2740K/5379K, external 1949K/2199K, paused 23ms
12-02 16:03:32.004: D/dalvikvm(7148): GC_EXTERNAL_ALLOC freed 3K, 49% free 2747K/5379K, external 4409K/4541K, paused 29ms
12-02 16:03:35.217: W/ResourceType(7148): No package identifier when getting value for resource number 0x00000000
12-02 16:03:35.217: D/AndroidRuntime(7148): Shutting down VM
12-02 16:03:35.217: W/dalvikvm(7148): threadid=1: thread exiting with uncaught exception (group=0x4001d5a0)
12-02 16:03:35.247: E/AndroidRuntime(7148): FATAL EXCEPTION: main
12-02 16:03:35.247: E/AndroidRuntime(7148): android.content.res.Resources$NotFoundException: Resource ID #0x0
12-02 16:03:35.247: E/AndroidRuntime(7148): at android.content.res.Resources.getValue(Resources.java:901)
12-02 16:03:35.247: E/AndroidRuntime(7148): at android.content.res.Resources.openRawResource(Resources.java:826)
12-02 16:03:35.247: E/AndroidRuntime(7148): at android.content.res.Resources.openRawResource(Resources.java:808)
12-02 16:03:35.247: E/AndroidRuntime(7148): at com.Tutorial.Sound.Activity2.savering(Activity2.java:148)
12-02 16:03:35.247: E/AndroidRuntime(7148): at com.Tutorial.Sound.Activity2.function1(Activity2.java:118)
12-02 16:03:35.247: E/AndroidRuntime(7148): at com.Tutorial.Sound.Activity2.onContextItemSelected(Activity2.java:109)
12-02 16:03:35.247: E/AndroidRuntime(7148): at android.app.Activity.onMenuItemSelected(Activity.java:2254)
12-02 16:03:35.247: E/AndroidRuntime(7148): at com.android.internal.policy.impl.PhoneWindow$ContextMenuCallback.onMenuItemSelected(PhoneWindow.java:2903)
12-02 16:03:35.247: E/AndroidRuntime(7148): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:160)
12-02 16:03:35.247: E/AndroidRuntime(7148): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:885)
12-02 16:03:35.247: E/AndroidRuntime(7148): at com.android.internal.view.menu.MenuDialogHelper.onClick(MenuDialogHelper.java:137)
12-02 16:03:35.247: E/AndroidRuntime(7148): at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:880)
12-02 16:03:35.247: E/AndroidRuntime(7148): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
12-02 16:03:35.247: E/AndroidRuntime(7148): at android.widget.ListView.performItemClick(ListView.java:3604)
12-02 16:03:35.247: E/AndroidRuntime(7148): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812)
12-02 16:03:35.247: E/AndroidRuntime(7148): at android.os.Handler.handleCallback(Handler.java:587)
12-02 16:03:35.247: E/AndroidRuntime(7148): at android.os.Handler.dispatchMessage(Handler.java:92)
12-02 16:03:35.247: E/AndroidRuntime(7148): at android.os.Looper.loop(Looper.java:143)
12-02 16:03:35.247: E/AndroidRuntime(7148): at android.app.ActivityThread.main(ActivityThread.java:4196)
12-02 16:03:35.247: E/AndroidRuntime(7148): at java.lang.reflect.Method.invokeNative(Native Method)
12-02 16:03:35.247: E/AndroidRuntime(7148): at java.lang.reflect.Method.invoke(Method.java:507)
12-02 16:03:35.247: E/AndroidRuntime(7148): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-02 16:03:35.247: E/AndroidRuntime(7148): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-02 16:03:35.247: E/AndroidRuntime(7148): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
savering(selectedSoundId)
似乎“selectedSoundId”可能未初始化
为了使您的selectedSoundId成为有效值,您需要单击声音并播放它。那部分会发生吗?
否则,你必须在function1(和2)方法中使用与clickListener中相同的代码,以便为menuId找到合适的soundId。