首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >保存android特定的按钮声音

保存android特定的按钮声音
EN

Stack Overflow用户
提问于 2011-10-13 19:50:02
回答 1查看 214关注 0票数 2

我有一个soundboard,我正在尝试将声音保存为铃声或通知,但它每次保存的声音都是相同的,我认为是从“有人能给我一些启示吗?”我认为它需要更改R.raw.sound1,以便它拉入任何按钮被单击以弹出上下文菜单的声音。

我已经添加了循环和集成代码,这使按钮工作以防万一。

编辑完整代码-错误:无法解析selectedSoundId

:-对于每三位表示selectedSoundId的代码

我不确定我是否已经做到了这一点:我应该在哪里以及如何添加这个?

应该将"int selectedSoundId“声明为一个类字段,以便在每个类方法中都可以访问它。

代码语言:javascript
复制
public class Soundboard extends Activity {
 private SoundManager mSoundManager;

/** Called when the activity is first created. */
int selectedSoundId;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.soundboard);

    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.backdoors etc};
    final int[] soundIds = { R.raw.back_doors etc };

    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="/sdcard/media/audio/ringtones/";

 String filename="ohhh"+".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, "Ohhh Ringtone");   
 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, true);   
 values.put(MediaStore.Audio.Media.IS_ALARM, true);   
 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; 
}

     //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="/sdcard/media/audio/notifications/";

 String filename="ohhh"+".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, "Ohhh Notification");   
 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, true);   
 values.put(MediaStore.Audio.Media.IS_ALARM, true);   
 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;
}
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-10-13 20:03:20

我不知道您要在哪里替换

代码语言:javascript
复制
R.raw.sound1

它是作为参数传递的,因此,正如我在代码中观察到的,每次调用保存方法时,都会保存相同的声音。

编辑:

  1. 创建int变量,该变量将保存有关最后选择的声音的信息:

int selectedSoundId;

  • In onClickListener将selectedSoundId设置为与当前按钮连接的声音id:

如果(v.getId() == buttonIdsi) { selectedSoundId = soundIdsi;//代码的其余部分}

  • 将selectedSoundId传递给savering和savenot方法:

存储( savenot(selectedSoundId); )(SelectedSoundId)

希望这能有所帮助。

编辑2:我正在粘贴你的代码和我的修改。

广告2.这是当你需要保存有关当前声音id的信息时。

代码语言:javascript
复制
final int[] buttonIds = { R.raw.sound1 "etc all buttons here"};
    final int[] soundIds = {R.raw.sound1 "etc all sounds here"};

    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;
                }
            }
        }
    };

广告3.这是调用savering和savenot方法的一部分。

代码语言:javascript
复制
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(); 
 }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7753666

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档