Media Player Fragement崩溃

时间:2015-06-30 22:32:58

标签: android android-fragments android-mediaplayer shoutcast

您可能已经看到我在几个月前创建媒体播放器时遇到了问题。好吧,我试图通过让它在片段中工作来进入下一个阶段,但应用程序一直停止/崩溃我。有谁可以帮助我吗?这是我的activity / fragment(代码隐藏)类

public class MediaPlayerNew extends Fragment implements 
android.media.MediaPlayer.OnCompletionListener, 
android.media.MediaPlayer.OnPreparedListener,
android.media.MediaPlayer.OnErrorListener,          
android.media.MediaPlayer.OnBufferingUpdateListener{

private MediaPlayer mp = null;
private String TAG = getClass().getSimpleName();
private Button play;
private Button pause;
private Button stop;
private TextView txtVStatus;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.activity_player, container, false);

    return view;
}

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    play = (Button) getActivity().findViewById(R.id.btnPlay);
    pause = (Button) getActivity().findViewById(R.id.btnPause);
    stop = (Button) getActivity().findViewById(R.id.btnStop);
    //status = (TextView) findViewById(R.id.txtVStatus);

pause.setEnabled(false);
stop.setEnabled(false);

play.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
        play();
    }
});

pause.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
        pause();
    }
});

stop.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
        stop();
    }
});
}

private void play() {
    //HTTP to BCRFM STREAM
    Uri myUri = Uri.parse("http://37.187.193.36:8002");
    try {
        if (mp == null) {
            this.mp = new android.media.MediaPlayer();
        } else {
            mp.stop();
            mp.reset();
        }
        mp.setDataSource(myUri.toString()); // Go to Initialized state)
        mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mp.setOnPreparedListener(this);
        mp.setOnBufferingUpdateListener(this);

        mp.setOnErrorListener(this);
        mp.prepareAsync();

        Log.d(TAG, "LoadClip Done");
    } catch (Throwable t) {
        Log.d(TAG, t.toString());
    }
}

@Override
public void onPrepared(android.media.MediaPlayer mp) {
    Log.d(TAG, "Stream is prepared");
    final TextView textViewToChange = (TextView) getActivity().findViewById(R.id.txtVStatus);
    textViewToChange.setText("Welcome to BCRfm");
    mp.start();
    play.setEnabled(false);
    pause.setEnabled(true);
    stop.setEnabled(true);

}

private void pause() {
    mp.pause();
    final TextView textViewToChange = (TextView) getActivity().findViewById(R.id.txtVStatus);
    textViewToChange.setText("You have paused your feed to BCRfm");
    play.setEnabled(true);
    pause.setEnabled(false);
    stop.setEnabled(true);
}

private void stop() {
    mp.stop();
    final TextView textViewToChange = (TextView) getActivity().findViewById(R.id.txtVStatus);
    textViewToChange.setText(" ");
    play.setEnabled(true);
    pause.setEnabled(false);
    stop.setEnabled(false);

}

@Override
public void onDestroy() {
    super.onDestroy();
    stop();
}

public void onCompletion(android.media.MediaPlayer mp) {
    stop();
}

public boolean onError(android.media.MediaPlayer mp, int what, int extra) {
    StringBuilder sb = new StringBuilder();
    sb.append("Media Player Error: ");
    switch (what) {
        case android.media.MediaPlayer.MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK:
            sb.append("Not Valid for Progressive Playback");
            break;
        case android.media.MediaPlayer.MEDIA_ERROR_SERVER_DIED:
            sb.append("Server Died");
            break;
        case android.media.MediaPlayer.MEDIA_ERROR_UNKNOWN:
            sb.append("Unknown");
            break;
        default:
            sb.append(" Non standard (");
            sb.append(what);
            sb.append(")");
    }
    sb.append(" (" + what + ") ");
    sb.append(extra);
    Log.e(TAG, sb.toString());
    return true;
}
public void onBufferingUpdate(android.media.MediaPlayer mp, int percent) {
    Log.d(TAG, "PlayerService onBufferingUpdate : " + percent + "%");
}
}

2 个答案:

答案 0 :(得分:0)

没关系,我修好了。

我改变了:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);......

public void onActivityCreated(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);..

它完美运行,唯一的问题是当我想从这个片段切换到另一个片段时,应用程序崩溃了。有什么提示吗?

修正了

更改

 @Override
  public void onDestroy() {
  super.onDestroy();
  stop();
}

 @Override
  public void onDestroy() {
  super.onDestroy();
}

答案 1 :(得分:0)

您的代码存在问题,为了清晰起见,我会给它们编号。

1)使用方法onCreateViewonCreate,至少(检查其他人!),将关键字@Override放在方法声明之上。请比较Google中的示例代码。 如果不这样做,也许编译器不会将其视为重写。

2)

没有任何问题
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

这是正常的代码。

3)改变自:

 @Override
  public void onDestroy() {
      super.onDestroy();
}

TO:

 @Override
  public void onDestroy() {
     stop();
     super.onDestroy();
}

主要区别在于stop()来电之前调用onDestroy()。当你破坏时,在所有其他代码之后调用destroy / destruct是标准的。 而且......你应该停止媒体播放器,否则即使在应用程序退出后它仍会继续播放。