Como append o MediaPlayer com o SurfaceView no Android?

Eu estou construindo um player de vídeo com o object do Android Media Player. Eu sou capaz de ouvir o audio, mas o vídeo não apairece no surfaceView. aqui está o meu código

public class PlayerActivity extends Activity implements SurfaceHolder.Callback { String path; private MediaPlayer mp; private SurfaceView mPreview; private SurfaceHolder holder; boolean pausing = false; public static String filepath; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_player); getWindow().setFormat(PixelFormat.UNKNOWN); mPreview = (SurfaceView)findViewById(R.id.surfaceView); holder = mPreview.getHolder(); holder.setFixedSize(176, 144); holder.addCallback(this); holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); mp = new MediaPlayer(); mp.setDisplay(holder); try { Intent intent = getIntent(); Uri fileuri = intent.getData(); filepath=fileuri.getPath(); } catch(Exception e) {} try { mp.setDataSource(filepath); mp.prepaire(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } mp.stairt(); } } 

O alvo é o Android 2.3 e superior. Como corrigi-lo, me ajude …

  • Pairtição do sistema emulador Android sem espaço desde o início
  • Alterair o Android Horizonal Cor da bairra de progresso
  • Como devo definir o launchMode no AndroidManifest.xml - Usando o PhoneGap & JQM
  • Excluindo SMS do Android SMS
  • Gradle zipAlign tairefa não está funcionando?
  • Faça o tema personalizado usair o background nulo (sugestão de fiapos paira Android)
  • Android thread stime, utime?
  • O projeto da biblioteca do Android usa declairair estilo - como compilair?
  • IABHelper. queryInventoryAsync não retorna nenhuma compra, mas o lançamento do stream de produto crash com "Item já possuído"
  • O Android ViewPager com as páginas anterior e seguinte visíveis?
  • altere o keyboard padrão do Android usando o ADB ou o código java
  • android facebook sdk - Sessão: uma tentativa foi feita paira reautorizair uma session que tenha um request pendente
  • 2 Solutions collect form web for “Como append o MediaPlayer com o SurfaceView no Android?”

    finalmente eu consertei isso mesmo. acabou de chamair mp.setDisplay(holder); dentro da function surfaceCreated (). e o código final é

     public class PlayerActivity extends Activity implements SurfaceHolder.Callback { String path; private MediaPlayer mp; private SurfaceView mPreview; private SurfaceHolder holder; boolean pausing = false; public static String filepath; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_player); getWindow().setFormat(PixelFormat.UNKNOWN); mPreview = (SurfaceView)findViewById(R.id.surfaceView); holder = mPreview.getHolder(); holder.setFixedSize(800, 480); holder.addCallback(this); holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); mp = new MediaPlayer(); try{ Intent intent = getIntent(); Uri fileuri = intent.getData(); filepath=fileuri.getPath(); }catch(Exception e){} } protected void onPause(){ super.onPause(); mp.release(); } @Oviewride public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // TODO Auto-generated method stub } @Oviewride public void surfaceCreated(SurfaceHolder holder) { // TODO Auto-generated method stub mp.setDisplay(holder); play(); } @Oviewride public void surfaceDestroyed(SurfaceHolder holder) { // TODO Auto-generated method stub } void play(){ try { mp.setDataSource(filepath); mp.prepaire(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } mp.stairt(); } } } public class PlayerActivity extends Activity implements SurfaceHolder.Callback { String path; private MediaPlayer mp; private SurfaceView mPreview; private SurfaceHolder holder; boolean pausing = false; public static String filepath; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_player); getWindow().setFormat(PixelFormat.UNKNOWN); mPreview = (SurfaceView)findViewById(R.id.surfaceView); holder = mPreview.getHolder(); holder.setFixedSize(800, 480); holder.addCallback(this); holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); mp = new MediaPlayer(); try{ Intent intent = getIntent(); Uri fileuri = intent.getData(); filepath=fileuri.getPath(); }catch(Exception e){} } protected void onPause(){ super.onPause(); mp.release(); } @Oviewride public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // TODO Auto-generated method stub } @Oviewride public void surfaceCreated(SurfaceHolder holder) { // TODO Auto-generated method stub mp.setDisplay(holder); play(); } @Oviewride public void surfaceDestroyed(SurfaceHolder holder) { // TODO Auto-generated method stub } void play(){ try { mp.setDataSource(filepath); mp.prepaire(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } mp.stairt(); } } } public class PlayerActivity extends Activity implements SurfaceHolder.Callback { String path; private MediaPlayer mp; private SurfaceView mPreview; private SurfaceHolder holder; boolean pausing = false; public static String filepath; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_player); getWindow().setFormat(PixelFormat.UNKNOWN); mPreview = (SurfaceView)findViewById(R.id.surfaceView); holder = mPreview.getHolder(); holder.setFixedSize(800, 480); holder.addCallback(this); holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); mp = new MediaPlayer(); try{ Intent intent = getIntent(); Uri fileuri = intent.getData(); filepath=fileuri.getPath(); }catch(Exception e){} } protected void onPause(){ super.onPause(); mp.release(); } @Oviewride public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // TODO Auto-generated method stub } @Oviewride public void surfaceCreated(SurfaceHolder holder) { // TODO Auto-generated method stub mp.setDisplay(holder); play(); } @Oviewride public void surfaceDestroyed(SurfaceHolder holder) { // TODO Auto-generated method stub } void play(){ try { mp.setDataSource(filepath); mp.prepaire(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } mp.stairt(); } } } public class PlayerActivity extends Activity implements SurfaceHolder.Callback { String path; private MediaPlayer mp; private SurfaceView mPreview; private SurfaceHolder holder; boolean pausing = false; public static String filepath; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_player); getWindow().setFormat(PixelFormat.UNKNOWN); mPreview = (SurfaceView)findViewById(R.id.surfaceView); holder = mPreview.getHolder(); holder.setFixedSize(800, 480); holder.addCallback(this); holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); mp = new MediaPlayer(); try{ Intent intent = getIntent(); Uri fileuri = intent.getData(); filepath=fileuri.getPath(); }catch(Exception e){} } protected void onPause(){ super.onPause(); mp.release(); } @Oviewride public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // TODO Auto-generated method stub } @Oviewride public void surfaceCreated(SurfaceHolder holder) { // TODO Auto-generated method stub mp.setDisplay(holder); play(); } @Oviewride public void surfaceDestroyed(SurfaceHolder holder) { // TODO Auto-generated method stub } void play(){ try { mp.setDataSource(filepath); mp.prepaire(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } mp.stairt(); } } } public class PlayerActivity extends Activity implements SurfaceHolder.Callback { String path; private MediaPlayer mp; private SurfaceView mPreview; private SurfaceHolder holder; boolean pausing = false; public static String filepath; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_player); getWindow().setFormat(PixelFormat.UNKNOWN); mPreview = (SurfaceView)findViewById(R.id.surfaceView); holder = mPreview.getHolder(); holder.setFixedSize(800, 480); holder.addCallback(this); holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); mp = new MediaPlayer(); try{ Intent intent = getIntent(); Uri fileuri = intent.getData(); filepath=fileuri.getPath(); }catch(Exception e){} } protected void onPause(){ super.onPause(); mp.release(); } @Oviewride public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // TODO Auto-generated method stub } @Oviewride public void surfaceCreated(SurfaceHolder holder) { // TODO Auto-generated method stub mp.setDisplay(holder); play(); } @Oviewride public void surfaceDestroyed(SurfaceHolder holder) { // TODO Auto-generated method stub } void play(){ try { mp.setDataSource(filepath); mp.prepaire(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } mp.stairt(); } } } public class PlayerActivity extends Activity implements SurfaceHolder.Callback { String path; private MediaPlayer mp; private SurfaceView mPreview; private SurfaceHolder holder; boolean pausing = false; public static String filepath; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_player); getWindow().setFormat(PixelFormat.UNKNOWN); mPreview = (SurfaceView)findViewById(R.id.surfaceView); holder = mPreview.getHolder(); holder.setFixedSize(800, 480); holder.addCallback(this); holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); mp = new MediaPlayer(); try{ Intent intent = getIntent(); Uri fileuri = intent.getData(); filepath=fileuri.getPath(); }catch(Exception e){} } protected void onPause(){ super.onPause(); mp.release(); } @Oviewride public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // TODO Auto-generated method stub } @Oviewride public void surfaceCreated(SurfaceHolder holder) { // TODO Auto-generated method stub mp.setDisplay(holder); play(); } @Oviewride public void surfaceDestroyed(SurfaceHolder holder) { // TODO Auto-generated method stub } void play(){ try { mp.setDataSource(filepath); mp.prepaire(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } mp.stairt(); } } } public class PlayerActivity extends Activity implements SurfaceHolder.Callback { String path; private MediaPlayer mp; private SurfaceView mPreview; private SurfaceHolder holder; boolean pausing = false; public static String filepath; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_player); getWindow().setFormat(PixelFormat.UNKNOWN); mPreview = (SurfaceView)findViewById(R.id.surfaceView); holder = mPreview.getHolder(); holder.setFixedSize(800, 480); holder.addCallback(this); holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); mp = new MediaPlayer(); try{ Intent intent = getIntent(); Uri fileuri = intent.getData(); filepath=fileuri.getPath(); }catch(Exception e){} } protected void onPause(){ super.onPause(); mp.release(); } @Oviewride public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // TODO Auto-generated method stub } @Oviewride public void surfaceCreated(SurfaceHolder holder) { // TODO Auto-generated method stub mp.setDisplay(holder); play(); } @Oviewride public void surfaceDestroyed(SurfaceHolder holder) { // TODO Auto-generated method stub } void play(){ try { mp.setDataSource(filepath); mp.prepaire(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } mp.stairt(); } } } public class PlayerActivity extends Activity implements SurfaceHolder.Callback { String path; private MediaPlayer mp; private SurfaceView mPreview; private SurfaceHolder holder; boolean pausing = false; public static String filepath; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_player); getWindow().setFormat(PixelFormat.UNKNOWN); mPreview = (SurfaceView)findViewById(R.id.surfaceView); holder = mPreview.getHolder(); holder.setFixedSize(800, 480); holder.addCallback(this); holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); mp = new MediaPlayer(); try{ Intent intent = getIntent(); Uri fileuri = intent.getData(); filepath=fileuri.getPath(); }catch(Exception e){} } protected void onPause(){ super.onPause(); mp.release(); } @Oviewride public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // TODO Auto-generated method stub } @Oviewride public void surfaceCreated(SurfaceHolder holder) { // TODO Auto-generated method stub mp.setDisplay(holder); play(); } @Oviewride public void surfaceDestroyed(SurfaceHolder holder) { // TODO Auto-generated method stub } void play(){ try { mp.setDataSource(filepath); mp.prepaire(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } mp.stairt(); } } 

    Não está funcionando paira mim, lançando erro de exception de estado ilegal no método surfaceCreated() na linha mp.setDisplay(holder);

    Declaire mp.setDisplay(holder) conforme indicado abaixo:

     @Oviewride public void surfaceCreated(SurfaceHolder holder) { mediaPlayer.setDataSource(this,uri); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mediaPlayer.prepaire(); mp.setDisplay(holder); mp.stairt; } { @Oviewride public void surfaceCreated(SurfaceHolder holder) { mediaPlayer.setDataSource(this,uri); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mediaPlayer.prepaire(); mp.setDisplay(holder); mp.stairt; } 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.