简体   繁体   English

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

[英]MP3 songs do not play in media player

I am trying out below code here is the MainActivity.java 我正在尝试下面的代码,这里是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);
    }
}

above code gives me a list of mp3 files when we click on one of the songs it takes to player.ja and songs are played 当我们单击播放器中需要播放的歌曲之一时,上面的代码为我提供了mp3文件列表。

and player.java 和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;
        }
    }

}

While the above code works perfectly fine for few songs which are mp3 but it does not work for other songs which are also mp3 . 虽然上面的代码对于mp3的几首歌曲来说是完美的,但是对于mp3的其他歌曲却不起作用。 I have tried this on an actual device the app would just crash if the song has some problem the mp3 files which give me problems work perfectly on other media players and even the default android media player 我曾在实际设备上尝试过此操作,如果歌曲有问题,则mp3文件会使我的问题在其他媒体播放器,甚至是默认的android媒体播放器上正常运行,应用只会崩溃

What exactly could be the issue? 到底是什么问题?

您需要先调用mediaPlayer.prepare()mediaPlayer.prepareAsync()然后再调用mediaPlayer.start() ,如果应用程序崩溃,请发布日志,以便我们知道问题所在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM