使用带有网址的MediaPlayer时,Android应用程序在5.0及更高版本中崩溃

时间:2015-07-22 10:22:06

标签: android android-5.0-lollipop

我正在开发一个使用远程网址播放mp3歌曲的Android应用程序。在api 5.0下面的Android设备中一切正常。但在三星s5(5.1)中启动应用程序时,它突然崩溃

mu logcat发出错误“QCmediaPlayer媒体播放器不存在。这是我的媒体播放器代码

public class MainActivity2 extends Activity implements OnClickListener, OnPreparedListener   {
    private ProgressBar playSeekBar;
    private final static String RADIO_STATION_URL ="https://aryaradio.s3.amazonaws.com/";
    private String KEYNAME,encodedurl;
    private List<String> playlistarray;
    private List<MediaPlayer> mplayerList;
    private ImageButton buttonPlay;
    private List<S3ObjectSummary> playlist=null;
    private ImageButton buttonStopPlay;
    ProgressDialog progress;
    URL url,currentsongurl;
    private MediaPlayer player;
    private AmazonS3Client mClient;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play);

        playlistarray=new ArrayList<String>();
        mplayerList=new ArrayList<MediaPlayer>();
        if(isNetworkAvailable()) {
            mClient = Util.getS3Client(MainActivity2.this);
            initializeUIElements();
            //new RefreshTask().execute();
            buttonStopPlay.setVisibility(View.INVISIBLE);
        } else {
            new AlertDialog.Builder(MainActivity2.this)
                .setTitle(getResources().getString(R.string.app_name))
                .setMessage(
                    getResources().getString(
                        R.string.internet_error))
                        .setPositiveButton("OK", null).show();
            buttonStopPlay.setVisibility(View.INVISIBLE);
            buttonPlay.setVisibility(View.INVISIBLE);
        }
    }

    private void initializeUIElements() {
        buttonPlay = (ImageButton) findViewById(R.id.Play);
        buttonPlay.setOnClickListener(this);
        buttonStopPlay = (ImageButton) findViewById(R.id.Stop);
        buttonStopPlay.setOnClickListener(this);
    }

    public void onClick(View v) {
        if (v == buttonPlay) {
            startPlaying();
        } else if (v == buttonStopPlay) {
            stopPlaying();
            finish();
            startActivity(new Intent(MainActivity2.this, MainActivity2.class));
        }
    }

    private void startPlaying() {
        buttonPlay.setVisibility(View.INVISIBLE);
        buttonStopPlay.setVisibility(View.VISIBLE);
        progress = new ProgressDialog(this);
        progress.setTitle("Message");
        progress.setMessage("Loading Song ...");
        progress.setCancelable(true);
        progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progress.show();

        player.prepareAsync();
        player.setOnPreparedListener(new OnPreparedListener() {
            public void onPrepared(MediaPlayer mp) {
                progress.dismiss();
                player.start();
                player.setOnCompletionListener(new OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mediaPlayer) {
                        try {
                           // player.setNextMediaPlayer(mplayerList.get(2));
                            stopPlaying();
                        } catch (IllegalArgumentException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (SecurityException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (IllegalStateException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } 
                    }
                });
            }
        });
    }

    private void stopPlaying() {
        killMediaPlayer();
        initializeMediaPlayer(playlistarray);
        buttonPlay.setVisibility(View.VISIBLE);
        buttonStopPlay.setVisibility(View.INVISIBLE);
    }

    private void initializeMediaPlayer(List<String> playlist) {
        player = new MediaPlayer();
        int noOfSongs=playlist.size();
        String url = "https://aryaradio.s3.amazonaws.com/us-east-1:eb604ac1-c4e3-4226-bea8-22f214a6b0b0/RecordingArya-9459.mp3.null";
        try {
            player.setAudioStreamType(AudioManager.STREAM_MUSIC);
            player.setDataSource(url);
            player.setOnPreparedListener(this);
            player.setOnErrorListener(new OnErrorListener() {
                @Override
                public boolean onError(MediaPlayer arg0, int arg1, int arg2) {
                    return false;
                }
            });
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        player.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {
            public void onBufferingUpdate(MediaPlayer mp, int percent) {
                Log.i("Buffering", "" + percent);
            }
        });
    }
}

0 个答案:

没有答案