【发布时间】:2014-05-15 07:55:48
【问题描述】:
我有以下情况;如果我转到我的 PlayerActivity(由 SurfaceView 和自定义媒体播放器组成)并尝试通过触发连接到我的 xml 的播放按钮的方法 playAudio(View v) 在 onCreate 的末尾自动启动视频,视频开始,但只能听到,看不到图像。如果我没有自动启动它,我按下播放按钮,视频播放没有问题。
onCreate
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player);
targetUri = this.getIntent().getData();
context = this;
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
audioPlayer = (RelativeLayout) findViewById(R.id.audio_player);
playButton = (Button) findViewById(R.id.playButton);
pauseButton = (Button) findViewById(R.id.pauseButton);
stopButton = (ImageView) findViewById(R.id.stopButton);
seekbar = (SeekBar) findViewById(R.id.seekBarPlayer);
runningTime = (TextView) findViewById(R.id.audioRunningTime);
totalTime = (TextView) findViewById(R.id.audioTotalTime);
getWindow().setFormat(PixelFormat.UNKNOWN);
surfaceView = (SurfaceView) findViewById(R.id.surfaceview);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setFixedSize(176, 144);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
surfaceView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Make video controls appear / disappear
Log.d(TAG, "onClick del surfaceView");
if (playerVisible) {
audioPlayer.setVisibility(RelativeLayout.INVISIBLE);
playerVisible = false;
} else {
audioPlayer.setVisibility(RelativeLayout.VISIBLE);
playerVisible = true;
}
}
});
playButton.performClick(); // I also tried playAudio(playButton);
}
playAudio(查看 v)
public void playAudio(View v) {
playButton.setVisibility(View.INVISIBLE);
pauseButton.setVisibility(View.VISIBLE);
if (hasPressedPaused == true && hasPressedPlayed == true) {
Log.d(TAG, "Resume @: " + playbackPosition);
mediaPlayer.seekTo(playbackPosition);
mediaPlayer.start();
} else {
Log.d(TAG, "playAudio");
mediaPlayer = new MediaPlayer();
if (mediaPlayer.isPlaying()) {
mediaPlayer.reset();
}
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDisplay(surfaceHolder);
try {
mediaPlayer.setDataSource(getApplicationContext(), targetUri);
mediaPlayer.prepare();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.start();
finalTime = mediaPlayer.getDuration();
startTime = mediaPlayer.getCurrentPosition();
seekbar.setMax((int) finalTime);
hasPressedPlayed = true;
totalTime.setText(String.format(
"%d:%d",
TimeUnit.MILLISECONDS.toMinutes((long) finalTime),
TimeUnit.MILLISECONDS.toSeconds((long) finalTime)
- TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS
.toMinutes((long) finalTime))));
runningTime.setText(String.format(
"%d:%d",
TimeUnit.MILLISECONDS.toMinutes((long) startTime),
TimeUnit.MILLISECONDS.toSeconds((long) startTime)
- TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS
.toMinutes((long) startTime))));
seekbar.setProgress((int) startTime);
myHandler.postDelayed(UpdateSongTime, 100);
}
// Set the callback for the audio to finish playing
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer arg0) {
Intent stopPlay = new Intent(context, POIActivity.class);
startActivity(stopPlay);
}
});
}
activity_player.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${packageName}.${activityClass}" >
<SurfaceView
android:id="@+id/surfaceview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<RelativeLayout
android:id="@+id/audio_player"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#14ad8f" >
<TextView
android:id="@+id/audioRunningTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/playButton"
android:text="@string/initial_time"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/audioTotalTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/playButton"
android:text="@string/initial_time"
android:textAppearance="?android:attr/textAppearanceSmall" />
<SeekBar
android:id="@+id/seekBarPlayer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/playButton"
android:layout_toLeftOf="@+id/audioTotalTime"
android:layout_toRightOf="@+id/audioRunningTime" />
<Button
android:id="@+id/moreVolumeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/seekBarPlayer"
android:layout_alignRight="@+id/seekBarPlayer"
android:onClick="adjustVolume"
android:text="+" />
<ImageView
android:id="@+id/stopButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/seekBarPlayer"
android:layout_marginRight="42dp"
android:layout_toLeftOf="@+id/moreVolumeButton"
android:onClick="stopAudio"
android:src="@drawable/stop_button"
android:text="STOP" />
<Button
android:id="@+id/pauseButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginRight="36dp"
android:layout_toLeftOf="@+id/stopButton"
android:onClick="pauseAudio"
android:text="PAUSE"
android:visibility="invisible" />
<Button
android:id="@+id/playButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginRight="36dp"
android:layout_toLeftOf="@+id/stopButton"
android:onClick="playAudio"
android:text="PLAY" />
<Button
android:id="@+id/lessVolumeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/seekBarPlayer"
android:layout_alignLeft="@+id/seekBarPlayer"
android:onClick="adjustVolume"
android:text="-" />
</RelativeLayout>
</RelativeLayout>
有人知道这里可能出了什么问题吗?有什么解决方法吗?
我的应用需要在 Android 2.3.5 上运行。
【问题讨论】:
标签: android android-mediaplayer surfaceview android-audiomanager