我遇到以下情况;如果我转到我的PlayerActivity(由SurfaceView和自定义媒体播放器组成)并尝试通过触发连接到我的播放按钮的方法onCreate
在playAudio(View v)
结束时自动启动视频xml,视频开始但只能听到,没有图像可见。如果我不自动启动它,并按下“播放”按钮,则视频播放没有问题。
的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上运行。
答案 0 :(得分:0)
似乎需要一段时间才能开始正常播放。在我的onCreate
中,我添加了以下内容:
playButton.postDelayed(new Runnable() {
@Override
public void run() {
playButton.performClick();
}
}, 30);
有趣的是,我的第一次尝试,使用playButton.post(new Runnable(){...
没有用,如果你将postDelayed
的第二个参数设置为一个太小的值,它就不会工作,因为它不应该