1

同じ問題に関する投稿をいくつか見ましたが、まだ問題を解決できません。mp4 ビデオを再生するために、surfaceview、fragments、および mediaplayer を使用しています。音声は得られますが、ビデオは得られません。ビデオは、アプリケーション以外では問題なく再生されます。さまざまな形式を試しましたが、うまくいきません。私はエミュレータを使用していません。テストには Samsung Galaxy Tab2 を使用しています。ビデオを表示するには何が欠けていますか?

ここに私のXMLファイルがあります:

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

<SurfaceView
    android:id="@+id/video_surfaceView"
    android:layout_weight="1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
>
</SurfaceView>

<Button
    android:id="@+id/playvideoplayer"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="- PLAY "/>

<Button
    android:id="@+id/pausevideoplayer"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="- PAUSE"/>

<Button
    android:id="@+id/stopvideoplayer"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="- STOP"/> 


</LinearLayout>

これが私のコードです:

    public class ChapterVideoFragment extends Fragment {

private static final String TAG = "ChapterVideoFragment";

private static final boolean VERBOSE = true;

private SurfaceView mSurfaceView;
private SurfaceHolder mSurfaceHolder;
private MediaPlayer mp = null;

private Button  mPlayVideo, 
                        mPauseVideo,
                        mStopVideo;

private boolean mPausing = false;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent,
        Bundle savedInstanceState) {
    if (VERBOSE) Log.v(TAG, "+++ onCreateView +++");
    View v = inflater.inflate(R.layout.fragment_video, parent, false);      

    mSurfaceView = (SurfaceView)v.findViewById(R.id.video_surfaceView);     
    mPlayVideo = (Button)v.findViewById(R.id.playvideoplayer);
    mPauseVideo = (Button)v.findViewById(R.id.pausevideoplayer);
    mStopVideo = (Button)v.findViewById(R.id.stopvideoplayer);

    mp = MediaPlayer.create(getActivity(), R.raw.improvisation);        
    mPlayVideo.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
                mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
            //Get the dimensions of the video
               int videoWidth = mp.getVideoWidth();
            int videoHeight = mp.getVideoHeight();

                //Get the width of the screen
        int screenWidth =  
  getActivity().getWindowManager().getDefaultDisplay().getWidth();                  

            //Get the SurfaceView layout parameters
            android.view.ViewGroup.LayoutParams lp = mSurfaceView.getLayoutParams();

            //Set the width of the SurfaceView to the width of the screen
            lp.width = screenWidth;

            //Set the height of the SurfaceView to match the aspect ratio of the video 
            //be sure to cast these as floats otherwise the calculation will likely be       
                0
            lp.height = (int) (((float)videoHeight / (float)videoWidth) * 
                (float)screenWidth);

            //Commit the layout parameters
            mSurfaceView.setLayoutParams(lp);  

            //mp.setDisplay(mSurfaceView.getHolder());            
            mp.start();

        }
    });        

SurfaceView のコードは次のとおりです。

           mSurfaceHolder = mSurfaceView.getHolder();
    mSurfaceHolder.addCallback(new SurfaceHolder.Callback() {

        @Override
        public void surfaceDestroyed(SurfaceHolder arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void surfaceCreated(SurfaceHolder arg0) {
            if (VERBOSE) Log.v(TAG, "+++ surfaceCreated +++");              
            // TODO Auto-generated method stub
        }

        @Override
        public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
            // TODO Auto-generated method stub

        }
    });       
4

1 に答える 1

0

videoview を使用してみてください - ビデオの表示に使用しているデバイスが mediaplayer オブジェクトのフォーマットをサポートしていない可能性があります。この問題は videoview で解決しました

于 2013-06-26T04:26:14.180 に答える