Search code examples
androiduriandroid-mediaplayerpreferenceringtone

Play Ringtone from preference not working, called from second activity on button press no errors


In the first activty a ringtone intent to select is made and in the second activity on button press I want to play the selection made from the first activity. I am getting no errors and button in the second activity does nothing.

Below is the relevant code:

    Button button2 = (Button)findViewById(R.id.button2);

    button2.setOnClickListener(new Button.OnClickListener(){


    @Override
    public void onClick(View v) {
    Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
    intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE,
    RingtoneManager.TYPE_NOTIFICATION);
    intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select
    Ringtone");
    if (mRingtoneUri != null) { 
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, 
    Uri.parse((String) mRingtoneUri));
    } else {
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, 
    (Uri) null);
    }
    startActivityForResult(intent, RINGTONE_REQUEST);
      }
      });
     }   

     protected void onActivityResult(int requestCode, int resultCode,
     Intent data, final String mRingtoneUri, Object RINGTONE, int
      RINGTONE_REQUEST) {
       if (requestCode == RINGTONE_REQUEST && resultCode == RESULT_OK) {
        final Uri uri = 
      data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
        String PREF = null;
        SharedPreferences preferences = getSharedPreferences(PREF, 
       MODE_PRIVATE);
        Editor editor = preferences.edit();
        if (uri == null)
            editor.putString((String) RINGTONE, null);
        else
            editor.putString((String) RINGTONE, uri.toString());
         editor.commit();


        Button PlayButton = (Button)  
        findViewById(R.id.Playbutton);
        PlayButton.setOnClickListener(new OnClickListener() {

            private Context context;

            @Override
            public void onClick(View view) {
                try {
                    mp.setDataSource(mRingtoneUri);
                } catch (IllegalArgumentException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (SecurityException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (IllegalStateException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();

                  }
                try {
                    mp.prepare();
                } catch (IllegalStateException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                 MediaPlayer.create(context, uri);
                mp.release();
                finish();




                 }});}
                }

Solution

  • In light of the above comment I will close this question and try to resolve the last issue. Hopefully the following code will help someone else facing this issue.

    Preference.xml
    
    <?xml version="1.0" encoding="utf-8"?>
    
    <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res
    /android"
    android:title="Settings">
    
    
     <PreferenceCategory android:title="Seconed Category">
        <RingtonePreference android:showDefault="true"
             android:key="ringtone" 
            android:title="Select sound"
            android:ringtoneType="ringtone">
        </RingtonePreference>
    
      </PreferenceCategory>
     </PreferenceScreen>
    
    Main.xml
    
    public class MyActivity extends Activity {
    
    protected MediaPlayer mp;
    protected String mRingtoneUri;
    protected Context context;
    protected Uri uri;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
    
        ((Button)findViewById(R.id.Button01)).setOnClickListener(new 
        OnClickListener() {
    
            @Override
            public void onClick(View v) {
                Intent settingsActivity = new Intent(getBaseContext(),
                        PreferencesActivityTest.class);
                startActivity(settingsActivity);
    
            }
        });
        }
    
       @Override
       protected void onResume() {
        super.onResume();
        PreferenceManager.getDefaultSharedPreferences(this);
    
    
      {
    
      }
    
    
      {
     ((Button)findViewById(R.id.button3)).setOnClickListener(new 
     OnClickListener() {
    
    
    
                @Override
                public void onClick(View v) {
    
                    SharedPreferences getAlarms = 
          PreferenceManager.getDefaultSharedPreferences(getBaseContext());
                    String alarms = getAlarms.getString("ringtone", "");
                        Uri.parse(alarms);
    
    
                    Toast.makeText(getApplicationContext(), alarms, 
          Toast.LENGTH_LONG).show();
    
    
                    Uri notification =   
    RingtoneManager.getActualDefaultRingtoneUri(getApplicationContext(),
     RingtoneManager.TYPE_NOTIFICATION);
                    MediaPlayer mp = 
    MediaPlayer.create(getApplicationContext(), notification);
                    mp.start();
    
    
                             }
    
                               });}}}
    
    
     Main.xml
    
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    
    <Button android:text="Show RingTone Preferences"   
    android:id="@+id/Button01"
        android:layout_width="wrap_content" 
    android:layout_height="wrap_content"></Button>
    
    
    <Button 
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Play" />
    
     </LinearLayout>