Android视频播放问题

时间:2016-12-22 05:33:35

标签: android

在我的应用中播放视频时遇到了一些麻烦。我有类似下面的代码播放我的音频文件,但是,当我尝试播放视频时,我没有看到图片,我仍然可以听到旁白,但没有视频。只是为了排除编码,我从我的资产中删除了一个文件,这个文件是在我的单元格中创建的,我试图使用上面的方法回放它而没有运气。任何想法都将不胜感激,谢谢。

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

<VideoView
    android:layout_weight="0.3"
    android:id="@+id/videoView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />

<ListView
    android:id="@+id/fragmentVidListView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

</ListView>

public class MediaPlayer extends AppCompatActivity implements OnPreparedListener, MediaController.MediaPlayerControl {

private static final String TAG = "VideoPlayer";
private android.media.MediaPlayer mediaPlayer;
private MediaController mediaController;
private String videoFileName= "test2.mp4";
private Handler handler = new Handler();


public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_video_view);

    mediaPlayer = new android.media.MediaPlayer();
    mediaPlayer.setOnPreparedListener(this);

    mediaController = new MediaController(this);

    try {
        AssetFileDescriptor descriptor = MediaPlayer.this.getAssets().openFd(videoFileName);
        long start = descriptor.getStartOffset();
        long end = descriptor.getLength();
        mediaPlayer.setDataSource(descriptor.getFileDescriptor(), start, end);
        mediaPlayer.prepare();
        mediaPlayer.start();
    } catch (IOException e) {
        Log.e(TAG, "Could not open file " + videoFileName + " for playback.", e);
    }

}

@Override
public void onPrepared(android.media.MediaPlayer mp) {
    Log.d(TAG, "onPrepared");
    mediaController.setMediaPlayer(this);
    mediaController.setAnchorView(findViewById(R.id.videoView1));

    handler.post(new Runnable() {
        public void run() {
            mediaController.setEnabled(true);
            mediaController.show();
        }
    });
}

@Override
protected void onStop() {
    super.onStop();
    mediaController.hide();
}

@Override
public boolean onTouchEvent(MotionEvent event) { //the MediaController will hide after 3 seconds - tap the screen to make it appear again
    mediaController.show();
    return false;
}

public void start() {
    mediaPlayer.start();
}

public void pause() {
    mediaPlayer.pause();
}

public int getDuration() {
    return mediaPlayer.getDuration();
}

public int getCurrentPosition() {
    return mediaPlayer.getCurrentPosition();
}

public void seekTo(int i) {
    mediaPlayer.seekTo(i);
}

public boolean isPlaying() {
    return mediaPlayer.isPlaying();
}

public int getBufferPercentage() {
    return 0;
}

public boolean canPause() {
    return true;
}

public boolean canSeekBackward() {
    return true;
}

public boolean canSeekForward() {
    return true;
}

@Override
public int getAudioSessionId() {
    return 0;
}


@Override
public void onStart() {
    super.onStart();
}
}

0 个答案:

没有答案