MP3歌曲无法在媒体播放器中播放

时间:2015-06-18 07:03:14

标签: java android audio mp3 media-player

我正在尝试下面的代码 这是MainActivity.java

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

import javax.crypto.spec.PSource;

import android.R.string;
import android.support.v7.app.ActionBarActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterViewFlipper;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {

    ListView lv;
    String[] items;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv =  (ListView) findViewById(R.id.lvPlaylist);
        final ArrayList<File> mySongs = findSongs(Environment.getExternalStorageDirectory());
        items =  new String[mySongs.size () ];
        for (int i = 0; i < mySongs.size(); i++) {
            //toast(mySongs.get(i).getName().toString());
            items[i] = mySongs.get(i).getName().toString();
        }

        ArrayAdapter<String> adp = new ArrayAdapter<String>(getApplicationContext(),R.layout.list_item,R.id.textView1,items);
        lv.setAdapter(adp);
        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub
                startActivity(new Intent(getApplicationContext(), player.class).putExtra("pos", position).putExtra("songlist",mySongs));
            }
        });
    }

    public ArrayList<File> findSongs(File root){
        ArrayList al = new ArrayList<File>();
        File[] files = root.listFiles();
        for(File singleFile : files){
            if (singleFile.isDirectory()&& !singleFile.isHidden()) {
                al.addAll(findSongs(singleFile));

            }
            else {
                if (singleFile.getName().endsWith(".mp3") || singleFile.getName().endsWith(".wav")) {
                    al.add(singleFile);
                }
            }
        }
        return al;
    }

    public void toast(String text) {
        Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();

    }

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

上面的代码给我一个mp3文件列表,当我们点击播放器播放的歌曲之一时播放歌曲

和 player.java

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.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;
            /*if(position-1 < 0)

            {
                position = mySongs.size()-1;

            }
            else
            {
                position= position-1;
            }*/
            u = Uri.parse(mySongs.get(position).toString());
            mp = MediaPlayer.create(getApplicationContext(), u);
            mp.start();
        default:
            break;
        }
    }

}

虽然上面的代码适用于少数几首mp3的歌曲,但它不适用于其他也是mp3的歌曲。我在实际设备上试过这个,如果歌曲有问题,应用程序会崩溃 让我遇到问题的mp3文件在其他媒体播放器甚至默认的Android媒体播放器上都能完美运行

究竟可能是什么问题?

1 个答案:

答案 0 :(得分:0)

您需要在致电mediaPlayer.prepare()之前致电mediaPlayer.prepareAsync()mediaPlayer.start(),如果应用程序崩溃,请发布您的日志,以便我们了解问题。