簡體   English   中英

Android Media Player無法播放具有特殊字符的音頻文件

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

我正在嘗試下面的代碼

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;
        }
    }

}

如果沒有特殊字符,普通文件的播放效果很好,即使不存在特殊字符,數組中的上一個和下一個文件也可以正常播放

如果文件包含#,$和%等特殊字符,它將如何播放

問題似乎在這部分

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();

我該如何播放帶有特殊字符的文件

您可以嘗試為此進行url編碼,我尚未測試過,但可能可以幫上忙。

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

或者,如果它不能幫助您嘗試此操作,

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM