在Eclipse中使用媒体播放器的音频无法保存到手机 [英] Can't save audio to phone using media player in eclipse

查看:156
本文介绍了在Eclipse中使用媒体播放器的音频无法保存到手机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个音板,你用户可以播放和停止的声音以及声音保存到手机上。

我已经将它设置罚款,但保存将无法工作。我没有得到任何错误,不知道什么是错。我可以想出说要保存它的框,但点击后什么都不做。使用ViewPagerIndicator。
任何帮助吗?
对不起只是一个初学者。

 公共类问题1扩展片段{私人的AdView AdView的;按钮button01;
按钮button02;
按钮button03;
按钮button04;
按钮button05;
按钮button06;
按钮button07;
按钮button08;
按钮button09;
按钮button10;
按钮button11;
按钮button12;
按钮button13;
按钮button14;
按钮button15;
按钮button16;
按钮button17;
按钮button18;    公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){
        Toast.makeText(this.getActivity(),龙preSS保存为铃声。Toast.LENGTH_LONG).show();        {返回inflater.inflate(R.layout.question1,NULL);}
    }    @覆盖
    公共无效onActivityCreated(捆绑savedInstanceState){
        super.onActivityCreated(savedInstanceState);
        。button01 =(按钮)getView()findViewById(R.id.button01);
        。button02 =(按钮)getView()findViewById(R.id.button02);
        。button03 =(按钮)getView()findViewById(R.id.button03);
        。button04 =(按钮)getView()findViewById(R.id.button04);
        。button05 =(按钮)getView()findViewById(R.id.button05);
        。button06 =(按钮)getView()findViewById(R.id.button06);
        。button07 =(按钮)getView()findViewById(R.id.button07);
        。button08 =(按钮)getView()findViewById(R.id.button08);
        。button09 =(按钮)getView()findViewById(R.id.button09);
        。button10 =(按钮)getView()findViewById(R.id.button10);
        。button11 =(按钮)getView()findViewById(R.id.button11);
        。button12 =(按钮)getView()findViewById(R.id.button12);
        。button13 =(按钮)getView()findViewById(R.id.button13);
        。button14 =(按钮)getView()findViewById(R.id.button14);
        。button15 =(按钮)getView()findViewById(R.id.button15);
        。button16 =(按钮)getView()findViewById(R.id.button16);
        。button17 =(按钮)getView()findViewById(R.id.button17);
        。button18 =(按钮)getView()findViewById(R.id.button18);    //导入声音文件
    最终的MediaPlayer sound01 = MediaPlayer.create(getActivity(),R.raw.sound01);
    最终的MediaPlayer sound02 = MediaPlayer.create(getActivity(),R.raw.sound02);
    最终的MediaPlayer sound03 = MediaPlayer.create(getActivity(),R.raw.sound03);
    最终的MediaPlayer sound04 = MediaPlayer.create(getActivity(),R.raw.sound04);
    最终的MediaPlayer sound05 = MediaPlayer.create(getActivity(),R.raw.sound05);
    最终的MediaPlayer sound06 = MediaPlayer.create(getActivity(),R.raw.sound06);
    最终的MediaPlayer sound07 = MediaPlayer.create(getActivity(),R.raw.sound07);
    最终的MediaPlayer sound08 = MediaPlayer.create(getActivity(),R.raw.sound08);
    最终的MediaPlayer sound09 = MediaPlayer.create(getActivity(),R.raw.sound09);
    最终的MediaPlayer sound10 = MediaPlayer.create(getActivity(),R.raw.sound10);
    最终的MediaPlayer sound11 = MediaPlayer.create(getActivity(),R.raw.sound11);
    最终的MediaPlayer sound12 = MediaPlayer.create(getActivity(),R.raw.sound12);
    最终的MediaPlayer sound13 = MediaPlayer.create(getActivity(),R.raw.sound13);
    最终的MediaPlayer sound14 = MediaPlayer.create(getActivity(),R.raw.sound14);
    最终的MediaPlayer sound15 = MediaPlayer.create(getActivity(),R.raw.sound15);
    最终的MediaPlayer sound16 = MediaPlayer.create(getActivity(),R.raw.sound16);
    最终的MediaPlayer sound17 = MediaPlayer.create(getActivity(),R.raw.sound17);
 //播放的点击声音文件
    按钮S02 =(按钮)getView()findViewById(R.id.button02)。
    s02.setText(this.getString(R.string.quote02));
    s02.setOnClickListener(新OnClickListener(){
        公共无效的onClick(视图v){
            尝试{
                。sound01 prepare();
            }赶上(IllegalStateException异常五){
                // TODO自动生成catch块
                e.printStackTrace();
            }赶上(IOException异常五){
                // TODO自动生成catch块
                e.printStackTrace();
            }
            sound01.start();
            }    });
    registerForContextMenu(S02);    按钮S03 =(按钮)getView()findViewById(R.id.button03)。
    s03.setText(this.getString(R.string.quote03));
    s03.setOnClickListener(新OnClickListener(){
        公共无效的onClick(视图v){
            尝试{
                。sound02 prepare();
            }赶上(IllegalStateException异常五){
                // TODO自动生成catch块
                e.printStackTrace();
            }赶上(IOException异常五){
                // TODO自动生成catch块
                e.printStackTrace();
            }
            sound02.start();
            }
    });
    registerForContextMenu(S03);    按钮S04 =(按钮)getView()findViewById(R.id.button04)。
    s04.setText(this.getString(R.string.quote04));
    s04.setOnClickListener(新OnClickListener(){
        公共无效的onClick(视图v){
            尝试{
                。sound03 prepare();
            }赶上(IllegalStateException异常五){
                // TODO自动生成catch块
                e.printStackTrace();
            }赶上(IOException异常五){
                // TODO自动生成catch块
                e.printStackTrace();
            }
            sound03.start();
            }
    });
    registerForContextMenu(S04);    按钮S05 =(按钮)getView()findViewById(R.id.button05)。
    s05.setText(this.getString(R.string.quote05));
    s05.setOnClickListener(新OnClickListener(){
        公共无效的onClick(视图v){
            尝试{
                。sound04 prepare();
            }赶上(IllegalStateException异常五){
                // TODO自动生成catch块
                e.printStackTrace();
            }赶上(IOException异常五){
                // TODO自动生成catch块
                e.printStackTrace();
            }
            sound04.start();
            }
    });
    registerForContextMenu(S05);    按钮S06 =(按钮)getView()findViewById(R.id.button06)。
    s06.setText(this.getString(R.string.quote06));
    s06.setOnClickListener(新OnClickListener(){
        公共无效的onClick(视图v){
            尝试{
                。sound05 prepare();
            }赶上(IllegalStateException异常五){
                // TODO自动生成catch块
                e.printStackTrace();
            }赶上(IOException异常五){
                // TODO自动生成catch块
                e.printStackTrace();
            }
            sound05.start();
            }
    });
    registerForContextMenu(S06);    按钮S07 =(按钮)getView()findViewById(R.id.button07)。
    s07.setText(this.getString(R.string.quote07));
    s07.setOnClickListener(新OnClickListener(){
        公共无效的onClick(视图v){
            尝试{
                。sound06 prepare();
            }赶上(IllegalStateException异常五){
                // TODO自动生成catch块
                e.printStackTrace();
            }赶上(IOException异常五){
                // TODO自动生成catch块
                e.printStackTrace();
            }
            sound06.start();
            }
    });
    registerForContextMenu(S07);
    按钮S08 =(按钮)getView()findViewById(R.id.button08)。
    s08.setText(this.getString(R.string.quote08));
    s08.setOnClickListener(新OnClickListener(){
        公共无效的onClick(视图v){
            尝试{
                。sound07 prepare();
            }赶上(IllegalStateException异常五){
                // TODO自动生成catch块
                e.printStackTrace();
            }赶上(IOException异常五){
                // TODO自动生成catch块
                e.printStackTrace();
            }
            sound07.start();
            }
    });
    registerForContextMenu(S08);
    按钮S09 =(按钮)getView()findViewById(R.id.button09)。
    s09.setText(this.getString(R.string.quote09));
    s09.setOnClickListener(新OnClickListener(){
        公共无效的onClick(视图v){
            尝试{
                。sound08 prepare();
            }赶上(IllegalStateException异常五){
                // TODO自动生成catch块
                e.printStackTrace();
            }赶上(IOException异常五){
                // TODO自动生成catch块
                e.printStackTrace();
            }
            sound08.start();
            }
    });
    registerForContextMenu(S09);
    按钮S10 =(按钮)getView()findViewById(R.id.button10)。
    s10.setText(this.getString(R.string.quote10));
    s10.setOnClickListener(新OnClickListener(){
        公共无效的onClick(视图v){
            尝试{
                。sound09 prepare();
            }赶上(IllegalStateException异常五){
                // TODO自动生成catch块
                e.printStackTrace();
            }赶上(IOException异常五){
                // TODO自动生成catch块
                e.printStackTrace();
            }
            sound09.start();
            }
    });
    registerForContextMenu(S10);
    按钮S11 =(按钮)getView()findViewById(R.id.button11)。
    s11.setText(this.getString(R.string.quote11));
    s11.setOnClickListener(新OnClickListener(){
        公共无效的onClick(视图v){
            尝试{
                。sound10 prepare();
            }赶上(IllegalStateException异常五){
                // TODO自动生成catch块
                e.printStackTrace();
            }赶上(IOException异常五){
                // TODO自动生成catch块
                e.printStackTrace();
            }
            sound10.start();
            }
    });
    registerForContextMenu(S11);
    按钮S12 =(按钮)getView()findViewById(R.id.button12)。
    s12.setText(this.getString(R.string.quote12));
    s12.setOnClickListener(新OnClickListener(){
        公共无效的onClick(视图v){
            尝试{
                。sound11 prepare();
            }赶上(IllegalStateException异常五){
                // TODO自动生成catch块
                e.printStackTrace();
            }赶上(IOException异常五){
                // TODO自动生成catch块
                e.printStackTrace();
            }
            sound11.start();
            }
    });
    registerForContextMenu(S12);
    按钮S13 =(按钮)getView()findViewById(R.id.button13)。
    s13.setText(this.getString(R.string.quote13));
    s13.setOnClickListener(新OnClickListener(){
        公共无效的onClick(视图v){
            尝试{
                。sound12 prepare();
            }赶上(IllegalStateException异常五){
                // TODO自动生成catch块
                e.printStackTrace();
            }赶上(IOException异常五){
                // TODO自动生成catch块
                e.printStackTrace();
            }
            sound12.start();
            }
    });
    registerForContextMenu(S13);
    按钮S14 =(按钮)getView()findViewById(R.id.button14)。
    s14.setText(this.getString(R.string.quote14));
    s14.setOnClickListener(新OnClickListener(){
        公共无效的onClick(视图v){
            尝试{
                。sound13 prepare();
            }赶上(IllegalStateException异常五){
                // TODO自动生成catch块
                e.printStackTrace();
            }赶上(IOException异常五){
                // TODO自动生成catch块
                e.printStackTrace();
            }
            sound13.start();
            }
    });
    registerForContextMenu(S14);
    按钮S15 =(按钮)getView()findViewById(R.id.button15)。
    s15.setText(this.getString(R.string.quote15));
    s15.setOnClickListener(新OnClickListener(){
        公共无效的onClick(视图v){
            尝试{
                。sound14 prepare();
            }赶上(IllegalStateException异常五){
                // TODO自动生成catch块
                e.printStackTrace();
            }赶上(IOException异常五){
                // TODO自动生成catch块
                e.printStackTrace();
            }
            sound14.start();
            }
    });
    registerForContextMenu(S15);
    按钮S16 =(按钮)getView()findViewById(R.id.button16)。
    s16.setText(this.getString(R.string.quote16));
    s16.setOnClickListener(新OnClickListener(){
        公共无效的onClick(视图v){
            尝试{
                。sound15 prepare();
            }赶上(IllegalStateException异常五){
                // TODO自动生成catch块
                e.printStackTrace();
            }赶上(IOException异常五){
                // TODO自动生成catch块
                e.printStackTrace();
            }
            sound15.start();
            }
    });
    registerForContextMenu(S16);
    按钮S17 =(按钮)getView()findViewById(R.id.button17)。
    s17.setText(this.getString(R.string.quote17));
    s17.setOnClickListener(新OnClickListener(){
        公共无效的onClick(视图v){
            尝试{
                。sound16 prepare();
            }赶上(IllegalStateException异常五){
                // TODO自动生成catch块
                e.printStackTrace();
            }赶上(IOException异常五){
                // TODO自动生成catch块
                e.printStackTrace();
            }
            sound16.start();
            }
    });
    registerForContextMenu(S17);
    按钮S18 =(按钮)getView()findViewById(R.id.button18)。
    s18.setText(this.getString(R.string.quote18));
    s18.setOnClickListener(新OnClickListener(){
        公共无效的onClick(视图v){
            尝试{
                。sound17 prepare();
            }赶上(IllegalStateException异常五){
                // TODO自动生成catch块
                e.printStackTrace();
            }赶上(IOException异常五){
                // TODO自动生成catch块
                e.printStackTrace();
            }
            sound17.start();
            }
    });
    registerForContextMenu(S18);    最终按钮S01 =(按钮)getView()findViewById(R.id.button01)。
    s01.setText(this.getString(R.string.quote01));
    s01.setOnClickListener(新View.OnClickListener(){
        公共无效的onClick(视图v){
            如果(sound01.isPlaying())
            {
    sound01.pause();
    sound01.seekTo(0);
            }
            如果(sound02.isPlaying())
            {
    sound02.pause();
    sound02.seekTo(0);
            }
            如果(sound03.isPlaying())
            {
    sound03.pause();
    sound03.seekTo(0);
            }
            如果(sound04.isPlaying())
            {
    sound04.pause();
    sound04.seekTo(0);
            };
            如果(sound05.isPlaying())
            {
    sound05.pause();
    sound05.seekTo(0);
            }
            如果(sound06.isPlaying())
            {
    sound06.pause();
    sound06.seekTo(0);
            }
            如果(sound07.isPlaying())
            {
    sound07.pause();
    sound07.seekTo(0);
            }
            如果(sound08.isPlaying())
            {
    sound08.pause();
    sound08.seekTo(0);
            }
            如果(sound09.isPlaying())
            {
    sound09.pause();
    sound09.seekTo(0);
            }
            如果(sound10.isPlaying())
            {
    sound10.pause();
    sound10.seekTo(0);
            }
            如果(sound11.isPlaying())
            {
    sound11.pause();
    sound11.seekTo(0);
            }
            如果(sound12.isPlaying())
            {
    sound12.pause();
    sound12.seekTo(0);
            }
            如果(sound13.isPlaying())
            {
    sound13.pause();
    sound13.seekTo(0);
            }
            如果(sound14.isPlaying())
            {
    sound14.pause();
    sound14.seekTo(0);
            }
            如果(sound15.isPlaying())
            {
    sound15.pause();
    sound15.seekTo(0);
            }
            如果(sound16.isPlaying())
            {
    sound16.pause();
    sound16.seekTo(0);
            }
            如果(sound17.isPlaying())
            {
    sound17.pause();
    sound17.seekTo(0);
            }        }});
    registerForContextMenu(S01);
    }
    //执行存储在长preSS功能
    公共无效onCreateContextMenu(文本菜单菜单视图V,ContextMenuInfo menuInfo){
        super.onCreateContextMenu(菜单,V,menuInfo);
            menu.setHeaderTitle(另存为...);
            menu.add(0,v.getId(),0,铃声/通知);
        }    @覆盖
    公共布尔onContextItemSelected(菜单项项){
        如果(item.getTitle()==铃声/通知){功能1(item.getItemId());}
        其他{返回false;}
    返回true;
    }    //检测哪个按钮被点击,保存为的strings.xml铃声
    公共布尔功能1(INT ressound){        // ----
              串soundname =;               开关(ressound){
            案例R.id.button02:
            ressound = R.raw.sound01;
            soundname =(this.getString(R.string.app_name))+ - +(this.getString(R.string.quote02));
            打破;
            }               开关(ressound){
            案例R.id.button03:
            ressound = R.raw.sound02;
            soundname =(this.getString(R.string.app_name))+ - +(this.getString(R.string.quote03));
            打破;
            }
               开关(ressound){
            案例R.id.button04:
            ressound = R.raw.sound03;
            soundname =(this.getString(R.string.app_name))+ - +(this.getString(R.string.quote04));
            打破;
            }           开关(ressound){
            案例R.id.button05:
            ressound = R.raw.sound04;
            soundname =(this.getString(R.string.app_name))+ - +(this.getString(R.string.quote05));
            打破;
            }
           开关(ressound){
            案例R.id.button06:
            ressound = R.raw.sound05;
            soundname =(this.getString(R.string.app_name))+ - +(this.getString(R.string.quote06));
            打破;
            }
           开关(ressound){
            案例R.id.button07:
            ressound = R.raw.sound06;
            soundname =(this.getString(R.string.app_name))+ - +(this.getString(R.string.quote07));
            打破;
            }
           开关(ressound){
            案例R.id.button08:
            ressound = R.raw.sound07;
            soundname =(this.getString(R.string.app_name))+ - +(this.getString(R.string.quote08));
            打破;
            }
           开关(ressound){
            案例R.id.button09:
            ressound = R.raw.sound08;
            soundname =(this.getString(R.string.app_name))+ - +(this.getString(R.string.quote09));
            打破;
            }
           开关(ressound){
            案例R.id.button10:
            ressound = R.raw.sound09;
            soundname =(this.getString(R.string.app_name))+ - +(this.getString(R.string.quote10));
            打破;
            }
           开关(ressound){
            案例R.id.button11:
            ressound = R.raw.sound10;
            soundname =(this.getString(R.string.app_name))+ - +(this.getString(R.string.quote11));
            打破;
            }
           开关(ressound){
            案例R.id.button12:
            ressound = R.raw.sound11;
            soundname =(this.getString(R.string.app_name))+ - +(this.getString(R.string.quote12));
            打破;
            }
           开关(ressound){
            案例R.id.button13:
            ressound = R.raw.sound12;
            soundname =(this.getString(R.string.app_name))+ - +(this.getString(R.string.quote13));
            打破;
            }
           开关(ressound){
            案例R.id.button14:
            ressound = R.raw.sound13;
            soundname =(this.getString(R.string.app_name))+ - +(this.getString(R.string.quote14));
            打破;
            }
           开关(ressound){
            案例R.id.button15:
            ressound = R.raw.sound14;
            soundname =(this.getString(R.string.app_name))+ - +(this.getString(R.string.quote15));
            打破;
            }
           开关(ressound){
            案例R.id.button16:
            ressound = R.raw.sound15;
            soundname =(this.getString(R.string.app_name))+ - +(this.getString(R.string.quote16));
            打破;
            }
           开关(ressound){
            案例R.id.button17:
            ressound = R.raw.sound16;
            soundname =(this.getString(R.string.app_name))+ - +(this.getString(R.string.quote17));
            打破;
            }
           开关(ressound){
            案例R.id.button18:
            ressound = R.raw.sound17;
            soundname =(this.getString(R.string.app_name))+ - +(this.getString(R.string.quote18));
            打破;
            }              //等等等等.....
              字节[]缓冲= NULL;
               InputStream的FIN = getActivity()getBaseContext()getResources()openRawResource(ressound)。;
               INT大小= 0;               尝试{
               大小= fIn.available();
               缓冲区=新的字节[大小]
               fIn.read(缓冲液);
               fIn.close();
               }赶上(IOException异常五){
               // TODO自动生成catch块
               返回false;
               }               字符串路径=/ SD卡/媒体/音频/铃声/;
               奥格字符串的文件名= soundname +;               布尔存在=(新文件(路径))存在();
               如果(存在!){新的文件(路径).mkdirs();}               FileOutputStream中保存;
               尝试{
               保存=新的FileOutputStream(路径+文件名);
               save.write(缓冲液);
               save.flush();
               save.close();
               }赶上(FileNotFoundException异常五){
               // TODO自动生成catch块
               返回false;
               }赶上(IOException异常五){
               // TODO自动生成catch块
               返回false;
               }               。getActivity()sendBroadcast(新意图(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,Uri.parse(文件://+路径+文件名)));               文件k =新的文件(路径,文件名);               ContentValues​​值=新ContentValues​​();
               values​​.put(MediaStore.MediaColumns.DATA,k.getAbsolutePath());
               values​​.put(MediaStore.MediaColumns.TITLE,soundname);
               values​​.put(MediaStore.MediaColumns.MIME_TYPE,音频/ OGG);
               values​​.put(MediaStore.Audio.Media.ARTIST拿破仑·希尔);
               values​​.put(MediaStore.Audio.Media.IS_RINGTONE,真);
               values​​.put(MediaStore.Audio.Media.IS_NOTIFICATION,真);
               values​​.put(MediaStore.Audio.Media.IS_ALARM,真);
               values​​.put(MediaStore.Audio.Media.IS_MUSIC,FALSE);               //把它插入到数据库
               getActivity().getContentResolver().insert(MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath()),值);
              串I =保存为铃声。
               Toast.makeText(getActivity()。getApplicationContext(),我,
               Toast.LENGTH_LONG).show();             返回true;
              }        // ----    }


解决方案

您需要切换

  item.getTitle()==铃声/通知

 铃声/通知.equals(item.getTitle());

下面的问题是一个很好的回答了原因:我如何比较字符串在Java中?

I'm trying to make a soundboard where you user can play and stop sound as well as save the sounds to phone.

I have set it up fine but the save will not work. I'm not getting any error and not sure whats wrong. I get the box to come up saying to save it, but when clicked does nothing. Using ViewPagerIndicator. Any help? Sorry only a beginner.

public class Question1 extends Fragment{ private AdView adView;

Button button01;
Button button02;
Button button03;
Button button04;
Button button05;
Button button06;
Button button07;
Button button08;
Button button09;
Button button10;
Button button11;
Button button12;
Button button13;
Button button14;
Button button15;
Button button16;
Button button17;
Button button18;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Toast.makeText(this.getActivity(), "Long Press To Save As Ringtone.", Toast.LENGTH_LONG).show();

        {return inflater.inflate(R.layout.question1, null);}
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState){
        super.onActivityCreated(savedInstanceState);
        button01 = (Button)getView().findViewById(R.id.button01);
        button02 = (Button)getView().findViewById(R.id.button02);
        button03 = (Button)getView().findViewById(R.id.button03);
        button04 = (Button)getView().findViewById(R.id.button04);
        button05 = (Button)getView().findViewById(R.id.button05);
        button06 = (Button)getView().findViewById(R.id.button06);
        button07 = (Button)getView().findViewById(R.id.button07);
        button08 = (Button)getView().findViewById(R.id.button08);
        button09 = (Button)getView().findViewById(R.id.button09);
        button10 = (Button)getView().findViewById(R.id.button10);
        button11 = (Button)getView().findViewById(R.id.button11);
        button12 = (Button)getView().findViewById(R.id.button12);
        button13 = (Button)getView().findViewById(R.id.button13);
        button14 = (Button)getView().findViewById(R.id.button14);
        button15 = (Button)getView().findViewById(R.id.button15);
        button16 = (Button)getView().findViewById(R.id.button16);
        button17 = (Button)getView().findViewById(R.id.button17);
        button18 = (Button)getView().findViewById(R.id.button18);



    // import sound files
    final MediaPlayer sound01 = MediaPlayer.create( getActivity(), R.raw.sound01);
    final MediaPlayer sound02 = MediaPlayer.create( getActivity(), R.raw.sound02);
    final MediaPlayer sound03 = MediaPlayer.create( getActivity(), R.raw.sound03);
    final MediaPlayer sound04 = MediaPlayer.create( getActivity(), R.raw.sound04);
    final MediaPlayer sound05 = MediaPlayer.create( getActivity(), R.raw.sound05);
    final MediaPlayer sound06 = MediaPlayer.create( getActivity(), R.raw.sound06);
    final MediaPlayer sound07 = MediaPlayer.create( getActivity(), R.raw.sound07);
    final MediaPlayer sound08 = MediaPlayer.create( getActivity(), R.raw.sound08);
    final MediaPlayer sound09 = MediaPlayer.create( getActivity(), R.raw.sound09);
    final MediaPlayer sound10 = MediaPlayer.create( getActivity(), R.raw.sound10);
    final MediaPlayer sound11 = MediaPlayer.create( getActivity(), R.raw.sound11);
    final MediaPlayer sound12 = MediaPlayer.create( getActivity(), R.raw.sound12);
    final MediaPlayer sound13 = MediaPlayer.create( getActivity(), R.raw.sound13);
    final MediaPlayer sound14 = MediaPlayer.create( getActivity(), R.raw.sound14);
    final MediaPlayer sound15 = MediaPlayer.create( getActivity(), R.raw.sound15);
    final MediaPlayer sound16 = MediaPlayer.create( getActivity(), R.raw.sound16);
    final MediaPlayer sound17 = MediaPlayer.create( getActivity(), R.raw.sound17);
 // play sound files on clicks


    Button s02 = (Button) getView().findViewById(R.id.button02); 
    s02.setText(this.getString(R.string.quote02));
    s02.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            try {
                sound01.prepare();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            sound01.start();                
            }

    });
    registerForContextMenu(s02);

    Button s03 = (Button) getView().findViewById(R.id.button03); 
    s03.setText(this.getString(R.string.quote03));
    s03.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            try {
                sound02.prepare();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            sound02.start();                
            }
    });
    registerForContextMenu(s03);

    Button s04 = (Button) getView().findViewById(R.id.button04); 
    s04.setText(this.getString(R.string.quote04));
    s04.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            try {
                sound03.prepare();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            sound03.start();                
            }
    });
    registerForContextMenu(s04);

    Button s05 = (Button) getView().findViewById(R.id.button05); 
    s05.setText(this.getString(R.string.quote05));
    s05.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            try {
                sound04.prepare();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            sound04.start();                
            }
    });
    registerForContextMenu(s05);

    Button s06 = (Button) getView().findViewById(R.id.button06); 
    s06.setText(this.getString(R.string.quote06));
    s06.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            try {
                sound05.prepare();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            sound05.start();                
            }
    });
    registerForContextMenu(s06);

    Button s07 = (Button) getView().findViewById(R.id.button07); 
    s07.setText(this.getString(R.string.quote07));
    s07.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            try {
                sound06.prepare();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            sound06.start();                
            }
    });
    registerForContextMenu(s07);
    Button s08 = (Button) getView().findViewById(R.id.button08); 
    s08.setText(this.getString(R.string.quote08));
    s08.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            try {
                sound07.prepare();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            sound07.start();                
            }
    });
    registerForContextMenu(s08);
    Button s09 = (Button) getView().findViewById(R.id.button09); 
    s09.setText(this.getString(R.string.quote09));
    s09.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            try {
                sound08.prepare();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            sound08.start();                
            }
    });
    registerForContextMenu(s09);
    Button s10 = (Button) getView().findViewById(R.id.button10); 
    s10.setText(this.getString(R.string.quote10));
    s10.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            try {
                sound09.prepare();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            sound09.start();                
            }
    });
    registerForContextMenu(s10);
    Button s11 = (Button) getView().findViewById(R.id.button11); 
    s11.setText(this.getString(R.string.quote11));
    s11.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            try {
                sound10.prepare();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            sound10.start();                
            }
    });
    registerForContextMenu(s11);
    Button s12 = (Button) getView().findViewById(R.id.button12); 
    s12.setText(this.getString(R.string.quote12));
    s12.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            try {
                sound11.prepare();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            sound11.start();                
            }
    });
    registerForContextMenu(s12);
    Button s13 = (Button) getView().findViewById(R.id.button13); 
    s13.setText(this.getString(R.string.quote13));
    s13.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            try {
                sound12.prepare();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            sound12.start();                
            }
    });
    registerForContextMenu(s13);
    Button s14 = (Button) getView().findViewById(R.id.button14); 
    s14.setText(this.getString(R.string.quote14));
    s14.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            try {
                sound13.prepare();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            sound13.start();                
            }
    });
    registerForContextMenu(s14);
    Button s15 = (Button) getView().findViewById(R.id.button15); 
    s15.setText(this.getString(R.string.quote15));
    s15.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            try {
                sound14.prepare();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            sound14.start();                
            }
    });
    registerForContextMenu(s15);
    Button s16 = (Button) getView().findViewById(R.id.button16); 
    s16.setText(this.getString(R.string.quote16));
    s16.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            try {
                sound15.prepare();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            sound15.start();                
            }
    });
    registerForContextMenu(s16);
    Button s17 = (Button) getView().findViewById(R.id.button17); 
    s17.setText(this.getString(R.string.quote17));
    s17.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            try {
                sound16.prepare();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            sound16.start();                
            }
    });
    registerForContextMenu(s17);
    Button s18 = (Button) getView().findViewById(R.id.button18); 
    s18.setText(this.getString(R.string.quote18));
    s18.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            try {
                sound17.prepare();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            sound17.start();                
            }
    });
    registerForContextMenu(s18);





    final Button s01 = (Button) getView().findViewById(R.id.button01);
    s01.setText(this.getString(R.string.quote01));
    s01.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            if(sound01.isPlaying())
            {
    sound01.pause();
    sound01.seekTo(0);
            }
            if(sound02.isPlaying())
            {
    sound02.pause();
    sound02.seekTo(0);
            }
            if(sound03.isPlaying())
            {
    sound03.pause();
    sound03.seekTo(0);
            }
            if(sound04.isPlaying())
            {
    sound04.pause();
    sound04.seekTo(0);
            };
            if(sound05.isPlaying())
            {
    sound05.pause();
    sound05.seekTo(0);
            }
            if(sound06.isPlaying())
            {
    sound06.pause();
    sound06.seekTo(0);
            }
            if(sound07.isPlaying())
            {
    sound07.pause();
    sound07.seekTo(0);
            }
            if(sound08.isPlaying())
            {
    sound08.pause();
    sound08.seekTo(0);
            }
            if(sound09.isPlaying())
            {
    sound09.pause();
    sound09.seekTo(0);
            }
            if(sound10.isPlaying())
            {
    sound10.pause();
    sound10.seekTo(0);
            }
            if(sound11.isPlaying())
            {
    sound11.pause();
    sound11.seekTo(0);
            }
            if(sound12.isPlaying())
            {
    sound12.pause();
    sound12.seekTo(0);
            }
            if(sound13.isPlaying())
            {
    sound13.pause();
    sound13.seekTo(0);
            }
            if(sound14.isPlaying())
            {
    sound14.pause();
    sound14.seekTo(0);
            }
            if(sound15.isPlaying())
            {
    sound15.pause();
    sound15.seekTo(0);
            }
            if(sound16.isPlaying())
            {
    sound16.pause();
    sound16.seekTo(0);
            }
            if(sound17.isPlaying())
            {
    sound17.pause();
    sound17.seekTo(0);
            }

        } });
    registerForContextMenu(s01);
    }
    // perform save functions on long press
    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/Notification");    
        }  

    @Override  
    public boolean onContextItemSelected(MenuItem item) {  
        if(item.getTitle()=="Ringtone/Notification"){function1(item.getItemId());}  
        else {return false;}  
    return true;  
    }  

    // detect which button was clicked, save as a ringtone with strings.xml
    public boolean function1(int ressound){  

        //----
              String soundname = "";

               switch(ressound){
            case R.id.button02:
            ressound = R.raw.sound01;
            soundname = (this.getString(R.string.app_name)) + " - " + (this.getString(R.string.quote02));
            break;
            }

               switch(ressound){
            case R.id.button03:
            ressound = R.raw.sound02;
            soundname = (this.getString(R.string.app_name)) + " - " + (this.getString(R.string.quote03));
            break;
            }
               switch(ressound){
            case R.id.button04:
            ressound = R.raw.sound03;
            soundname = (this.getString(R.string.app_name)) + " - " + (this.getString(R.string.quote04));
            break;
            }

           switch(ressound){
            case R.id.button05:
            ressound = R.raw.sound04;
            soundname = (this.getString(R.string.app_name)) + " - " + (this.getString(R.string.quote05));
            break;
            }
           switch(ressound){
            case R.id.button06:
            ressound = R.raw.sound05;
            soundname = (this.getString(R.string.app_name)) + " - " + (this.getString(R.string.quote06));
            break;
            }
           switch(ressound){
            case R.id.button07:
            ressound = R.raw.sound06;
            soundname = (this.getString(R.string.app_name)) + " - " + (this.getString(R.string.quote07));
            break;
            }
           switch(ressound){
            case R.id.button08:
            ressound = R.raw.sound07;
            soundname = (this.getString(R.string.app_name)) + " - " + (this.getString(R.string.quote08));
            break;
            }
           switch(ressound){
            case R.id.button09:
            ressound = R.raw.sound08;
            soundname = (this.getString(R.string.app_name)) + " - " + (this.getString(R.string.quote09));
            break;
            }
           switch(ressound){
            case R.id.button10:
            ressound = R.raw.sound09;
            soundname = (this.getString(R.string.app_name)) + " - " + (this.getString(R.string.quote10));
            break;
            }
           switch(ressound){
            case R.id.button11:
            ressound = R.raw.sound10;
            soundname = (this.getString(R.string.app_name)) + " - " + (this.getString(R.string.quote11));
            break;
            }
           switch(ressound){
            case R.id.button12:
            ressound = R.raw.sound11;
            soundname = (this.getString(R.string.app_name)) + " - " + (this.getString(R.string.quote12));
            break;
            }
           switch(ressound){
            case R.id.button13:
            ressound = R.raw.sound12;
            soundname = (this.getString(R.string.app_name)) + " - " + (this.getString(R.string.quote13));
            break;
            }
           switch(ressound){
            case R.id.button14:
            ressound = R.raw.sound13;
            soundname = (this.getString(R.string.app_name)) + " - " + (this.getString(R.string.quote14));
            break;
            }
           switch(ressound){
            case R.id.button15:
            ressound = R.raw.sound14;
            soundname = (this.getString(R.string.app_name)) + " - " + (this.getString(R.string.quote15));
            break;
            }
           switch(ressound){
            case R.id.button16:
            ressound = R.raw.sound15;
            soundname = (this.getString(R.string.app_name)) + " - " + (this.getString(R.string.quote16));
            break;
            }
           switch(ressound){
            case R.id.button17:
            ressound = R.raw.sound16;
            soundname = (this.getString(R.string.app_name)) + " - " + (this.getString(R.string.quote17));
            break;
            }
           switch(ressound){
            case R.id.button18:
            ressound = R.raw.sound17;
            soundname = (this.getString(R.string.app_name)) + " - " + (this.getString(R.string.quote18));
            break;
            }





              //and so on and so on.....
              byte[] buffer=null;
               InputStream fIn = getActivity().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=soundname+".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;
               }

               getActivity().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, soundname);
               values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");
               values.put(MediaStore.Audio.Media.ARTIST, "NapoleonHill");
               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
               getActivity().getContentResolver().insert(MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath()), values);


              String i = "Saved as Ringtone.";
               Toast.makeText(getActivity().getApplicationContext(), i,
               Toast.LENGTH_LONG).show();

             return true;


              }

        //----  

    }

解决方案

You need to switch

item.getTitle() == "Ringtone/Notification"

to

"Ringtone/Notification".equals(item.getTitle());

The following question is a good answer to why: How do I compare strings in Java?

这篇关于在Eclipse中使用媒体播放器的音频无法保存到手机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆