2011-10-30 26 views
1

アンドロイドシミュレータが黒い画面のサウンドを再生するときにのみビデオを再生します。私のコードは以下の通りです。私はlogcatに取得するように見えるビデオを再生するときにのみ黒い画面のサウンドを表示するアンドロイドシミュレータ

エラーは以下のとおりです。

10-30 06:49:35.663: W/GraphicBufferAllocator(33): alloc(160, 128, 842094169, 00002930, ...) failed -22 (Invalid argument) 
10-30 06:49:35.663: E/SurfaceFlinger(33): GraphicBufferAlloc::createGraphicBuffer(w=160, h=128) failed (Invalid argument), handle=0x0 
10-30 06:49:35.663: E/SurfaceTexture(33): [SurfaceView] dequeueBuffer: SurfaceComposer::createGraphicBuffer failed 
10-30 06:49:35.673: W/SoftwareRenderer(35): Surface::dequeueBuffer returned error -22 

ビデオの高さと幅は、私は多分アンドロイドがこのサイズのビデオを再生することはできません考えていた

10-30 06:49:35.223: D/Video Height:(595): 128 
10-30 06:49:35.223: D/Video Width:(595): 160 

です(それはかなり小さいですが)それはばかげているようです。私はいくつかのビデオを試してみましたが、それらのいずれも再生できません(mp4、または3gp)。私はその音だけを聞く。ビデオは黒です。私はVLCでそれらを再生することができますので、ビデオファイルは正常です。ちょうど私はアンドロイドシミュレータでこのメディアプレイヤーアプリでそれらを再生することはできません。

私はAVDとしてAndroid 4.0を使用しています。つまりPC上のシミュレータです。


package com.jevan.mediaplayer; 

import java.io.IOException; 
import android.app.Activity; 
import android.content.res.AssetFileDescriptor; 
import android.media.AudioManager; 
import android.media.MediaPlayer; 
import android.media.MediaPlayer.OnPreparedListener; 
import android.media.MediaPlayer.OnVideoSizeChangedListener; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.SurfaceHolder; 
import android.view.SurfaceView; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 

public class MediaPlayerActivity extends Activity 
    implements SurfaceHolder.Callback, OnPreparedListener, 
    OnVideoSizeChangedListener { 
/** Called when the activity is first created. */ 
private int mVideoWidth=0; 
private int mVideoHeight=0; 
private MediaPlayer mp = null; 
private SurfaceHolder holder = null; 
private SurfaceView sv = null; 
private boolean mIsVideoReadyToBePlayed = false; 
private boolean mIsVideoSizeKnown = false; 
private Button myButton = null; 

    public void surfaceCreated(SurfaceHolder holder) { 
     /*MediaPlayer mediaPlayer = MediaPlayer.create(context, uri); 
     mediaPlayer.start(); // no need to call prepare(); create() does that for you 
     */ 
     Log.d("surfaceCreated()", "surfaceCreated called"); 
     mp = new MediaPlayer(); 

     AssetFileDescriptor afd = null; 
     try { 
      afd = getAssets().openFd("747.3gp"); 
     } catch (IOException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 
     try { 
      mp.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength()); 
     } catch (IllegalArgumentException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } catch (IllegalStateException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } catch (IOException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 
     try { 
      afd.close(); 
     } catch (IOException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 

     /* 
     try { 
      mp.setDataSource("http://www.mp4point.com/downloads/d7c320246079.mp4"); 
     } catch (IllegalArgumentException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } catch (SecurityException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } catch (IllegalStateException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } catch (IOException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 
     */ 

     mp.setDisplay(holder); 
     try { 
      mp.prepare(); 
     } catch (IllegalStateException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     mp.setOnPreparedListener(this); 
     mp.setOnVideoSizeChangedListener(this); 

     mp.setAudioStreamType(AudioManager.STREAM_MUSIC); 

     /*setVolumeControlStream(AudioManager.STREAM_MUSIC);*/ 

     mp.setScreenOnWhilePlaying(true); 

     Log.d("Video Height:", Integer.toString(mp.getVideoHeight())); 
     Log.d("Video Width:", Integer.toString(mp.getVideoWidth())); 

     mp.setLooping(false); 

     //mp.start(); 
     // i.e. react on the end of the music-file: 
     /* 
     mp.setOnCompletionListener(new 
       OnCompletionListener(){ 
      public void onCompletion(MediaPlayer arg0) { 
       // File has ended 
      } 
     }); 
     */ 

     Log.d("surfaceCreated()", "surfaceCreated finishing"); 
    } 

    public void surfaceChanged(SurfaceHolder holder, 
      int format, int width, int height) { 
     // TODO Auto-generated method stub 

    } 

    public void surfaceDestroyed(SurfaceHolder holder) { 
     // TODO Auto-generated method stub 

    } 

    public void onPrepared(MediaPlayer mediaplayer) { 
     Log.d("onPrepared", "onPrepared called"); 
     mIsVideoReadyToBePlayed = true; 
     if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) { 
      startVideoPlayback(mediaplayer); 
     } 
     Log.d("onPrepared", "onPrepared finishing"); 
    } 

    private void startVideoPlayback(MediaPlayer mediaplayer) { 
     if (!(mediaplayer.isPlaying())) { 
      holder.setFixedSize(mVideoWidth, mVideoHeight); 
      mediaplayer.start(); 
     } 
    } 

    public void onVideoSizeChanged(MediaPlayer mp, int width, int height) { 
     Log.d("onVideoSizeChanged", 
       "onVideoSizeChanged called (Width "+Integer.toString(width)+" Height "+Integer.toString(height)+")"); 
     if (width == 0 || height == 0) { 
      Log.e("Log", "invalid video width(" + width + ") or height(" + height + ")"); 
      return; 
     } 
     mIsVideoSizeKnown = true; 
     mVideoWidth = width; 
     mVideoHeight = height; 
     if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) { 
      startVideoPlayback(mp); 
     } 
    } 

    private void releaseMediaPlayer() { 
     Log.d("releaseMediaPlayer", "releaseMediaPlayer called"); 
     if (mp != null) { 
      mp.release(); 
      mp = null; 
      Log.d("releaseMediaPlayer", "mp release has been called and mp made null"); 
     } 
    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
     releaseMediaPlayer(); 
     doCleanUp(); 
    } 

    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 
     releaseMediaPlayer(); 
     doCleanUp(); 
    } 

    private void doCleanUp() { 
     mVideoWidth = 0; 
     mVideoHeight = 0; 
     mIsVideoReadyToBePlayed = false; 
     mIsVideoSizeKnown = false; 
    } 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    Log.d("onCreate", "onCreate called"); 
    sv = (SurfaceView) findViewById(R.id.surface_view); 
    holder = sv.getHolder(); 
    holder.addCallback(this); 
    holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 

    myButton = (Button)this.findViewById(R.id.button); 
    myButton.setOnClickListener(new OnClickListener(){ 
      public void onClick(View arg0) { 
       if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) { 
        startVideoPlayback(mp); 
       } 
      } 
    }); 
    Log.d("onCreate", "onCreate finishing"); 
} 
} 

は私のmain.xmlは、次のようになります。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" android:layout_height="fill_parent"> 

<Button android:layout_width="fill_parent" 
    android:layout_height="wrap_content" android:id="@+id/button" 
    android:text="Start Video" android:layout_alignParentBottom="true" /> 

<SurfaceView android:id="@+id/surface_view" 
     android:layout_height="fill_parent" 
     android:layout_width="fill_parent" 
     android:layout_above="@+id/button"></SurfaceView> 

</RelativeLayout> 

答えて

-1

私が言うことができる唯一のことは、エミュレータがひどいということである - それも、強力なPCや事実に遅い死んで、死にましたそれはビデオを再生することはできません驚きです(私のPC上ではほとんど動作し、それも私のラップトップで起動しません!)

あなたが勇気があれば - エミュレータ'。あなたはVirtualboxの下でそれを実行し、あなたがどんなデバイスやエミュレータと同じようにそれに接続することができます。

方向を変えるのは面倒です(何らかの理由で開発者が風景に夢中になっているようです)、ARMコードに依存するものは何も動作しませんが、試してみる価値があります。

http://www.android-x86.org/

+0

が、私は私はそれが自動的に適切に自分自身をサイズおよび例えば、一貫してかなり機能しますが、代わりにVideoViewを使用してお勧めします私はテストしている480x480のビデオクリップを再生できるはずのHTC Evoで同じ問題を抱えています。 –

1

私は、通常のデバイスと同じ問題を抱えていた、最悪の部分は、毎回起こるようには見えないということなので、デバッグには非常に困難です。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" android:layout_height="match_parent" 
    android:layout_gravity="fill|bottom" android:gravity="fill|bottom"> 
    <LinearLayout android:layout_alignParentBottom="true" 
     android:layout_alignParentTop="true" android:layout_alignParentRight="true" 
     android:layout_alignParentLeft="true" android:layout_width="match_parent" 
     android:layout_height="match_parent" android:gravity="center"> 
     <VideoView android:id="@+id/videoView" 
      android:layout_centerInParent="true" android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 
    </LinearLayout> 
    <Button android:id="@+id/activate_button" style="@style/buttonStyle" 
     android:layout_width="wrap_content" android:layout_height="wrap_content" 
     android:layout_gravity="bottom|center_horizontal" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true" android:layout_alignParentLeft="true" 
     android:onClick="activateClick" android:text="@string/activateButtonLabel" /> 
</RelativeLayout> 

で使用::彼の質問への答えではありません

public class ExternalMediaPreview extends Activity implements OnPreparedListener 
    { 

     private VideoView   mVideoView; 
     protected int    resid; 
     protected String   mediaPackage; 
     protected ActivityInfo  mActivityInfo; 

     public ExternalMediaPreview() 
      { 
       super(); 
      } 

     @Override 
     public void onCreate(Bundle icicle) 
      { 
       super.onCreate(icicle); 
       requestWindowFeature(Window.FEATURE_NO_TITLE); 
       setContentView(R.layout.video_preview); 
       mVideoView = (VideoView) findViewById(R.id.videoView); 
       mVideoView.setOnPreparedListener(this); 
       try 
        { 
         Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.test_video); 
         mVideoView.setVideoURI(uri); 
         Log.i(TAG, "Uri loaded: " + uri.toString()); 
        } 
       catch(Exception e) 
        { 
         e.printStackTrace(); 
        } 
      } 
     @Override 
     public void onPrepared(MediaPlayer mp) 
      { 
       Log.i(TAG, "videoView prepared, setting loop to true"); 
       mp.setLooping(true); 
       mVideoView.start(); 
      } 
} 
関連する問題