首页 文章

Android MediaPlayer不显示视频,只播放视频文件的音频

提问于
浏览
1

我正在尝试在我的Android应用程序中播放视频 . 但它只播放视频的音频而不在屏幕上显示任何内容!

这是我的java活动文件:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        try {
            videoPlayer();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public void videoPlayer() throws IllegalArgumentException, SecurityException, IllegalStateException, IOException{
        Uri myUri = Uri.fromFile(new File("/sdcard/sub_sda1/a.mpg"));
        MediaPlayer mediaPlayer = new MediaPlayer();
        SurfaceView mSurfaceView = (SurfaceView) findViewById(R.id.surface);
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mediaPlayer.setDataSource(getApplicationContext(), myUri);
        mediaPlayer.prepare();
        int videoWidth = mediaPlayer.getVideoWidth();
        int videoHeight = mediaPlayer.getVideoHeight();
        //Get the width of the screen
        int screenWidth = 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);  
        mediaPlayer.start();
    }

}

并且有我的xml活动布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hamed" />

    <SurfaceView 
        android:id="@+id/surface" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:paddingTop="10dip" />

</RelativeLayout>

我也看过这些问题,但我没有得到答案:

Android MediaPlayer - Sometimes No Video is Played Even Though Audio Plays

Android MediaPlayer does not display video, only play audio

https://stackoverflow.com/questions/9302611/mediaplayer-only-playing-audio-not-audiovideo

我想我应该在xml活动布局中设置一些东西,但我没有 . 你能告诉我你的建议吗?

1 回答

  • 4

    尝试使用VideoView

    videoView = (VideoView)findViewById(R.id.VideoView);        
     videoView.setVideoPath("/sdcard/your_video.3gp");
     videoView.start();
    

    希望这会对你有所帮助 .

相关问题