目前在做电视相关的视频切换,MediaPlayer+SurfaceView 切换一直有黑屏的现象。
电视盒子版本Android 4.0.3 还不支持硬加速(盒子本身问题),所以这个TextureView不能使用,surfaceView一直有黑屏问题。
stackoverflow两个Mediaplayer 无黑屏切换
public class StreamingActivity extends Activity implements SurfaceHolder.Callback, MediaPlayer.OnPreparedListener, MediaPlayer.OnCompletionListener
{
MediaPlayer mediaPlayerPrimary;
MediaPlayer mediaPlayerSecondary;
SurfaceHolder surfaceHolder;
SurfaceView surfaceView;
String videoSourcePrimary = "videoSourcePrimaryURL";
String videoSourceSecondary = "videoSourceSecondaryURL";
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.streaming_activity_layout);
/** Create SurfaceView */
surfaceView = (SurfaceView) findViewById(R.id.surface_view);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
}
@Override
public void surfaceCreated(SurfaceHolder arg0)
{
try
{
/** Create MediaPlayer(s) */
mediaPlayerPrimary = new MediaPlayer();
mediaPlayerSecondary = new MediaPlayer();
/** Attach primary MediaPlayer to SurfaceView */
mediaPlayerPrimary.setDisplay(surfaceHolder);
/** Set sources */
mediaPlayerPrimary.setDataSource(videoSourcePrimary);
mediaPlayerSecondary.setDataSource(videoSourceSecondary);
/** Prepare MediaPlayer(s) */
mediaPlayerPrimary.prepare();
mediaPlayerSecondary.prepareAsync();
/** Set listeners */
mediaPlayerPrimary.setOnPreparedListener(this);
mediaPlayerPrimary.setOnCompletionListener(this);
mediaPlayerSecondary.setOnPreparedListener(this);
mediaPlayerSecondary.setOnCompletionListener(this);
/** Set audio stream type */
mediaPlayerPrimary.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayerSecondary.setAudioStreamType(AudioManager.STREAM_MUSIC);
}
catch (Exception e)
{
e.printStackTrace();
}
}
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3)
{
}
@Override
public void surfaceDestroyed(SurfaceHolder arg0)
{
}
@Override
public void onPrepared(MediaPlayer mp)
{
if (mp == mediaPlayerPrimary)
{
/** First video ready, start playback */
mediaPlayerPrimary.start();
}
else if (mp == mediaPlayerSecondary)
{
/** Second video is ready */
}
}
@Override
public void onCompletion(MediaPlayer mp)
{
/** First video is completed, start second video */
mediaPlayerPrimary.release();
mediaPlayerSecondary.setDisplay(surfaceHolder);
mediaPlayerSecondary.start();
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.streaming_activity_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings)
{
return true;
}
return super.onOptionsItemSelected(item);
}
}