android media player is not playing audio file which has special character

264 Views Asked by At

i am trying out below code

import java.io.File;
import java.util.ArrayList;

import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter.ViewBinder;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.SeekBar;

public class player extends ActionBarActivity implements View.OnClickListener{
    static MediaPlayer mp;
    ArrayList<File> mySongs;
    int position;
    Uri u;

    SeekBar sb;
    Button btPv, btFB,btplay,btFF, btNxt;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_player);
        btFB = (Button) findViewById(R.id.btFB);
        btPv = (Button) findViewById(R.id.btPv);
        btplay = (Button) findViewById(R.id.btplay);
        btFF = (Button) findViewById(R.id.btFF);
        btNxt = (Button) findViewById(R.id.btNxt);

        btFB.setOnClickListener(this) ;
        btPv.setOnClickListener(this);
        btplay.setOnClickListener(this);
        btFF.setOnClickListener(this);
        btNxt.setOnClickListener(this);
//      
        if(mp != null)
        {
            mp.stop();
            mp.release();

        }


        Intent i = getIntent();
        Bundle  b = i.getExtras();
        mySongs = (ArrayList) b.getParcelableArrayList("songlist");
        position = b.getInt("pos", 0);
        u = Uri.parse(mySongs.get(position).toString());
        //mp = MediaPlayer.create(getApplicationContext(), u).start();
        mp.create(this, u).start();
        //mp.start();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    @Override
    public void onClick(View v)
    {
        int id = v.getId();
        switch (id) {
        case R.id.btplay:
            if(mp.isPlaying())
            {btplay.setText("play"); 
                mp.pause(); 
            }
            else
            {   btplay.setText("play"); 
                mp.start();
            }
            break;
        case R.id.btFF:
            mp.seekTo(mp.getCurrentPosition()+ 5000);
            break;
        case R.id.btFB:
            mp.seekTo(mp.getCurrentPosition()- 5000);
            break;
        case R.id.btNxt:
            mp.stop();
            mp.release();
            position = (position+1)%mySongs.size();
            u = Uri.parse(mySongs.get(position).toString());
            mp = MediaPlayer.create(getApplicationContext(), u);
            mp.start();
            break;
        case R.id.btPv :
            mp.stop();
            mp.release();
            // Important
            position =  (position-1<0) ?  mySongs.size()-1: position-1;

            u = Uri.parse(mySongs.get(position).toString());
            mp = MediaPlayer.create(getApplicationContext(), u);
            mp.start();
        default:
            break;
        }
    }

}

normal files play very well if there are no special characters, even the previous and next files , from the array play well, if no special characters are present

how ever if the files contain special characters like #,$ , % etc it wont play

the problem seems in this part

Intent i = getIntent();
            Bundle  b = i.getExtras();
            mySongs = (ArrayList) b.getParcelableArrayList("songlist");
            position = b.getInt("pos", 0);
            u = Uri.parse(mySongs.get(position).toString());
            //mp = MediaPlayer.create(getApplicationContext(), u).start();
            mp.create(this, u).start();

how can i make these files with special characters play

1

There are 1 best solutions below

2
On

You can try url encoding for this, i have not tested but may be it can help.

URLEncoder.encode(song_url, "UTF-8");

or if it cant help then try this,

URI uri = new URI(song_url); 
mediaPlayer.setDataSource(uri.toASCIIString());