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

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

问题描述

我正在尝试制作一个音板,用户可以在其中播放和停止声音,并将声音保存到手机。

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

我已经设置好了保存不行。我没有得到任何错误,不知道是什么错误。我得到盒子来保存它,但是当点击没有什么。使用ViewPagerIndicator。
任何帮助?
对不起只有一个初学者。

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;


              }

        //----  

    }


推荐答案

您需要切换

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

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

以下问题是一个很好的答案,为什么:如何比较Java中的字符串?

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

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

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