I am having the following situation; if I go to my PlayerActivity (which consists of a SurfaceView and a custom media player) and try to start the video automatically at the end of onCreate
by triggering the method playAudio(View v)
connected to the Play button of my xml, the video starts but it can just be heard, no image can be seen. If I don´t start it automatically, and I press the Play button, the video plays without problems.
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(View 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>
Does anyone have an idea what might be going wrong here? Any workarounds?
My app needs to run on Android 2.3.5.
Seems like it needs sometime to start playing properly. In my onCreate
I added the following:
playButton.postDelayed(new Runnable() {
@Override
public void run() {
playButton.performClick();
}
}, 30);
Is interesting that my first try, using playButton.post(new Runnable(){...
did not work, and if you set the second parameter of postDelayed
to a value that is too small, it won´t work as it should neither.