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 …

  • Quando usair FragmentManager :: putFragment e getFragment
  • Não é possível executair o aplicativo iónico 2 no emulador Android Genymotion
  • Título do títuloDivider in Dialog
  • O iOS tem algo pairecido com o RecyclerView do Android?
  • Não é possível view a tree de dependência com gradlew OU gradle
  • O que acontece com um database Sqlite quando o aplicativo é removido
  • Como concatenair ou merge dois ou mais files de vídeo no Android?
  • BaseAdapter.notifyDatasetChanged () não atualizando o ListView
  • Remova o espaçamento entre itens no RecyclerView android
  • Fundo preto na image cairregada com o cairregador de imagens univiewisal
  • onTaskRemoved () não está sendo chamado nos dispositivos HUAWEI e XIOMI
  • O repo de maven de tecido provoca compilações lentas de gradle
  • 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.