简体   繁体   English

音板铃声/通知音

[英]Sound Board Ringtone/notification tones

I want to be able set the sounds that I have on the sound board as ringtones and notification tones. 我希望能够将音板上的声音设置为铃声和通知音。 I've looked up this on this website and I can't seem to find a solution. 我已经在此网站上查询了此内容,但似乎找不到解决方案。 Any ideas?? 有任何想法吗??

Thanks for looking. 感谢您的光临。 :-) :-)

My code is 我的代码是

  package com.sherlock;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.content.res.Resources;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.sherlocksoundboard.R;

public class Content extends Activity {

     int savetype = RingtoneManager.TYPE_RINGTONE;

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

            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.button1, R.id.Button01, R.id.Button02, R.id.Button03, R.id.Button04,R.id.Button05,R.id.Button06,};
            final int[] soundIds = { R.raw.sherlock, R.raw.deargod, R.raw.donttalk, R.raw.lookat, R.raw.stop,R.raw.text,R.raw.john, };

            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 selectedSoundId){
         byte[] buffer=null;
         InputStream fIn = getBaseContext().getResources().openRawResource(selectedSoundId);
         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());
         Uri newUri = getContentResolver().insert(uri, values); 
         RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE, newUri);




         return true; 
        }








        //Save in Notification Folder

        public boolean savenot(int selectedSoundId){
         byte[] buffer=null;
         InputStream fIn = getBaseContext().getResources().openRawResource(selectedSoundId);
         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/ringtone/";


         String filename= "Sherlock"+".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_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory()))); 

         File k = new File(path, filename);   
         ContentValues values = new ContentValues();   
         values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
         values.put(MediaStore.MediaColumns.TITLE, "Sherlock");
         values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
         values.put(MediaStore.Audio.Media.ARTIST, "Elvis");  
         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);    



          //Insert it into the database
         Uri newUri = this.getContentResolver().insert(MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath()), values);

         // set as ringtone
         RingtoneManager.setActualDefaultRingtoneUri(this, savetype, newUri);


         return true; 



        }







        }

To set any audio file as ringtone it should be stored into database. 要将任何音频文件设置为铃声,应将其存储到数据库中。 //then only it is setted otherwise it wouldn't give any error but not set as default ringtone. //然后只进行设置,否则不会发出任何错误,但不会设置为默认铃声。 //you can add more details BY content.put() method. //您可以通过content.put()方法添加更多详细信息。

String filepath ="/sdcard/play2.mp3";
File ringtoneFile = new File(filepath);

ContentValues content = new ContentValues();
content.put(MediaStore.MediaColumns.DATA,ringtoneFile.getAbsolutePath());
content.put(MediaStore.MediaColumns.TITLE, "chinnu");
content.put(MediaStore.MediaColumns.SIZE, 215454);
content.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");
content.put(MediaStore.Audio.Media.ARTIST, "Madonna");
content.put(MediaStore.Audio.Media.DURATION, 230);
content.put(MediaStore.Audio.Media.IS_RINGTONE, true);
content.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
content.put(MediaStore.Audio.Media.IS_ALARM, false);
content.put(MediaStore.Audio.Media.IS_MUSIC, false);


//Insert it into the database
Log.i(TAG, "the absolute path of the file is :"+
ringtoneFile.getAbsolutePath());
Uri uri = MediaStore.Audio.Media.getContentUriForPath(
ringtoneFile.getAbsolutePath());
Uri newUri = context.getContentResolver().insert(uri, content);
ringtoneUri = newUri; 
Log.i(TAG,"the ringtone uri is :"+ringtoneUri);
RingtoneManager.setActualDefaultRingtoneUri(context,
RingtoneManager.TYPE_RINGTONE,newUri);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM